Drupal 7 Advanced Training
Illustration by John Hersey

Improving rules


From:

Drupal 7 Advanced Training

with Tom Geller

Video: Improving rules

In the last video, we created a rule that prevented people from changing their usernames to include the word foo in them, but although it basically worked, it had several problems. In this video, I am going to go improve and debug that rule. We won't be able to solve every problem, but we will get pretty far. This is a process that you'll find yourself doing quite a lot, so it's worth showing with a practical example. Once again, just to outline exactly what we did, we created a user named nonstaff. We also created a Staff role here.
Expand all | Collapse all
  1. 4m 51s
    1. Welcome
      1m 7s
    2. Planning your Drupal career
      3m 44s
  2. 25m 46s
    1. Comparing development and production environments
      6m 22s
    2. Configuring your settings.php file
      7m 10s
    3. Running multiple sites from one Drupal installation
      7m 32s
    4. Troubleshooting common issues
      4m 42s
  3. 51m 20s
    1. Understanding your Drupal site's place on the web
      2m 44s
    2. Selecting a web host
      7m 20s
    3. Using Unix's command-line interface
      8m 23s
    4. Working with your web host's access restrictions
      4m 51s
    5. Using cPanel and other graphical web host interfaces
      3m 40s
    6. Transferring files to and from your web host
      5m 49s
    7. Moving databases using phpMyAdmin
      6m 34s
    8. Moving databases using Unix commands
      6m 8s
    9. Modifying server configuration files
      5m 51s
  4. 27m 11s
    1. Learning from case studies
      3m 13s
    2. Planning your site
      4m 18s
    3. Populating your site with Devel Generate
      3m 43s
    4. Managing URL paths
      3m 20s
    5. Restricting access to downloadable files
      5m 55s
    6. Reusing site components with the Features module
      6m 42s
  5. 21m 54s
    1. Understanding and installing Drush
      5m 23s
    2. Installing Drupal using Drush
      7m 24s
    3. Building a site using Drush
      9m 7s
  6. 32m 0s
    1. Backing up with the Backup and Migrate module
      8m 17s
    2. Moderating comment spam
      6m 47s
    3. Migrating from Drupal 6
      4m 28s
    4. Migrating to Drupal 7
      12m 28s
  7. 22m 24s
    1. Letting users log in through OpenID
      5m 10s
    2. Letting users log in through Facebook, Twitter, and other services
      9m 43s
    3. Republishing posts on Facebook and Twitter
      7m 31s
  8. 1h 5m
    1. Understanding Drupal's base themes
      5m 55s
    2. Introducing base themes: Zen and subtheming
      11m 35s
    3. Introducing base themes: Fusion and extensibility
      10m 44s
    4. Introducing base themes: AdaptiveTheme and responsive design
      7m 25s
    5. Introducing base themes: Omega and mobile devices
      7m 9s
    6. Using Firebug and other theming tools
      7m 20s
    7. Modifying themes with Sweaver
      6m 59s
    8. Modifying themes with Livethemer
      8m 45s
  9. 49m 36s
    1. Understanding the Rules module
      6m 49s
    2. Demonstrating how Rules works in Drupal Commerce
      3m 53s
    3. Creating practical rules
      6m 37s
    4. Improving rules
      9m 10s
    5. Defining conditions with the Context module
      7m 51s
    6. Varying layout with the Context and Delta modules
      5m 0s
    7. Varying layout with the Panels module
      10m 16s
  10. 52m 46s
    1. Enabling social features
      9m 46s
    2. Implementing a voting system
      9m 15s
    3. Rewarding good behavior with the Userpoints module
      5m 57s
    4. Setting up Organic Groups
      6m 28s
    5. Adding content to groups
      2m 21s
    6. Seeing group activity better with views and panels
      10m 13s
    7. Making groups private
      4m 54s
    8. Letting each group have its own permissions
      3m 52s
  11. 25m 32s
    1. Understanding packaged Drupal distributions
      2m 8s
    2. Getting a head start with Acquia Drupal
      7m 28s
    3. Creating online storefronts with Drupal Commerce
      6m 53s
    4. Creating government web sites with OpenPublic
      9m 3s
  12. 1h 30m
    1. Taking the assignment
      3m 26s
    2. Planning the project
      3m 43s
    3. Preparing the infrastructure
      8m 42s
    4. Starting the design
      7m 35s
    5. Filling in the content
      7m 29s
    6. Developing the layout
      10m 27s
    7. Finishing the layout
      7m 8s
    8. Refining the CSS
      6m 32s
    9. Setting up sections for regional offices
      4m 53s
    10. Configuring regional offices
      7m 18s
    11. Connecting to social media
      5m 9s
    12. Cleaning up
      12m 32s
    13. Securing the site
      5m 56s
  13. 37s
    1. Next steps
      37s

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course Drupal 7 Advanced Training
7h 50m Intermediate May 17, 2012

