[support] need help patching a module with custom field formatter and tokens

andy baxter andy at earthsong.free-online.co.uk
Thu Mar 3 07:36:26 UTC 2011


hi,

I am writing a patch for the NAT (node auto term) module. (See 
http://drupal.org/node/1075432 for more information about what I'm 
trying to do.)

The thing I'm having a problem with is using token_replace() inside a 
hook_field_formatter_view() hook function to generate a link path from a 
formatter setting string. One of the tokens I've created works fine - it 
uses the $term object for a taxonomy term, which is returned as part of 
the existing module code. When I set the setting to 
'nat-bad-link/[term:tid]', it is correctly transformed into e.g. 
'nat-bad-link/65'.

However I can't get my code to work with the other 3 objects I want to 
expose, which are all passed as function parameters: $field, $entity, 
and $entity_type. E.g. if I use the token '[field:id]', the token isn't 
replaced in the final path. I am explicitly casting these to (object) 
because they are passed in as nested arrays.

I've tried printing out $term and (object) $field using dpr(), and the 
results look identical in structure, so I can't see why it isn't working.

The code I have written looks like this (partly from the original nat 
code, with some additions):

  /**
  * Implements hook_field_formatter_view().
  */
function nat_field_formatter_view($entity_type, $entity, $field, 
$instance, $lan
gcode, $items, $display) {
   $element = array();
   $settings = $display['settings'];
   foreach ($items as $delta => $item) {
     if ($item['tid'] == 'autocreate') {
       $element[$delta] = array('#markup' => check_plain($item['name']));
     }
     else {
       $term = $item['taxonomy_term'];
       $nids = array_keys(nat_get_nids(array($term->tid), FALSE));
       if ($nids && $nids[0]) {
         $element[$delta] = array(
           '#type' => 'link',
           '#title' => $term->name,
           '#href' => "node/" . $nids[0]
         );
       }
       else {
         // it's a bad link, so mark it as such and redirect to a 
handling page.
         if ($settings['missing_link_handler_path'] != '') {
           // first substitute the arguments.
           //dpr($term);
           //dpr((object)$entity_type);
           // ******************************* This is the bit that won't 
work ************************
           
$href=token_replace($settings['missing_link_handler_path'],array(
             'term' => $term,
             'field' => (object) $field,
             'entity_type' => (object) $entity_type,
             'entity' => (object) $entity,
             )
           );
           $element[$delta] = array(
             '#type' => 'link',
             '#title' => $term->name,
             '#href' => check_plain($href), // TODO - check whether 
check_url is needed.
             '#prefix' => '<span class="nat-missing-link">',
             '#suffix' => '</span>'
           );
         }
         else {
           $element[$delta] = array('#markup' => check_plain($term->name));
         }
       }
     }
   }

   return $element;
}

Any help appreciated as I was stuck for hours on this last night! :-)

thanks,

andy baxter


More information about the support mailing list