Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / src / Form / ConfigureSectionForm.php
1 <?php
2
3 namespace Drupal\layout_builder\Form;
4
5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Form\SubformState;
9 use Drupal\Core\Layout\LayoutInterface;
10 use Drupal\Core\Plugin\PluginFormFactoryInterface;
11 use Drupal\Core\Plugin\PluginFormInterface;
12 use Drupal\Core\Plugin\PluginWithFormsInterface;
13 use Drupal\layout_builder\Controller\LayoutRebuildTrait;
14 use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
15 use Drupal\layout_builder\Section;
16 use Drupal\layout_builder\SectionStorageInterface;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18
19 /**
20  * Provides a form for configuring a layout section.
21  *
22  * @internal
23  */
24 class ConfigureSectionForm extends FormBase {
25
26   use AjaxFormHelperTrait;
27   use LayoutRebuildTrait;
28
29   /**
30    * The layout tempstore repository.
31    *
32    * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
33    */
34   protected $layoutTempstoreRepository;
35
36   /**
37    * The plugin being configured.
38    *
39    * @var \Drupal\Core\Layout\LayoutInterface|\Drupal\Core\Plugin\PluginFormInterface
40    */
41   protected $layout;
42
43   /**
44    * The plugin form manager.
45    *
46    * @var \Drupal\Core\Plugin\PluginFormFactoryInterface
47    */
48   protected $pluginFormFactory;
49
50   /**
51    * The section storage.
52    *
53    * @var \Drupal\layout_builder\SectionStorageInterface
54    */
55   protected $sectionStorage;
56
57   /**
58    * The field delta.
59    *
60    * @var int
61    */
62   protected $delta;
63
64   /**
65    * Indicates whether the section is being added or updated.
66    *
67    * @var bool
68    */
69   protected $isUpdate;
70
71   /**
72    * Constructs a new ConfigureSectionForm.
73    *
74    * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
75    *   The layout tempstore repository.
76    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
77    *   The class resolver.
78    * @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
79    *   The plugin form manager.
80    */
81   public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver, PluginFormFactoryInterface $plugin_form_manager) {
82     $this->layoutTempstoreRepository = $layout_tempstore_repository;
83     $this->classResolver = $class_resolver;
84     $this->pluginFormFactory = $plugin_form_manager;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public static function create(ContainerInterface $container) {
91     return new static(
92       $container->get('layout_builder.tempstore_repository'),
93       $container->get('class_resolver'),
94       $container->get('plugin_form.factory')
95     );
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function getFormId() {
102     return 'layout_builder_configure_section';
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $plugin_id = NULL) {
109     $this->sectionStorage = $section_storage;
110     $this->delta = $delta;
111     $this->isUpdate = is_null($plugin_id);
112
113     if ($this->isUpdate) {
114       $section = $this->sectionStorage->getSection($this->delta);
115     }
116     else {
117       $section = new Section($plugin_id);
118     }
119     $this->layout = $section->getLayout();
120
121     $form['#tree'] = TRUE;
122     $form['layout_settings'] = [];
123     $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
124     $form['layout_settings'] = $this->getPluginForm($this->layout)->buildConfigurationForm($form['layout_settings'], $subform_state);
125
126     $form['actions']['submit'] = [
127       '#type' => 'submit',
128       '#value' => $this->isUpdate ? $this->t('Update') : $this->t('Add section'),
129       '#button_type' => 'primary',
130     ];
131     if ($this->isAjax()) {
132       $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
133     }
134
135     return $form;
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function validateForm(array &$form, FormStateInterface $form_state) {
142     $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
143     $this->getPluginForm($this->layout)->validateConfigurationForm($form['layout_settings'], $subform_state);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function submitForm(array &$form, FormStateInterface $form_state) {
150     // Call the plugin submit handler.
151     $subform_state = SubformState::createForSubform($form['layout_settings'], $form, $form_state);
152     $this->getPluginForm($this->layout)->submitConfigurationForm($form['layout_settings'], $subform_state);
153
154     $plugin_id = $this->layout->getPluginId();
155     $configuration = $this->layout->getConfiguration();
156
157     if ($this->isUpdate) {
158       $this->sectionStorage->getSection($this->delta)->setLayoutSettings($configuration);
159     }
160     else {
161       $this->sectionStorage->insertSection($this->delta, new Section($plugin_id, $configuration));
162     }
163
164     $this->layoutTempstoreRepository->set($this->sectionStorage);
165     $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl());
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
172     return $this->rebuildAndClose($this->sectionStorage);
173   }
174
175   /**
176    * Retrieves the plugin form for a given layout.
177    *
178    * @param \Drupal\Core\Layout\LayoutInterface $layout
179    *   The layout plugin.
180    *
181    * @return \Drupal\Core\Plugin\PluginFormInterface
182    *   The plugin form for the layout.
183    */
184   protected function getPluginForm(LayoutInterface $layout) {
185     if ($layout instanceof PluginWithFormsInterface) {
186       return $this->pluginFormFactory->createInstance($layout, 'configure');
187     }
188
189     if ($layout instanceof PluginFormInterface) {
190       return $layout;
191     }
192
193     throw new \InvalidArgumentException(sprintf('The "%s" layout does not provide a configuration form', $layout->getPluginId()));
194   }
195
196 }