Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityDisplayModeBase.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
7
8 /**
9  * Base class for config entity types with settings for form and view modes.
10  */
11 abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityDisplayModeInterface {
12
13   /**
14    * The ID of the form or view mode.
15    *
16    * @var string
17    */
18   protected $id;
19
20   /**
21    * The human-readable name of the form or view mode.
22    *
23    * @var string
24    */
25   protected $label;
26
27   /**
28    * The entity type this form or view mode is used for.
29    *
30    * This is not to be confused with EntityDisplayModeBase::$entityType which is
31    * inherited from Entity::$entityType.
32    *
33    * @var string
34    */
35   protected $targetEntityType;
36
37   /**
38    * Whether or not this form or view mode has custom settings by default.
39    *
40    * If FALSE, entities displayed in this mode will reuse the 'default' display
41    * settings by default (e.g. right after the module exposing the form or view
42    * mode is enabled), but administrators can later use the Field UI to apply
43    * custom display settings specific to the form or view mode.
44    *
45    * @var bool
46    */
47   protected $status = TRUE;
48
49   /**
50    * Whether or not the rendered output of this view mode is cached by default.
51    *
52    * @var bool
53    */
54   protected $cache = TRUE;
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
60     /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $a */
61     /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $b */
62     // Sort by the type of entity the view mode is used for.
63     $a_type = $a->getTargetType();
64     $b_type = $b->getTargetType();
65     $type_order = strnatcasecmp($a_type, $b_type);
66     return $type_order != 0 ? $type_order : parent::sort($a, $b);
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getTargetType() {
73     return $this->targetEntityType;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function setTargetType($target_entity_type) {
80     $this->targetEntityType = $target_entity_type;
81     return $this;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function calculateDependencies() {
88     parent::calculateDependencies();
89     $target_entity_type = \Drupal::entityManager()->getDefinition($this->targetEntityType);
90     $this->addDependency('module', $target_entity_type->getProvider());
91     return $this;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function preSave(EntityStorageInterface $storage) {
98     parent::preSave($storage);
99     \Drupal::entityManager()->clearCachedFieldDefinitions();
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public static function preDelete(EntityStorageInterface $storage, array $entities) {
106     parent::preDelete($storage, $entities);
107     \Drupal::entityManager()->clearCachedFieldDefinitions();
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   protected function urlRouteParameters($rel) {
114     $uri_route_parameters = parent::urlRouteParameters($rel);
115     if ($rel === 'add-form') {
116       $uri_route_parameters['entity_type_id'] = $this->getTargetType();
117     }
118
119     return $uri_route_parameters;
120   }
121
122 }