FormatterBase.php 3.82 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\FormatterBase.
6 7
 */

8
namespace Drupal\Core\Field;
9 10 11 12 13 14 15 16 17

/**
 * Base class for 'Field formatter' plugin implementations.
 */
abstract class FormatterBase extends PluginSettingsBase implements FormatterInterface {

  /**
   * The field definition.
   *
18
   * @var \Drupal\Core\Field\FieldDefinitionInterface
19
   */
20
  protected $fieldDefinition;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

  /**
   * The formatter settings.
   *
   * @var array
   */
  protected $settings;

  /**
   * The label display setting.
   *
   * @var string
   */
  protected $label;

  /**
   * The view mode.
   *
   * @var string
   */
  protected $viewMode;

  /**
   * Constructs a FormatterBase object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
48 49
   * @param array $plugin_definition
   *   The plugin implementation definition.
50
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
51
   *   The definition of the field to which the formatter is associated.
52 53 54 55 56 57 58
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   */
59
  public function __construct($plugin_id, array $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode) {
60
    parent::__construct(array(), $plugin_id, $plugin_definition);
61

62
    $this->fieldDefinition = $field_definition;
63 64 65 66 67 68
    $this->settings = $settings;
    $this->label = $label;
    $this->viewMode = $view_mode;
  }

  /**
69
   * {@inheritdoc}
70
   */
71
  public function view(FieldItemListInterface $items) {
72 73
    $addition = array();

74
    $elements = $this->viewElements($items);
75
    if ($elements) {
76
      $entity = $items->getEntity();
77
      $entity_type = $entity->entityType();
78
      $field_name = $this->fieldDefinition->getFieldName();
79 80
      $info = array(
        '#theme' => 'field',
81
        '#title' => $this->fieldDefinition->getFieldLabel(),
82
        '#access' => $items->access('view'),
83 84
        '#label_display' => $this->label,
        '#view_mode' => $this->viewMode,
85
        '#language' => $items->getLangcode(),
86 87 88
        '#field_name' => $field_name,
        '#field_type' => $this->fieldDefinition->getFieldType(),
        '#field_translatable' => $this->fieldDefinition->isFieldTranslatable(),
89 90 91
        '#entity_type' => $entity_type,
        '#bundle' => $entity->bundle(),
        '#object' => $entity,
92
        '#items' => $items->getValue(TRUE),
93
        '#formatter' => $this->getPluginId(),
94
        '#cache' => array('tags' => array())
95 96
      );

97 98 99 100 101 102 103 104 105 106 107 108
      // Gather cache tags from reference fields.
      foreach ($items as $item) {
        if (isset($item->format)) {
          $info['#cache']['tags']['filter_format'] = $item->format;
        }

        if (isset($item->entity)) {
          $info['#cache']['tags'][$item->entity->entityType()][] = $item->entity->id();
          $info['#cache']['tags'][$item->entity->entityType() . '_view'] = TRUE;
        }
      }

109
      $addition[$field_name] = array_merge($info, $elements);
110 111 112 113 114 115
    }

    return $addition;
  }

  /**
116
   * {@inheritdoc}
117 118 119 120 121 122
   */
  public function settingsForm(array $form, array &$form_state) {
    return array();
  }

  /**
123
   * {@inheritdoc}
124 125
   */
  public function settingsSummary() {
126
    return array();
127 128 129
  }

  /**
130
   * {@inheritdoc}
131
   */
132
  public function prepareView(array $entities_items) { }
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  /**
   * Returns the array of field settings.
   *
   * @return array
   *   The array of settings.
   */
  protected function getFieldSettings() {
    return $this->fieldDefinition->getFieldSettings();
  }

  /**
   * Returns the value of a field setting.
   *
   * @param string $setting_name
   *   The setting name.
   *
   * @return mixed
   *   The setting value.
   */
  protected function getFieldSetting($setting_name) {
    return $this->fieldDefinition->getFieldSetting($setting_name);
  }

157
}