0

I have written a custom field widget called foo which is inserted into an entity form to provide an autocomplete to another web service. I am trying to get the field data to be sent in an email via Rules. I am able to get the entityform tokens using List, text and the standard widgets to show in the email body and the list. However the form field defined in custom widget "foo" appears [entityform:field_foo] rather than [entityform:field-foo] which means that the send email rule in Rules appears to ignore it.

From my understanding, I believe that I need to create declare a token using hook_token_info():

function MYMODULE_token_info() {
  $info['types']['foo'] = array (
    'name' => t('Foo'),
    'description' => t('Tokens related to the foo widget'),
    'needs-data' => 'entityform'
  );
  $info['tokens']['foo']['organisation'] = array (
    'name' => t('Janet'),
    'description' => t('Custom foo widget field'),
    'type' => 'entityform'
  );
  return $info;
}

and then declare hook_tokens():

function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if ($type == 'entityform' && !empty($data['entityform'])) {
    $elementform = $data['entityform'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'field-foo':
           $replacements[$original] = str_replace('_', '-', $name);
           break;
      }
    }
  }
  return $replacements;
}

The other avenue I was exploring was whether this token needs declaring to Rules via hook_rules_data_info() but I cannot see why so I have not explored it further as the entity form appears to be pushing it through to Rules, just not creating the token so that the email can see it.

  • Did you create a separate "field widget" or custom "field"? It sounds like you are creating a "field" and not just the widget. Is that right? You could just create a "field widget" that does your auto complete that you can use with a standard text field. That would save you a lot of work. – tedbow Dec 1 '13 at 14:00
0

I have managed to find a work around using hook_tokens(). Through viewing the $data array, I was able to find the field and write some code which manipulated the incorrect field and created the token correctly for Rules by walking through the field array.

function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
 $replacements = array();
 $sanitize = !empty($options['sanitize']);
  if ($type == 'entityform' && !empty($data['entityform'])) {
    $elementform = $data['entityform'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
       case 'field-foo':
         $name = str_replace('-', '_', $name);
         $field = $elementform->name
         $replacements[$original] = $field[und][0]['value'];
         break;
      }
    }
  }
  return $replacements;
}

I would be happy to learn if there is a better way of achieving this goal in Drupal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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