Viewers: in countries Watching now:

This course teaches web site designers how to take their sites to the next level with a few advanced techniques and the free and open-source Drupal software. Author Tom Geller shows how to configure the most popular add-on modules; use *nix commands and an FTP program to manage a Drupal site on a web server; change its visual appearance using the latest graphical tools; automate and speed through common tasks with Drush; integrate with social media sites; and see how "supermodules" like Panels, Context, Rules, and Features open up new worlds of code-free development.

Drupal 7 Advanced Training was designed as a follow-up to Drupal 7 Essential Training and it also dovetails nicely with our other Drupal courses, such as Drupal 7 Reporting and Visualizing Data and Create Your First Online Store with Drupal Commerce.

Topics include:
  • Moving a site from the development environment to production
  • Hosting a Drupal site
  • Moving databases with phpMyAdmin and Unix commands
  • Making site administration more efficient with Drush
  • Backing up site data
  • Moderating comments
  • Migrating from previous versions of Drupal
  • Working with themes
  • Creating variable layouts
  • Enabling social features
  • Creating an online store with Drupal Commerce
Subjects:
Developer Web
Software:
Drupal
Author:
Tom Geller

Improving rules

In the last video, we created a rule that prevented people from changing their usernames to include the word foo in them, but although it basically worked, it had several problems. In this video, I am going to go improve and debug that rule. We won't be able to solve every problem, but we will get pretty far. This is a process that you'll find yourself doing quite a lot, so it's worth showing with a practical example. Once again, just to outline exactly what we did, we created a user named nonstaff. We also created a Staff role here.

The idea is that only people with the Staff role should be able to change their name to include foo somewhere in the name. I will sign in as nonstaff, and show what happens if somebody tries to change their name who is not a staff person. Go up to My account > Edit, change it to foo, and Save. It stays as nonstaff, and we get this warning message. But the first problem is that that affects all users.

It also happens to people who do have the Staff role. Well, that's easy to change. We will just add a condition. I will go back to my Administrative browser, go up to Configuration, and then down to Rules, and edit that rule, which is, Block non staff from the name foo. We will add a condition, and let's see what our options are. Ah! User has role(s); perfect. We want it to happen whenever a user who doesn't have the Staff role tries to change their name, so staff, and Negate.

In other words, this is going to be tripped unless they are a member of staff, and Save. And just to be sure, I am going to add an and in here. In other words, both of these conditions have to be true. So click Add and, and then we can rearrange this. Go down and Save. Now let's test that again. I will go back to my browser where I have nonstaff logged in. I will change that name, once again, to this is foo, and it should be turned back.

Yeah, it was turned back. Now I will go in and I'll create a new user who is a staff person. Go to People > Add user, the name will naturally be staff, and we will give them the Staff role. Then I switch over to my browser here, Log out, and re-log in as the staff person. Now I will try the same thing. Edit the name, and change it to staff foo.

If all goes well, that should pass through just fine, and it did. Now we come to the second problem. While we have stop nonstaff from changing their names to include foo, they can still create accounts that include foo. Now, there is an obvious solution, but like many obvious solutions, it's wrong. We could go back here, go up to our rule, edit it, and simply add an event up here, so that it's after updating or creating an account.

