Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\field_layout\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
6 use Drupal\Core\Layout\LayoutInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides shared code for entity displays.
|
Chris@0
|
10 *
|
Chris@0
|
11 * Both EntityViewDisplay and EntityFormDisplay must maintain their parent
|
Chris@0
|
12 * hierarchy, while being identically enhanced by Field Layout. This trait
|
Chris@0
|
13 * contains the code they both share.
|
Chris@0
|
14 */
|
Chris@0
|
15 trait FieldLayoutEntityDisplayTrait {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Gets a layout definition.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @param string $layout_id
|
Chris@0
|
21 * The layout ID.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @return \Drupal\Core\Layout\LayoutDefinition
|
Chris@0
|
24 * The layout definition.
|
Chris@0
|
25 */
|
Chris@0
|
26 protected function getLayoutDefinition($layout_id) {
|
Chris@0
|
27 return \Drupal::service('plugin.manager.core.layout')->getDefinition($layout_id);
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::getLayoutId().
|
Chris@0
|
32 */
|
Chris@0
|
33 public function getLayoutId() {
|
Chris@0
|
34 return $this->getThirdPartySetting('field_layout', 'id');
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::getLayoutSettings().
|
Chris@0
|
39 */
|
Chris@0
|
40 public function getLayoutSettings() {
|
Chris@0
|
41 return $this->getThirdPartySetting('field_layout', 'settings', []);
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::setLayoutId().
|
Chris@0
|
46 */
|
Chris@0
|
47 public function setLayoutId($layout_id, array $layout_settings = []) {
|
Chris@0
|
48 if ($this->getLayoutId() !== $layout_id) {
|
Chris@0
|
49 // @todo Devise a mechanism for mapping old regions to new ones in
|
Chris@0
|
50 // https://www.drupal.org/node/2796877.
|
Chris@0
|
51 $layout_definition = $this->getLayoutDefinition($layout_id);
|
Chris@0
|
52 $new_region = $layout_definition->getDefaultRegion();
|
Chris@0
|
53 $layout_regions = $layout_definition->getRegions();
|
Chris@0
|
54 foreach ($this->getComponents() as $name => $component) {
|
Chris@0
|
55 if (isset($component['region']) && !isset($layout_regions[$component['region']])) {
|
Chris@0
|
56 $component['region'] = $new_region;
|
Chris@0
|
57 $this->setComponent($name, $component);
|
Chris@0
|
58 }
|
Chris@0
|
59 }
|
Chris@0
|
60 }
|
Chris@0
|
61 $this->setThirdPartySetting('field_layout', 'id', $layout_id);
|
Chris@0
|
62 // Instantiate the plugin and consult it for the updated plugin
|
Chris@0
|
63 // configuration. Once layouts are no longer stored as third party settings,
|
Chris@0
|
64 // this will be handled by the code in
|
Chris@0
|
65 // \Drupal\Core\Config\Entity\ConfigEntityBase::set() that handles
|
Chris@0
|
66 // \Drupal\Core\Entity\EntityWithPluginCollectionInterface.
|
Chris@0
|
67 $layout_settings = $this->doGetLayout($layout_id, $layout_settings)->getConfiguration();
|
Chris@0
|
68 $this->setThirdPartySetting('field_layout', 'settings', $layout_settings);
|
Chris@0
|
69 return $this;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::setLayout().
|
Chris@0
|
74 */
|
Chris@0
|
75 public function setLayout(LayoutInterface $layout) {
|
Chris@0
|
76 $this->setLayoutId($layout->getPluginId(), $layout->getConfiguration());
|
Chris@0
|
77 return $this;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Implements \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface::getLayout().
|
Chris@0
|
82 */
|
Chris@0
|
83 public function getLayout() {
|
Chris@0
|
84 return $this->doGetLayout($this->getLayoutId(), $this->getLayoutSettings());
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * Gets the layout plugin.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @param string $layout_id
|
Chris@0
|
91 * A layout plugin ID.
|
Chris@0
|
92 * @param array $layout_settings
|
Chris@0
|
93 * An array of settings.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return \Drupal\Core\Layout\LayoutInterface
|
Chris@0
|
96 * The layout plugin.
|
Chris@0
|
97 */
|
Chris@0
|
98 protected function doGetLayout($layout_id, array $layout_settings) {
|
Chris@0
|
99 return \Drupal::service('plugin.manager.core.layout')->createInstance($layout_id, $layout_settings);
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * Overrides \Drupal\Core\Entity\EntityDisplayBase::init().
|
Chris@0
|
104 */
|
Chris@0
|
105 protected function init() {
|
Chris@0
|
106 $this->ensureLayout();
|
Chris@0
|
107 parent::init();
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Overrides \Drupal\Core\Entity\EntityDisplayBase::preSave().
|
Chris@0
|
112 */
|
Chris@0
|
113 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
114 parent::preSave($storage);
|
Chris@0
|
115
|
Chris@0
|
116 // Ensure the plugin configuration is updated. Once layouts are no longer
|
Chris@0
|
117 // stored as third party settings, this will be handled by the code in
|
Chris@0
|
118 // \Drupal\Core\Config\Entity\ConfigEntityBase::preSave() that handles
|
Chris@0
|
119 // \Drupal\Core\Entity\EntityWithPluginCollectionInterface.
|
Chris@0
|
120 if ($this->getLayoutId()) {
|
Chris@0
|
121 $this->setLayout($this->getLayout());
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function ensureLayout($default_layout_id = 'layout_onecol') {
|
Chris@0
|
129 if (!$this->getLayoutId()) {
|
Chris@0
|
130 $this->setLayoutId($default_layout_id);
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 return $this;
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * Overrides \Drupal\Core\Entity\EntityDisplayBase::calculateDependencies().
|
Chris@0
|
138 *
|
Chris@0
|
139 * Ensure the plugin dependencies are included. Once layouts are no longer
|
Chris@0
|
140 * stored as third party settings, this will be handled by the code in
|
Chris@0
|
141 * \Drupal\Core\Config\Entity\ConfigEntityBase::calculateDependencies() that
|
Chris@0
|
142 * handles \Drupal\Core\Entity\EntityWithPluginCollectionInterface.
|
Chris@0
|
143 */
|
Chris@0
|
144 public function calculateDependencies() {
|
Chris@0
|
145 parent::calculateDependencies();
|
Chris@0
|
146
|
Chris@0
|
147 // This can be called during uninstallation, so check for a valid ID first.
|
Chris@0
|
148 if ($this->getLayoutId()) {
|
Chris@0
|
149 $this->calculatePluginDependencies($this->getLayout());
|
Chris@0
|
150 }
|
Chris@18
|
151 return $this;
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 }
|