3 namespace Drupal\field_layout\Form;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Form\SubformState;
7 use Drupal\Core\Plugin\PluginFormInterface;
8 use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
11 * Provides shared code for entity display forms.
13 * Both EntityViewDisplayEditForm and EntityFormDisplayEditForm must maintain
14 * their parent hierarchy, while being identically enhanced by Field Layout.
15 * This trait contains the code they both share.
17 trait FieldLayoutEntityDisplayFormTrait {
20 * The field layout plugin manager.
22 * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
24 protected $layoutPluginManager;
27 * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::getRegions().
29 public function getRegions() {
32 $layout_definition = $this->layoutPluginManager->getDefinition($this->getEntity()->getLayoutId());
33 foreach ($layout_definition->getRegions() as $name => $region) {
35 'title' => $region['label'],
36 'message' => $this->t('No field is displayed.'),
40 $regions['hidden'] = [
41 'title' => $this->t('Disabled', [], ['context' => 'Plural']),
42 'message' => $this->t('No field is hidden.'),
49 * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::form().
51 public function form(array $form, FormStateInterface $form_state) {
52 $form = parent::form($form, $form_state);
54 $form['field_layouts'] = [
56 '#title' => $this->t('Layout settings'),
59 $layout_plugin = $this->getLayout($this->getEntity(), $form_state);
61 $form['field_layouts']['field_layout'] = [
63 '#title' => $this->t('Select a layout'),
64 '#options' => $this->layoutPluginManager->getLayoutOptions(),
65 '#default_value' => $layout_plugin->getPluginId(),
67 'callback' => '::settingsAjax',
68 'wrapper' => 'field-layout-settings-wrapper',
69 'trigger_as' => ['name' => 'field_layout_change'],
72 $form['field_layouts']['submit'] = [
74 '#name' => 'field_layout_change',
75 '#value' => $this->t('Change layout'),
76 '#submit' => ['::settingsAjaxSubmit'],
77 '#attributes' => ['class' => ['js-hide']],
79 'callback' => '::settingsAjax',
80 'wrapper' => 'field-layout-settings-wrapper',
84 $form['field_layouts']['settings_wrapper'] = [
85 '#type' => 'container',
86 '#id' => 'field-layout-settings-wrapper',
90 if ($layout_plugin instanceof PluginFormInterface) {
91 $form['field_layouts']['settings_wrapper']['layout_settings'] = [];
92 $subform_state = SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state);
93 $form['field_layouts']['settings_wrapper']['layout_settings'] = $layout_plugin->buildConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], $subform_state);
100 * Gets the layout plugin for the currently selected field layout.
102 * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $entity
103 * The current form entity.
104 * @param \Drupal\Core\Form\FormStateInterface $form_state
105 * The current state of the form.
107 * @return \Drupal\Core\Layout\LayoutInterface
110 protected function getLayout(EntityDisplayWithLayoutInterface $entity, FormStateInterface $form_state) {
111 if (!$layout_plugin = $form_state->get('layout_plugin')) {
112 $stored_layout_id = $entity->getLayoutId();
113 // Use selected layout if it exists, falling back to the stored layout.
114 $layout_id = $form_state->getValue('field_layout', $stored_layout_id);
115 // If the current layout is the stored layout, use the stored layout
116 // settings. Otherwise leave the settings empty.
117 $layout_settings = $layout_id === $stored_layout_id ? $entity->getLayoutSettings() : [];
119 $layout_plugin = $this->layoutPluginManager->createInstance($layout_id, $layout_settings);
120 $form_state->set('layout_plugin', $layout_plugin);
122 return $layout_plugin;
126 * Ajax callback for the field layout settings form.
128 public static function settingsAjax($form, FormStateInterface $form_state) {
129 return $form['field_layouts']['settings_wrapper'];
133 * Submit handler for the non-JS case.
135 public function settingsAjaxSubmit($form, FormStateInterface $form_state) {
136 $form_state->set('layout_plugin', NULL);
137 $form_state->setRebuild();
141 * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::validateForm().
143 public function validateForm(array &$form, FormStateInterface $form_state) {
144 parent::validateForm($form, $form_state);
146 $layout_plugin = $this->getLayout($this->getEntity(), $form_state);
147 if ($layout_plugin instanceof PluginFormInterface) {
148 $subform_state = SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state);
149 $layout_plugin->validateConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], $subform_state);
154 * Overrides \Drupal\field_ui\Form\EntityDisplayFormBase::submitForm().
156 public function submitForm(array &$form, FormStateInterface $form_state) {
157 parent::submitForm($form, $form_state);
159 $entity = $this->getEntity();
160 $layout_plugin = $this->getLayout($entity, $form_state);
161 if ($layout_plugin instanceof PluginFormInterface) {
162 $subform_state = SubformState::createForSubform($form['field_layouts']['settings_wrapper']['layout_settings'], $form, $form_state);
163 $layout_plugin->submitConfigurationForm($form['field_layouts']['settings_wrapper']['layout_settings'], $subform_state);
166 $entity->setLayout($layout_plugin);
170 * Gets the form entity.
172 * @return \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface
173 * The current form entity.
175 abstract public function getEntity();