You are here

function linkicon_field_formatter_view in Link Icon 7

Implements hook_field_formatter_view().

File

./linkicon.module, line 88
A link field formatter to create icon classes based on predefined titles.

Code

function linkicon_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if (empty($items) || $display['type'] != 'linkicon') {
    return $element;
  }
  $path = drupal_get_path('module', 'linkicon');
  $field_type = $field['type'];
  $field_name = $instance['field_name'];
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  $config = linkicon_simplify_settings($display['settings']);
  $prefix_class = check_plain($config['prefix']);
  $contents = array();
  foreach ($items as $delta => $item) {

    // The A tag attributes.
    $attributes = array();
    $attributes['class'][] = 'linkicon__item';

    // Linkicon requires both link text and URL available with proper
    // validation during input, no need extra checks.
    $icon_name = $field_type == 'link_field' ? $item['title'] : check_plain(strip_tags($item['value']));
    $display_title = isset($item['display_title']) ? $item['display_title'] : $icon_name;
    $tooltip = isset($item['tooltip']) ? $item['tooltip'] : $display_title;
    $icon_class = drupal_clean_css_identifier(drupal_strtolower($prefix_class . '-' . $icon_name));

    // This is similar as widget setting Static title, only available at
    // Display for more flexible options by view modes.
    if (!empty($config['global_title']) && empty($config['no_text'])) {
      $display_title = $config['global_title'];
    }

    // Sanitize $display_title before use since html is TRUE for the link.
    $item['html'] = TRUE;

    // Tokenized text is sanitized by default.
    $display_title = token_replace($display_title, array(
      $entity_type => $entity,
    ));
    $character_maxlength = isset($config['maxlength']) && $config['maxlength'] ? $config['maxlength'] : 60;
    $icon_element = array(
      '#theme' => 'linkicon_item',
      '#title' => truncate_utf8($display_title, $character_maxlength, TRUE, TRUE),
      '#icon_name' => check_plain($icon_name),
      '#settings' => $config,
    );
    $item['title'] = drupal_render($icon_element);

    // Link module stores classes as spaced string.
    if (!empty($item['attributes']['class'])) {
      $attributes['class'] = array_merge(array(
        $item['attributes']['class'],
      ), $attributes['class']);
    }

    // Unlike D8 link module, D7 link expect a spaced string, so feed it.
    $item['attributes']['class'] = implode(" ", $attributes['class']);

    // Our pure CSS3 tooltip depends on data-title.
    if ($config['tooltip']) {
      $tooltip = token_replace($tooltip, array(
        $entity_type => $entity,
      ));
      $item['attributes']['data-title'] = truncate_utf8($tooltip, $character_maxlength, TRUE, TRUE);
    }

    // We are done for the item, pass it over to link to do its job.
    $contents[$delta] = array(
      '#theme' => 'link_formatter_link_default',
      '#element' => $item,
      '#field' => $instance,
      '#display' => $display,
    );
  }

  // Build own wrapper for greater control.
  $config['id'] = drupal_clean_css_identifier("linkicon-{$entity_type}-{$bundle}-{$field_name}-{$id}");
  $element[0] = array(
    '#theme' => 'linkicon',
    '#items' => $contents,
    '#config' => $config,
  );

  // Attached our assets if so configured.
  if (!empty($config['load'])) {
    $element[0]['#attached']['css'][] = array(
      'data' => $path . '/css/linkicon.css',
    );
    if ($config['font'] && empty($config['bundle'])) {
      $element[0]['#attached']['css'][] = array(
        'data' => strip_tags($config['font']),
      );
    }
  }
  return $element;
}