function link_field_formatter_view

Implements hook_field_formatter_view().

File

drupal/core/modules/field/modules/link/link.module, line 342
Defines simple link field types.

Code

function link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  foreach ($items as $delta => $item) {

    // By default use the full URL as the link title.
    $link_title = $item['url'];

    // If the title field value is available, use it for the link title.
    if (empty($settings['url_only']) && !empty($item['title'])) {

      // Unsanitizied token replacement here because $options['html'] is FALSE
      // by default in theme_link().
      $link_title = token_replace($item['title'], array(
        $entity_type => $entity,
      ), array(
        'sanitize' => FALSE,
        'clear' => TRUE,
      ));
    }

    // Trim the link title to the desired length.
    if (!empty($settings['trim_length'])) {
      $link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
    }
    if ($display['type'] == 'link') {
      if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
        $element[$delta] = array(
          '#type' => 'markup',
          '#markup' => check_plain($link_title),
        );
      }
      else {
        $element[$delta] = array(
          '#type' => 'link',
          '#title' => $link_title,
          '#href' => $item['path'],
          '#options' => $item['options'],
        );
      }
    }
    elseif ($display['type'] == 'link_separate') {

      // The link_separate formatter has two titles; the link title (as in the
      // field values) and the URL itself. If there is no title value,
      // $link_title defaults to the URL, so it needs to be unset.
      // The URL title may need to be trimmed as well.
      if (empty($item['title'])) {
        $link_title = NULL;
      }
      $url_title = $item['url'];
      if (!empty($settings['trim_length'])) {
        $url_title = truncate_utf8($item['url'], $settings['trim_length'], FALSE, TRUE);
      }
      $element[$delta] = array(
        '#theme' => 'link_formatter_link_separate',
        '#title' => $link_title,
        '#url_title' => $url_title,
        '#href' => $item['path'],
        '#options' => $item['options'],
      );
    }
  }
  return $element;
}