You are here

function field_token_value_field_formatter_view in Field Token Value 7

Implements hook_field_formatter_view().

File

./field_token_value.module, line 164
Provides a field type allowing the value to be set using tokens.

Code

function field_token_value_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  // Because the field value is determined by the instance settings, even if the
  // user somehow managed to add multiple items, the same value will be set for
  // each one. Because of this we only ever use the first value.
  if (!empty($items[0]['value'])) {

    // By default a paragraph tag is used.
    $element[0] = array(
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $items[0]['value'],
    );
    if (!empty($settings['wrapper'])) {
      $wrapper_info = field_token_value_get_wrapper_by_key($settings['wrapper']);

      // Update the output tag based on the wrapper info.
      $element[0]['#tag'] = $wrapper_info['tag'];

      // If the wrapper contains attributes such as class, add them in.
      if (isset($wrapper_info['attributes'])) {
        $element[0]['#attributes'] = $wrapper_info['attributes'];
      }

      // Allow modules to alter the output of the field. For example to possibly
      // attach CSS or JS for a particular tag.
      drupal_alter('field_token_value_output', $element[0], $wrapper_info);
    }
  }
  return $element;
}