Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / src / Controller / AddSectionController.php
diff --git a/web/core/modules/layout_builder/src/Controller/AddSectionController.php b/web/core/modules/layout_builder/src/Controller/AddSectionController.php
new file mode 100644 (file)
index 0000000..9478959
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+
+namespace Drupal\layout_builder\Controller;
+
+use Drupal\Core\DependencyInjection\ClassResolverInterface;
+use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
+use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
+use Drupal\layout_builder\Section;
+use Drupal\layout_builder\SectionStorageInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+
+/**
+ * Defines a controller to add a new section.
+ *
+ * @internal
+ */
+class AddSectionController implements ContainerInjectionInterface {
+
+  use AjaxHelperTrait;
+  use LayoutRebuildTrait;
+
+  /**
+   * The layout tempstore repository.
+   *
+   * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
+   */
+  protected $layoutTempstoreRepository;
+
+  /**
+   * AddSectionController constructor.
+   *
+   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
+   *   The layout tempstore repository.
+   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
+   *   The class resolver.
+   */
+  public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, ClassResolverInterface $class_resolver) {
+    $this->layoutTempstoreRepository = $layout_tempstore_repository;
+    $this->classResolver = $class_resolver;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container) {
+    return new static(
+      $container->get('layout_builder.tempstore_repository'),
+      $container->get('class_resolver')
+    );
+  }
+
+  /**
+   * Adds the new section.
+   *
+   * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
+   *   The section storage.
+   * @param int $delta
+   *   The delta of the section to splice.
+   * @param string $plugin_id
+   *   The plugin ID of the layout to add.
+   *
+   * @return \Symfony\Component\HttpFoundation\Response
+   *   The controller response.
+   */
+  public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {
+    $section_storage->insertSection($delta, new Section($plugin_id));
+
+    $this->layoutTempstoreRepository->set($section_storage);
+
+    if ($this->isAjax()) {
+      return $this->rebuildAndClose($section_storage);
+    }
+    else {
+      $url = $section_storage->getLayoutBuilderUrl();
+      return new RedirectResponse($url->setAbsolute()->toString());
+    }
+  }
+
+}