EntityDisplay.php 1.9 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\Annotation\EntityType;
11
use Drupal\Core\Annotation\Translation;
12
use Drupal\entity\EntityDisplayBase;
13
use Drupal\entity\EntityDisplayInterface;
14 15 16

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

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

42
    parent::__construct($values, $entity_type);
43 44 45
  }

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

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

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

}