The problem with that is what the actions actually are. They will try to set a data value that doesn't exist. That is, it will look for the unchanged account name, which didn't exist before; see, right here. So what we actually have to do is create an entirely different rule. This is where things gets a little confusing, because you might have conflicting rules with the same event. If that's the case, I recommend you simply take out a piece of paper, and start writing down what your rules actually do. So let's create that rule. Now, we could create it from scratch, but a lot of what we want is actually here in this existing rule, so we will go back to our Rules, and we will clone this one.

Before I do that, I am just going to rename this, so that it has a more distinctive name. Go down to Settings, and this will be Block non staff from editing to the name foo, and we could change the machine name if we want, but that's not really necessary, and Save changes. Now I will go back up to my Rules, and I'll clone that. Block non staff from creating with the name foo. The good news is, the Machine name also changes here, so we know that there are going to be two separate rules, and we Save the changes.

Now we will change the event that triggers it. Instead of updating, it's going to be after creating a new account. So we look down at user, and see what our options are. Yup; After saving a new user account. Now, since we are allowing it to be saved, we then have to delete it once it's been created, and we Add. Then of course, we delete the old event after updating. To take into account that change that I just made, we have to look at our actions. We'll show a message on the site; that's still good.

Set a data value; once again, we're creating a new account, so this account-unchanged doesn't actually exist. I will delete that action, and then I will add a new one. This one is going to be Delete entity, and the selector is simply account; that is to say, the account that the user attempted to create, and Save. I think we're okay. Now let's test it.

I am going to switch to the other browser, and Log out from staff foo. Now I will try to create a new account, but before I do that, I am just going to go back and make sure that that person can create an account by going to Configuration, and Account settings. Yup, Visitors can create accounts. So let's give it a try. Create a new account. I'll call this person I really am foo. Now, if this works correctly, then of course, it will be rejected.

Create new account, and you see it did not log us in the way that we expected. If we go back to our administrative browser, and look at the People, that role would've shown up here at the top of the list, but it's not there, so it simply rejected it. Now, that's not really the best user interface, though, so I am going to improve it just a little bit more. I want to tell the user who tries to set up such an account that they really aren't allowed to do that. Now, we could flash a message but we can do something even better. I will go back up to Configuration, and down to Rules, and then I'm going to open a new tab, where I create a page. It's going to just be a Basic page, we'll say Forbidden username, and in the Body, You can't create a username containing "foo".

Go down to the bottom, and Save it. Let's just note this URL. It's /content/forbidden-username. Now when I go back to the Rules, one of the actions I can take when someone attempts is to redirect them to that page. So Block non staff from creating with the name foo, edit, and add one more action. This one is down under System; Page redirect, and the place it's going to go to is /content/forbidden-username.

By the way, watch out for the leading and trailing slashes. That can mess you up quite a bit. One way that I can tell that I don't need the leading slash is I look down here at the example, which doesn't have one. It will force to redirect, and you can do some other things, like put on a destination parameter if you're using those on your site. I wouldn't worry about that unless you're doing some pretty fancy stuff, though. If you don't know what it is, you can ignore it. I will Save, and give it another try. So back to my non-logged in user. I'll go back and try to create a new account.

Click Create a new account, call this one foofoo, and let's see what happens. We got that page, just as we expected. So our rule is just getting better and better, but believe it or not, it could still be improved even further. For example, the rule doesn't work right now when an administrator creates a staff account that contains foo. Also, we never addressed that annoying message that says your changes have been saved when you edit a name that gets rejected. As far as I know, that can't be solved using rules.

You would have to hide it with custom programming, or some fancy site configuration, or possibly with a contributed module. Actually, as I was going through this, I found that the Omega Tools module, and the Omega theme would give you this feature, and if you want to find out more about that, I would play with Omega Tools as you learned about in the video about the Omega theme. I also want to mention that what I showed you here could have been put together in several different ways, including using variables, components, and other aspects of rules that we didn't cover, and won't have time to in this course.

But at this point, you have the ammunition you need to go forth and learn more. The place to start, once again, is on Drupal.org in the documentation linked from drupal.org/project/rules.

There are currently no FAQs about Drupal 7 Advanced Training.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Drupal 7 Advanced Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.