comparison core/modules/field_layout/src/Entity/FieldLayoutEntityDisplayTrait.php @ 0:4c8ae668cc8c

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