Assume a list of entities, created by the Views module. For example a list of "All blocked/active users" which looks like so:

User name      Active  Created date        Last login
Hillary.Trump  No      06/18/2016 - 13:45   
Donald.Clinton No      06/18/2016 - 13:42   
Someone.Else   Yes     05/12/2016 - 12:09  09/17/2017 - 11:18
Pierre.Vriens  Yes     03/11/2016 - 21:05  10/06/2017 - 16:04
Dries.Buytaert Yes     11/23/2015 - 10:11  09/17/2017 - 10:38
Anonymous      No

The goal is to use the Rules module to change the status for selected users from blocked to active. This should be possible by also using the Rules Link module, based on what's mentioned on the project page of this module, i.e.:

Features: ... The links are usually displayed using Views...

Usages: ... The Rules Link module will create a Views field for each link, which can be hidden, using the visibility conditions ...

My question: How to show authorized users a link for relevant Views results using the Rules Link module? Some more details about my question:

  • The links should be shown within an extra column, (say) as last column to the right.
  • Authorized users: the extra column should only be shown to users with permission to use (click) the link, say to users with role "Administrator".
  • Relevant: the link should only be shown for users for which the value of the Active column is No. Obviously, for Anonymous it doesn't make sense to show that link (since you cannot change the status for Anonymous to active.

For the sake of completeness, here is an export of my (extremely basic) view related to the sample above (also note the view_of_users path in it):

