It appears that this module does not work with rules. I am trying to setup a coupon type on drupal commerce that has a role reference field with the intention to have the coupon be limited to the roles selected. This was originally discussed here: http://drupal.org/node/1466622

Having tried this now, I am unable to get the role reference field to be available when setting up a rules condition to check which roles have been selected using this field. My assumption, seeing as there are no other issues addressing rules integration with this module is that it simply hasn't happened. Thus the feature request.

Any help is appreciated. Thanks!

Comments

Jorrit’s picture

Status: Active » Needs review

The fix is very simple:

edit rolereference.module, and insert the following piece of code in rolereference_field_info():

'property_type' => 'integer',

Such that it becomes:

function rolereference_field_info() {
  return array(
    'rolereference' => array(
      'label'             => t('Role reference'),
      'description'       => t('This field stores the ID of a related role as an integer value.'),
      'settings'          => array('referenceable_roles' => array()),
      'default_widget'    => 'options_select',
      'default_formatter' => 'rolereference_default',
      'property_type'     => 'integer',
    )
  );
}

Now clear your caches. Rules will now detect your field to be a list of integers (if cardinality > 1) or an integer (if cardinality = 1). I am now able to use the roles reference field as input for the "Add user role" action.

If you don't want to edit a contributed module, you can add the following code in your own module:

/**
 * Implements hook_field_info_alter().
 *
 * Adds support for using role reference fields in Rules and other
 * modules that depend on Entity API metadata.
 */
function YOURMODULE_field_info_alter(&$info) {
  if (!isset($info['rolereference']) || isset($info['rolereference']['property_type'])) {
    return;
  }

  $info['rolereference']['property_type'] = 'integer';
}
RyanPrice’s picture

Status: Needs review » Reviewed & tested by the community

Thanks Jorrit,

I can confirm this does the trick and is already rolled into the 7.x-1.x-dev version.

RyanPrice’s picture

Status: Reviewed & tested by the community » Closed (fixed)

Closing as its already been rolled.

Jorrit’s picture

OK, sorry I hadn't seen that. Could a new release be created?

RyanPrice’s picture

I only saw it because I was going to submit a patch with your solution only to find it was already there in the dev release.

I have no idea what the maintainer's plan is for a release schedule.

gregsomers’s picture

Version: 7.x-1.x-dev » 7.x-1.1
Category: Feature request » Bug report
Issue summary: View changes
Status: Closed (fixed) » Active

This doesn't seem to be working. In the database, the type of this field is stored as rolereference. Rules is looking for type list_integer to assign roles..

The data selector does not allow me to use a rolereference field to assign a user roles.

Thoughts?

EDIT: on further inspection, it fails the rules type check RulesData::typesMatch

juliakoelsch’s picture

Hi, I ran into this issue also, and there is an issue in the Rules issue queue that can help fix this. Once you apply the patch, you will need to create an action called "Generate a list from an integer". Choose your rolereference field and create a variable. Then, create a second action to assign roles, refer to the variable created in the first action. The Rules issue containing the patch is here: https://drupal.org/node/1525582

I'm not sure if there is something that RuleReference can change to prevent the need for having to manually create a list from the field value, so leaving the issue open.

rjbrown99’s picture

Thanks for this, although it didn't help in my case. Rules was still unable to see rolereference integer values and the Rules patch (referenced in #7) created a list of ALL roles (starting with 0) instead of just picking up one single role.

In my case, I wanted to read the single role value from a source node and use it to built access to a destination node. My solution was:

Step 1. Add a variable
- Type integer
- value: node:nid (the source node that has the rolereference field)
- php evaluation (where my node's rolereference field is called field_webform_role)

$node = node_load($value);
if (isset($node->field_webform_role['und'][0]['rid'])) {
  return $node->field_webform_role['und'][0]['rid'];
}
else {
  return 0;
}

That left me with a variable with the integer role # of the role in question. Secondly:

Step 2. Add a variable
- List of integer
- (no value, just create the list)

Step 3. Add an item to a list
- Use the list created above in step 2.
- Use the data selector created in step 1.

In my case I'm using this content access patch: https://www.drupal.org/project/content_access/issues/1464948

I was then able to use my list as input to that rule to set the appropriate permissions on the created node.

Step 1 is non-ideal but rules won't pick up or allow me to use the value here because it's not stored as an integer. The patch from #7 would create a list such as (1,2,3,4,5,6,7) if your original role ID was 7. In my case that would grant access to all roles #1-7 when used, which is not what I wanted.

Hope that helps someone else.