0

I am using an custom field formatter for a view field like this:

function custom_fields_field_formatter_info() {

  return array(
    // the key must be unique, so it's best to prefix with your module's name.
    'custom_fields_breadcrumbs' => array(
      'label' => t('Breadcrumbs'),
      'field types' => array('entityreference'),
    ),
  );
}

/**
* Implements hook_field_formatter_settings_form().
*/
function custom_fields_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {

  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
   if ($display['type'] == 'custom_fields_breadcrumbs') {
    $form = array();
       $form['active'] = array(
      '#type' => 'textfield',
      '#title' => t('Mark as active'),
      '#description' => t('You can specify a node id, which should have class "active". Yo can use replacement tokens.'),
      '#default_value' => $settings['active'],
      '#size' => 4,
      '#maxlength' => 128,
      '#required' => FALSE,
    );    
  }
    return $form;
}
function custom_fields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  $element = array();
  $settings = $display['settings'];

  switch ($display['type']) {
     case 'custom_fields_breadcrumbs':
      $ids = array();
      foreach ($items as $item) {
        $ids[$item['target_id']] = $item['target_id'];
      }
      $items = array();
      // http://api.drupal.org/api/drupal/modules!node!node.module/function/node_load_multiple/7
      $items = node_load_multiple($ids, array(), FALSE);
      foreach ($items as $delta => $item) {
        $label = $item->title;
        // modeled after entityreference_field_formatter_view() in entityreference.module
        if ($display['settings']['link'] && ($uri = node_uri($item))) {
          $otpions = array();
          if (isset($settings['active']) && $delta == $settings['active']) {
            $otpions = array('attributes' => array('class' => array('active')) );
          }
          $element[$delta] = array('#markup' => l($label, $uri['path'], $otpions));
        }
        else {
          $element[$delta] = array('#markup' => check_plain($label));
        }
      }
      break;
  }
  return $element;
}

Everything works as expected, but I want to be able to use replacement patterns for my custom setting $settings['active']. How do I do this?

I think, I have to use the method:

tokenize_value()

from the views object. But I don't have the views object within the hook, haven't I?

0

I found out how to do this. We have the views object via the parameter "display", so we can use:

$display['views_view']->style_plugin->tokenize_value($settings['pager']['active'], 0)

to tokenize a field value.

| improve this answer | |

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.