LayoutBuilderHtmlEntityFormController.php 2.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace Drupal\layout_builder\Controller;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Controller\FormController;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Overrides the entity form controller service for layout builder operations.
 */
14
class LayoutBuilderHtmlEntityFormController extends FormController {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

  use DependencySerializationTrait;

  /**
   * The entity form controller being decorated.
   *
   * @var \Drupal\Core\Controller\FormController
   */
  protected $entityFormController;

  /**
   * Constructs a LayoutBuilderHtmlEntityFormController object.
   *
   * @param \Drupal\Core\Controller\FormController $entity_form_controller
   *   The entity form controller being decorated.
   */
  public function __construct(FormController $entity_form_controller) {
    $this->entityFormController = $entity_form_controller;
  }

  /**
   * {@inheritdoc}
   */
  public function getContentResult(Request $request, RouteMatchInterface $route_match) {
    $form = $this->entityFormController->getContentResult($request, $route_match);

    // If the form render element has a #layout_builder_element_keys property,
    // first set the form element as a child of the root render array. Use the
    // keys to get the layout builder element from the form render array and
    // copy it to a separate child element of the root element to prevent any
    // forms within the layout builder element from being nested.
    if (isset($form['#layout_builder_element_keys'])) {
      $build['form'] = &$form;
      $layout_builder_element = &NestedArray::getValue($form, $form['#layout_builder_element_keys']);
      $build['layout_builder'] = $layout_builder_element;
      // Remove the layout builder element within the form.
      $layout_builder_element = [];
      return $build;
    }

    // If no #layout_builder_element_keys property, return form as is.
    return $form;
  }

59 60 61 62 63 64 65 66 67 68 69 70 71 72
  /**
   * {@inheritdoc}
   */
  protected function getFormArgument(RouteMatchInterface $route_match) {
    return $this->entityFormController->getFormArgument($route_match);
  }

  /**
   * {@inheritdoc}
   */
  protected function getFormObject(RouteMatchInterface $route_match, $form_arg) {
    return $this->entityFormController->getFormObject($route_match, $form_arg);
  }

73
}