LinkSeparateFormatter.php 2.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Contains \Drupal\link\Plugin\field\formatter\LinkSeparateFormatter.
 *
 * @todo
 * Merge into 'link' formatter once there is a #type like 'item' that
 * can render a compound label and content outside of a form context.
 * http://drupal.org/node/1829202
 */

13
namespace Drupal\link\Plugin\Field\FieldFormatter;
14

15
use Drupal\Core\Field\FieldItemListInterface;
16 17 18 19

/**
 * Plugin implementation of the 'link_separate' formatter.
 *
20
 * @FieldFormatter(
21
 *   id = "link_separate",
22
 *   label = @Translation("Separate link text and URL"),
23 24 25 26 27 28 29
 *   field_types = {
 *     "link"
 *   }
 * )
 */
class LinkSeparateFormatter extends LinkFormatter {

30 31 32 33 34 35 36 37 38 39 40
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
      'trim_length' => '80',
      'rel' => '',
      'target' => '',
    ) + parent::defaultSettings();
  }

41 42 43
  /**
   * {@inheritdoc}
   */
44
  public function viewElements(FieldItemListInterface $items) {
45
    $element = array();
46
    $entity = $items->getEntity();
47 48 49
    $settings = $this->getSettings();

    foreach ($items as $delta => $item) {
50
      // By default use the full URL as the link text.
51 52
      $url = $this->buildUrl($item);
      $link_title = $url->toString();
53

54
      // If the link text field value is available, use it for the text.
55
      if (empty($settings['url_only']) && !empty($item->title)) {
56
        // Unsanitized token replacement here because $options['html'] is FALSE
57
        // by default in l().
58
        $link_title = \Drupal::token()->replace($item->title, array($entity->getEntityTypeId() => $entity), array('sanitize' => FALSE, 'clear' => TRUE));
59 60
      }

61 62
      // The link_separate formatter has two titles; the link text (as in the
      // field values) and the URL itself. If there is no link text value,
63
      // $link_title defaults to the URL, so it needs to be unset.
64
      // The URL version may need to be trimmed as well.
65
      if (empty($item->title)) {
66 67
        $link_title = NULL;
      }
68
      $url_title = $url->toString();
69 70
      if (!empty($settings['trim_length'])) {
        $link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
71
        $url_title = truncate_utf8($url_title, $settings['trim_length'], FALSE, TRUE);
72
      }
73

74 75 76 77
      $element[$delta] = array(
        '#theme' => 'link_formatter_link_separate',
        '#title' => $link_title,
        '#url_title' => $url_title,
78
        '#url' => $url,
79
      );
80 81 82 83 84 85 86 87 88

      if (!empty($item->_attributes)) {
        // Set our RDFa attributes on the <a> element that is being built.
        $url->setOption('attributes', $item->_attributes);

        // Unset field item attributes since they have been included in the
        // formatter output and should not be rendered in the field template.
        unset($item->_attributes);
      }
89 90 91 92 93
    }
    return $element;
  }
}