$view = new view();
$view->name = 'view_of_users';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'View of Users';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'All blocked/active Users';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'administer users';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'name' => 'name',
  'rid' => 'rid',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'name' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
  'rid' => array(
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = 'User name';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
/* Field: User: Active */
$handler->display->display_options['fields']['status']['id'] = 'status';
$handler->display->display_options['fields']['status']['table'] = 'users';
$handler->display->display_options['fields']['status']['field'] = 'status';
$handler->display->display_options['fields']['status']['not'] = 0;
/* Field: User: Created date */
$handler->display->display_options['fields']['created']['id'] = 'created';
$handler->display->display_options['fields']['created']['table'] = 'users';
$handler->display->display_options['fields']['created']['field'] = 'created';
$handler->display->display_options['fields']['created']['date_format'] = 'short';
$handler->display->display_options['fields']['created']['second_date_format'] = 'long';
/* Field: User: Last login */
$handler->display->display_options['fields']['login']['id'] = 'login';
$handler->display->display_options['fields']['login']['table'] = 'users';
$handler->display->display_options['fields']['login']['field'] = 'login';
$handler->display->display_options['fields']['login']['date_format'] = 'short';
$handler->display->display_options['fields']['login']['second_date_format'] = 'long';
/* Sort criterion: User: Created date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'users';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'view_of_users';
up vote 1 down vote accepted

Below are the steps I used to get this to work ...

Step 1: Add a Rules Link

Enable the Rules Link module, and navigate to admin/config/workflow/rules_links where you can use the link to relative path admin/config/workflow/rules_links/add to perform an Add rules link. Enter these values on the various form fields:

  • Label: Unblock a blocked user, with related machine name unblock_a_blocked_user (change to whatever you like, but better don't change it afterwards or strange things may happen, so make sure to pick the right label/name from the start).
  • Entity type: User (change to whatever entity that fits your needs). I did not check the related checkbox "Show link when entity is rendered" (pick what fits for you).
  • Link text: Unblock user (change to whatever you like).
  • Link type: Normal (though Confirmation could be a valid alternative).
  • Path: unblock_user (change to whatever you like).
  • Additional Variables: no need for additional variables, except the "User" variable already shown with Data type = User, Label = User, Machine name = user and Usage = Parameter (too bad you cannot adapt its Label / Machine name to something more self explaining, like "User to unblock").

Use the Continue button to continue, after which you'll get an informational message like so:

Your Rules Link has been created along with the required components. Please remember to set the permissions for your link and to add actions to your reactions rule.

Note that an extra tab is added also, labeled "Configure Rules" (to the right of the tab labeled "Edit").

Use the Save Rules Link button at the bottom to move on, which will then show the screen again located at admin/config/workflow/rules_links. It will now show an extra entry with a label like Unblock a blocked user (Machine name: unblock_a_blocked_user) (or whatever values you used to create the Rules Link).

Here is an export of the Rules Link described above:

{
  "settings" : {
    "text" : "Unblock user",
    "link_type" : "token",
    "bundles" : [],
    "entity_link" : 0,
    "view_mode" : []
  },
  "name" : "unblock_a_blocked_user",
  "label" : "Unblock a blocked user",
  "path" : "unblock_user",
  "entity_type" : "user",
  "rdf_mapping" : []
}

Step 2: Configure permissions

Navigate to /admin/people/permissions and move on to the "Rules Link" section of it (located at admin/people/permissions#module-rules_link). There should be an entry like "Unblock a blocked user: Execute rules link", for which you can specify the roles that should be allowed to use this Rules Link.

As per the Authorized users part of my question, I granted the permission to role Administrator (only). That will ensure that for any user who does not have the role Administrator, but who does have access to the views results (as in the question), the Rules Link is hidden for such user.

Step 3: Configure Access Conditions of the Rules Link

Head back to admin/config/workflow/rules_links/ and click the edit link of the Rules Link you created before (in my case located at admin/config/workflow/rules_links/manage/unblock_a_blocked_user/components). Then use the "Configure Rules" tab to specify the relevant "Access Conditions". Don't be confused ... 'Access' here is not about permissions! Instead they are like an extra refinement of the permissions: they can be used to specify when to show the Rules Links.

As per the Relevant part of my question, I specified these Rules Conditions (which must both be satisfied), using the Add condition link (right above "Reactions"):

  1. Data comparison, with:

    • Data to compare: [user:uid]
    • Operator: is greater than
    • Data value: 0
  2. Data comparison, with:

    • Data to compare: [user:status]
    • Data value: Blocked

The 1st condition will ensure the link is NOT shown for Anonymous. And the 2nd condition will ensure the link is only shown for users for which the value of the Active column is No.

These Access Conditions of the Rules Link are actually saved as a regular Rules Component (using a Component Plugin type of "Condition set (AND)" and with machine name rules_link_condition_unblock_a_blocked_user). Here is how it looks like (in Rules Export format):

{ "rules_link_condition_unblock_a_blocked_user" : {
    "LABEL" : "Rules link: Unblock a blocked user condition",
    "PLUGIN" : "and",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "user" : { "label" : "user", "type" : "user" } },
    "AND" : [
      { "data_is" : { "data" : [ "user:uid" ], "op" : "\u003E", "value" : "0" } },
      { "data_is" : { "data" : [ "user:status" ], "value" : "0" } }
    ]
  }
}

Step 4: Configure Reactions of the Rules Link

The bottom part of the "Configure Rules" tab, below the label Reactions, is used to specify the relevant set of rules (1 or more) to be performed when the Rules Link is actually used (clicked). As per my question, I need to create a rule here to "change the status for selected users from blocked to active". To do so, I used the Add rule link (right above "Settings") to add a new rule (I only need 1) with name "Make user active".

Use the Save changes button, which will then show the screen to create the actual rule. For my case, this is how the actual rule looks like:

  • Rules Conditions: none (I don't need any extra validations).
  • Rules Actions:

    • Unblock a user, for User: [user]
    • Page redirect, with URL = view_of_users ( * )

Use the Save changes button at the bottom to move on.

( * ): this page redirect (to path view_of_users) is actually a work around for the outstanding Rules Link feature request Stay on current page. It ensures the same views result is displayed again and avoids that after using the link, the site's homepage is shown. Adapt this path to the path of your own views result.

These Reactions of the Rules Link are actually saved as a regular Rules Component (using a Component Plugin type of "Rule set" and with machine name rules_link_set_unblock_a_blocked_user). Here is how it looks like (in Rules Export format):

{ "rules_link_set_unblock_a_blocked_user" : {
    "LABEL" : "Rules link: Unblock a blocked user rules set",
    "PLUGIN" : "rule set",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "user" : { "type" : "user", "label" : "user" } },
    "RULES" : [
      { "RULE" : {
          "DO" : [
            { "user_unblock" : { "account" : [ "user" ] } },
            { "redirect" : { "url" : "view_of_users" } }
          ],
          "LABEL" : "Make user active"
        }
      }
    ]
  }
}

Step 5: Add the links as extra Views column

With everything as described in the previous steps in place, the finishing touch is to have the links produced by the Rules Link module actually show up in an extra column added to the View results.

However, assuming you're familiar with the basics of Views, it isn't obvious at first about how to actually add that extra column. The clue to get it to work, can be found in this part of the module's README.txt:

Rules Links link will create a Views field for each link, using the title of the link as title for the field. So if you have link for nodes with the delete 'Delete node', you'll find the Views field under name 'Content: Delete node'.

In my case my entities are Users instead of Nodes. And the Views field I'm looking for is 'User: Unblock a blocked user' (whereas the 'Unblock a blocked user'-part of it equals the label of the Rules Link I added, as explained in Step 1). So that is the field I added as extra (last) field.

Final result

After completing the previous Steps, the updated list of "All blocked/active users" looks like so for a user with role Administrator:

User name      Active  Created date        Last login
Hillary.Trump  No      06/18/2016 - 13:45                      Unblock user
Donald.Clinton No      06/18/2016 - 13:42                      Unblock user
Someone.Else   Yes     05/12/2016 - 12:09  09/17/2017 - 11:18
Pierre.Vriens  Yes     03/11/2016 - 21:05  10/06/2017 - 16:04
Dries.Buytaert Yes     11/23/2015 - 10:11  09/17/2017 - 10:38
Anonymous      No

Note the extra column added (to the right), with links labeled Unblock user which are only shown for the blocked users, and not for Anonymous. After using (clicking) that link, for some user with Active=No, the user's status becomes Active, and the link is no longer shown for that user.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.