comparison core/modules/layout_builder/src/Controller/LayoutBuilderController.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\layout_builder\Controller; 3 namespace Drupal\layout_builder\Controller;
4 4
5 use Drupal\Core\Ajax\AjaxHelperTrait;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; 6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Messenger\MessengerInterface; 7 use Drupal\Core\Messenger\MessengerInterface;
7 use Drupal\Core\Plugin\PluginFormInterface; 8 use Drupal\Core\Plugin\PluginFormInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait; 9 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\Url; 10 use Drupal\Core\Url;
22 */ 23 */
23 class LayoutBuilderController implements ContainerInjectionInterface { 24 class LayoutBuilderController implements ContainerInjectionInterface {
24 25
25 use LayoutBuilderContextTrait; 26 use LayoutBuilderContextTrait;
26 use StringTranslationTrait; 27 use StringTranslationTrait;
28 use AjaxHelperTrait;
27 29
28 /** 30 /**
29 * The layout tempstore repository. 31 * The layout tempstore repository.
30 * 32 *
31 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface 33 * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
88 */ 90 */
89 public function layout(SectionStorageInterface $section_storage, $is_rebuilding = FALSE) { 91 public function layout(SectionStorageInterface $section_storage, $is_rebuilding = FALSE) {
90 $this->prepareLayout($section_storage, $is_rebuilding); 92 $this->prepareLayout($section_storage, $is_rebuilding);
91 93
92 $output = []; 94 $output = [];
95 if ($this->isAjax()) {
96 $output['status_messages'] = [
97 '#type' => 'status_messages',
98 ];
99 }
93 $count = 0; 100 $count = 0;
94 for ($i = 0; $i < $section_storage->count(); $i++) { 101 for ($i = 0; $i < $section_storage->count(); $i++) {
95 $output[] = $this->buildAddSectionLink($section_storage, $count); 102 $output[] = $this->buildAddSectionLink($section_storage, $count);
96 $output[] = $this->buildAdministrativeSection($section_storage, $count); 103 $output[] = $this->buildAdministrativeSection($section_storage, $count);
97 $count++; 104 $count++;
112 * The section storage. 119 * The section storage.
113 * @param bool $is_rebuilding 120 * @param bool $is_rebuilding
114 * Indicates if the layout is rebuilding. 121 * Indicates if the layout is rebuilding.
115 */ 122 */
116 protected function prepareLayout(SectionStorageInterface $section_storage, $is_rebuilding) { 123 protected function prepareLayout(SectionStorageInterface $section_storage, $is_rebuilding) {
124 // If the layout has pending changes, add a warning.
125 if ($this->layoutTempstoreRepository->has($section_storage)) {
126 $this->messenger->addWarning($this->t('You have unsaved changes.'));
127 }
128
117 // Only add sections if the layout is new and empty. 129 // Only add sections if the layout is new and empty.
118 if (!$is_rebuilding && $section_storage->count() === 0) { 130 if (!$is_rebuilding && $section_storage->count() === 0) {
119 $sections = []; 131 $sections = [];
120 // If this is an empty override, copy the sections from the corresponding 132 // If this is an empty override, copy the sections from the corresponding
121 // default. 133 // default.
159 'section_storage' => $storage_id, 171 'section_storage' => $storage_id,
160 'delta' => $delta, 172 'delta' => $delta,
161 ], 173 ],
162 [ 174 [
163 'attributes' => [ 175 'attributes' => [
164 'class' => ['use-ajax', 'add-section__link'], 176 'class' => ['use-ajax', 'new-section__link'],
165 'data-dialog-type' => 'dialog', 177 'data-dialog-type' => 'dialog',
166 'data-dialog-renderer' => 'off_canvas', 178 'data-dialog-renderer' => 'off_canvas',
167 ], 179 ],
168 ] 180 ]
169 ), 181 ),
170 ], 182 ],
171 '#type' => 'container', 183 '#type' => 'container',
172 '#attributes' => [ 184 '#attributes' => [
173 'class' => ['add-section'], 185 'class' => ['new-section'],
174 ], 186 ],
175 ]; 187 ];
176 } 188 }
177 189
178 /** 190 /**
224 'delta' => $delta, 236 'delta' => $delta,
225 'region' => $region, 237 'region' => $region,
226 ], 238 ],
227 [ 239 [
228 'attributes' => [ 240 'attributes' => [
229 'class' => ['use-ajax', 'add-block__link'], 241 'class' => ['use-ajax', 'new-block__link'],
230 'data-dialog-type' => 'dialog', 242 'data-dialog-type' => 'dialog',
231 'data-dialog-renderer' => 'off_canvas', 243 'data-dialog-renderer' => 'off_canvas',
232 ], 244 ],
233 ] 245 ]
234 ), 246 ),
235 ]; 247 ];
236 $build[$region]['layout_builder_add_block']['#type'] = 'container'; 248 $build[$region]['layout_builder_add_block']['#type'] = 'container';
237 $build[$region]['layout_builder_add_block']['#attributes'] = ['class' => ['add-block']]; 249 $build[$region]['layout_builder_add_block']['#attributes'] = ['class' => ['new-block']];
238 $build[$region]['layout_builder_add_block']['#weight'] = 1000; 250 $build[$region]['layout_builder_add_block']['#weight'] = 1000;
239 $build[$region]['#attributes']['data-region'] = $region; 251 $build[$region]['#attributes']['data-region'] = $region;
240 $build[$region]['#attributes']['class'][] = 'layout-builder--layout__region'; 252 $build[$region]['#attributes']['class'][] = 'layout-builder--layout__region';
241 } 253 }
242 254
267 'data-dialog-renderer' => 'off_canvas', 279 'data-dialog-renderer' => 'off_canvas',
268 ], 280 ],
269 ], 281 ],
270 'remove' => [ 282 'remove' => [
271 '#type' => 'link', 283 '#type' => 'link',
272 '#title' => $this->t('Remove section'), 284 '#title' => $this->t('Remove section <span class="visually-hidden">@section</span>', ['@section' => $delta + 1]),
273 '#url' => Url::fromRoute('layout_builder.remove_section', [ 285 '#url' => Url::fromRoute('layout_builder.remove_section', [
274 'section_storage_type' => $storage_type, 286 'section_storage_type' => $storage_type,
275 'section_storage' => $storage_id, 287 'section_storage' => $storage_id,
276 'delta' => $delta, 288 'delta' => $delta,
277 ]), 289 ]),