Book Image

Drupal Rules How-to

By : Robert Varkonyi
Book Image

Drupal Rules How-to

By: Robert Varkonyi

Overview of this book

Rules is what every Drupal site builder and developer has to use when creating event ñ action-based applications. The framework provides a highly flexible way to create sophisticated, condition-based functions any Drupal based system into an interactive application. Rules makes Drupal rule the CMS world."Drupal Rules How-to" is a practical, hands-on guide that provides you with a number of clear step-by-step exercises, which will help you take advantage of the real power of the Rules framework, and understand how to use it on a site builder and developer levelThis book demonstrates the power and flexibility of the Rules framework. It discusses the main aspects of the module both from the site builder and developer perspective, from basic and advanced Rule configurations using Events, Conditions, Actions and Components to getting familiar with the Rules API. You will also learn how to use additional modules together with Rules to further extend the possibilities of your Drupal system, such as Rules Scheduler to schedule the execution of your Rule configurations and Views Bulk Operations to execute Rule configurations on a view result list. The book also demonstrates the main API features that enable you to create your own Events, Conditions and Actions, provide new data types to Rules and execute your configurations programmatically
Table of Contents (7 chapters)

Adding a taxonomy term to a node using Views Bulk Operations and Rules (Should know)

This recipe describes how to add a specific taxonomy term to a list of nodes using Views Bulk Operations (VBO) and Rules.

Getting ready

Install and enable Views, Views UI, and Views Bulk Operations. Go to Structure | Views and create a new table view that lists all nodes posted on the site, and add a Bulk operations: Content field to it.

How to do it...

  1. Create a new rule component, select the Rule plugin and require an Entity | Node parameter. Name the new component Add taxonomy term to node.

  2. Add a Condition, Entities | Entity has field, use node as the entity, and set the field to field_tags.

  3. Add a new Action, Data | Add an item to a list and set the value to node:field-tags.

  4. In the Item to add fieldset, click on the Switch to the direct input mode button and enter the ID of the taxonomy term to add, as shown in the following screenshot:

  5. Go back to the view and click on the Bulk Operations: Content field.

  6. In the popup window, select the rule component we created in the SELECTED OPERATIONS fieldset, as shown in the following screenshot:

How it works...

Views Bulk Operations can use Rules components to execute Actions on a list of entities and objects. We can create the Rules components with parameters and VBO will make these components available as operations in our Bulk Operations field configuration, if the field type matches the component's parameter type. For example, when creating a component that requires a Node parameter, we need to add the same type of VBO field (Content: Bulk Operations) to the view, because this is how VBO determines what kind of parameter is being passed to Rules.

There's more...

If we want this feature to be a bit more flexible and choose a taxonomy term, we want to add to the nodes instead of always adding a preconfigured term ID, we can do the following:

  1. Add a new Entity | Taxonomy term parameter to our component, set the machine name to the term.

  2. Edit our Action, Add an item to a list, in the Item to add fieldset, click on Switch to data selection, and enter term. This is given in the following screenshot:

    Now when executing the operation, VBO will display a configuration screen where we can enter the ID of the taxonomy term we want to add to the node.