EntityDisplay.php 1.87 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity\Entity\EntityDisplay.
6 7
 */

8
namespace Drupal\entity\Entity;
9

10
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
11
use Drupal\entity\EntityDisplayBase;
12 13 14

/**
 * Configuration entity that contains display options for all components of a
15
 * rendered entity in a given view mode.
16
 *
17
 * @EntityType(
18 19
 *   id = "entity_display",
 *   label = @Translation("Entity display"),
20 21 22
 *   controllers = {
 *     "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
 *   },
23 24 25
 *   config_prefix = "entity.display",
 *   entity_keys = {
 *     "id" = "id",
26 27
 *     "uuid" = "uuid",
 *     "status" = "status"
28 29 30
 *   }
 * )
 */
31
class EntityDisplay extends EntityDisplayBase implements EntityViewDisplayInterface {
32

33 34 35 36 37
  /**
   * {@inheritdoc}
   */
  protected $displayContext = 'view';

38
  /**
39
   * {@inheritdoc}
40
   */
41 42
  public function __construct(array $values, $entity_type) {
    $this->pluginManager = \Drupal::service('plugin.manager.field.formatter');
43

44
    parent::__construct($values, $entity_type);
45 46 47
  }

  /**
48
   * {@inheritdoc}
49
   */
50
  public function getRenderer($field_name) {
51 52
    if (isset($this->plugins[$field_name])) {
      return $this->plugins[$field_name];
53 54 55
    }

    // Instantiate the formatter object from the stored display properties.
56
    if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) {
57
      $formatter = $this->pluginManager->getInstance(array(
58
        'field_definition' => $definition,
59
        'view_mode' => $this->originalMode,
60 61 62 63 64 65 66 67 68 69
        // No need to prepare, defaults have been merged in setComponent().
        'prepare' => FALSE,
        'configuration' => $configuration
      ));
    }
    else {
      $formatter = NULL;
    }

    // Persist the formatter object.
70
    $this->plugins[$field_name] = $formatter;
71 72 73 74
    return $formatter;
  }

}