Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating practical rules

From: Drupal 7 Advanced Training

Video: Creating practical rules

You've seen the theory behind how the Rules module works, we created a simple rule, and you've seen some pretty fancy applications, using Drupal Commerce. Now, in the next two videos, let's go a little deeper into the complexity of rules, using a practical example. Here's the scenario: let's say that you allow the public to create accounts on your site, and they are allowed to change their usernames, but only people who are on your staff are allowed to have the word foo in their usernames. So you need to prevent anybody who's not on staff from having a username that includes the word foo.

Creating practical rules

You've seen the theory behind how the Rules module works, we created a simple rule, and you've seen some pretty fancy applications, using Drupal Commerce. Now, in the next two videos, let's go a little deeper into the complexity of rules, using a practical example. Here's the scenario: let's say that you allow the public to create accounts on your site, and they are allowed to change their usernames, but only people who are on your staff are allowed to have the word foo in their usernames. So you need to prevent anybody who's not on staff from having a username that includes the word foo.

To make this simulation work, I'll create a rule called staff, so we can differentiate between company staff, and the general public. I won't actually make that differentiation in this video; it'll be in the next one, but here we go. We simply go up to People > Permissions, and then Roles. We create the staff role, and we move to its proper place above the administrator, and Save. Now we go into the Permissions, and we just have to make one small change. Go all the way down to the User group. We want to allow people to Change their own username.

When we click Authenticated User, of course, that also checks the staff box, since it includes all authenticated users, including those with any kind of role. And then I Save permissions. Now let's make the rule. We go to Configuration, then down to Rules, and then we Add a new rule. I'll call this Block non-staff from the name foo, and the event is going to be, let's take a look; it's something on the User group. Ah, After updating an existing user account. Now, that doesn't take care of all of the different possibilities; someone can still create an account with that name, but we'll get to that in the next video.

Then I click Save. The next thing we need to do is figure out a condition. When should we trip this rule? We go down to conditions, and click Add condition. There are many options here, and it usually takes a while to figure out which is which. For example, what's the difference between Data comparison, and Text comparison? As with the Views module, the more time you spend on these and just playing around, the better you'll understand which ones you need. I happen to know that in this case, I need Text comparison, so I select it, and then I get some additional options here.

The first question is, what I'm going to compare it to? Well, if I look at the Data Selectors, I see that I have lots and lots of options. I know it has to do with an account name, but there are some subtle differences between site:current-user:name, and account:name, and so forth. Well, after playing around with it for a while, I happen to know that the one that I want is account:name. So I'll go up here, add account, and then colon, that's shows me all the options in that group, and then account:name. Scroll down, and then I could add the various things I want to actually compare.

I want to compare that Value to the string foo. And down under the Comparison Operation, starts with, ends with; no. As long as it contains foo, we shouldn't allow them to change their name. I also want to point out this little checkbox. You can create negative rules. In other words, you can say either let everything pass if it matches this comparison, or make everything fail if it matches this comparison. I'll click Save. In this case, I'm making the action be tripped if somebody tries to enter foo in their username.

The next step is to add our actions, and I'm going to add two. The first one is going to be a warning. The action I want is under the System group: Show a message on the site. Then I have my options: You can't have a user name that contains "foo". Try again. I scroll down, and decide that I'll make that a Warning message. That sounds like an appropriate kind. Status is a green thing at the top of your screen; it doesn't look so scary. Warning is yellow, and Error is red, each with its own icon.

And I will let it Repeat, in case a person tries more than once, and click Save. Now, that tells them that they can't do it, but I also want to add an action that stops them from doing it. So I'll click Add action, and once again, a little bit of playing around has told me that the Value that I want here is Set a data value, so I'll select that, and the thing that's going to change is, once again, the account:name. This one right here. So I go back up say, account:name, Continue, and then I say what I want it to be set to. And the Value is actually going to be an unusual little token down here.

We have account-unchanged; in other words, it looks back to what it was before the person attempted to change it, and it's going to be account-unchanged:name. So I copy it, and paste it, go down to the bottom of the screen, and Save. Now let's give this rule a try. To do so, I am going to up to People, and I'll add a user. This user is going to be called, lets say, nonstaff. E-mail address will be nonstaff@ example.com, and has a password.

Now, this person is not a staff member, remember. We will create the new account, and then go over to a browser where we're not logged in. I'll login as that person. In order to change a username when you're logged in, you go up to My account, and then click Edit. Let's see what happens if this person says, my name is foo, which of course, contains the string that we want to forbid. Go down to the bottom, and click Save, and indeed, we got exactly what we wanted.

We get the Warning message right here, and the username stays as nonstaff. The thing is, if you are not careful, you might be tempted to stop here, but it's not really finished First of all, we haven't differentiated between staff members and nonstaff members. We trigger the same rule even when staff members want to change their name. Second, this rule only gets triggered when user's change an existing account. They could still create that account with foo in it just fine. Third, we still have this confusing line here that says, The changes have been saved, even though they weren't.

We'll look at each of these issues in the next video.

Show transcript

This video is part of

Image for Drupal 7 Advanced Training
Drupal 7 Advanced Training

72 video lessons · 11131 viewers

Tom Geller
Author

 
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 10s
    1. Learning from case studies
      3m 13s
    2. Planning your site
      4m 18s
    3. Populating your site with Devel Generate
      3m 42s
    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 53s
    1. Understanding and installing Drush
      5m 23s
    2. Installing Drupal using Drush
      7m 24s
    3. Building a site using Drush
      9m 6s
  6. 31m 59s
    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 27s
  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 35s
    1. Understanding the Rules module
      6m 48s
    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 learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.

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 ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.