4

I have installed the Examples for Developers module, and enabled the Field Example submodule. It creates a new field type called field_example_rgb.

I've added this field to a new content type. I'm trying to set and/or display the value of the field using Rules - to no avail. Rules does not "see" the field in the node.

I've tried adding a file called field_example.rules.inc that implements hook_rules_data_info(), still without luck. Can someone please help me figure out how to make the field visible to Rules?

This is the code of the hook_rules_data_info()

   function field_example_rules_data_info() {
  return array(
    'field_example_rgb' => array (
      'label' => t('example color rgb'),
      'wrap' => TRUE,
      'property info' => array(
        'rgb' => array(
            'type' => 'text',
            'label' => 'RGB',
        ),
    ),
    ),
  );
}
3
  • 1
    Are you using the condition "entity has field" to get the fields in rules? Mar 6, 2015 at 6:29
  • It's entity Api you need to integrate with directly, not rules - rules uses entity for such things. As luck would have it, someone's already written the exact code for you: thecarneyeffect.co.uk/…
    – Clive
    Mar 6, 2015 at 10:03
  • Clive, so simple yet elegant, it brought tears to my eyes. Thank you! It worked!! (@burnsjeremy, no need to use "entity has field" - Clive's trick just works). Clive, would you like to enter your comment as an answer so that I can mark it as correct?
    – argiepiano
    Mar 6, 2015 at 13:35

1 Answer 1

0

Just add the property_type key to the array you already have, like so:

'property_type' => 'text',

With that added line, the Entity API knows how to handle that field.

For a more complex field, such as fields with more than one database column, it is not enough to simply declare the property type. You need to specify further callbacks that may alter the generated property info. To do so, you need to use the key property_callbacks to add a custom callback function that will provide the extra metadata required. For example:

'property_type' => 'field_example_rgb',
'property_callbacks' => array('field_example_rgb_property_info_callback'),

Source: Describe field properties to make custom fields work with Entity Metadata Wrappers, which includes some more info about this topic also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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