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