Same name and namespace in other branches
  1. 8.9.x core/modules/layout_builder/src/Form/DefaultsEntityForm.php \Drupal\layout_builder\Form\DefaultsEntityForm
  2. 9 core/modules/layout_builder/src/Form/DefaultsEntityForm.php \Drupal\layout_builder\Form\DefaultsEntityForm

Provides a form containing the Layout Builder UI for defaults.

@internal Form classes are internal.

Hierarchy

Expanded class hierarchy of DefaultsEntityForm

1 file declares its use of DefaultsEntityForm
layout_builder.module in core/modules/layout_builder/layout_builder.module
Provides hook implementations for Layout Builder.

File

core/modules/layout_builder/src/Form/DefaultsEntityForm.php, line 20

Namespace

Drupal\layout_builder\Form
View source
class DefaultsEntityForm extends EntityForm {
  use PreviewToggleTrait;
  use LayoutBuilderEntityFormTrait;

  /**
   * Layout tempstore repository.
   *
   * @var \Drupal\layout_builder\LayoutTempstoreRepositoryInterface
   */
  protected $layoutTempstoreRepository;

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The section storage.
   *
   * @var \Drupal\layout_builder\SectionStorageInterface
   */
  protected $sectionStorage;

  /**
   * Constructs a new DefaultsEntityForm.
   *
   * @param \Drupal\layout_builder\LayoutTempstoreRepositoryInterface $layout_tempstore_repository
   *   The layout tempstore repository.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info service.
   */
  public function __construct(LayoutTempstoreRepositoryInterface $layout_tempstore_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
    $this->layoutTempstoreRepository = $layout_tempstore_repository;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('layout_builder.tempstore_repository'), $container
      ->get('entity_type.bundle.info'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    $form['#attributes']['class'][] = 'layout-builder-form';
    $form['layout_builder'] = [
      '#type' => 'layout_builder',
      '#section_storage' => $section_storage,
      '#process' => [
        [
          static::class,
          'layoutBuilderElementGetKeys',
        ],
      ],
    ];
    $form['layout_builder_message'] = $this
      ->buildMessage($section_storage
      ->getContextValue('display'));
    $this->sectionStorage = $section_storage;
    return parent::buildForm($form, $form_state);
  }

  /**
   * Form element #process callback.
   *
   * Save the layout builder element array parents as a property on the top form
   * element so that they can be used to access the element within the whole
   * render array later.
   *
   * @see \Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController
   */
  public static function layoutBuilderElementGetKeys(array $element, FormStateInterface $form_state, &$form) {
    $form['#layout_builder_element_keys'] = $element['#array_parents'];
    return $element;
  }

  /**
   * Renders a message to display at the top of the layout builder.
   *
   * @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $entity
   *   The entity view display being edited.
   *
   * @return array
   *   A renderable array containing the message.
   */
  protected function buildMessage(LayoutEntityDisplayInterface $entity) {
    $entity_type_id = $entity
      ->getTargetEntityTypeId();
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $bundle_info = $this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id);
    $args = [
      '@bundle' => $bundle_info[$entity
        ->getTargetBundle()]['label'],
      '@plural_label' => $entity_type
        ->getPluralLabel(),
    ];
    if ($entity_type
      ->hasKey('bundle')) {
      $message = $this
        ->t('You are editing the layout template for all @bundle @plural_label.', $args);
    }
    else {
      $message = $this
        ->t('You are editing the layout template for all @plural_label.', $args);
    }
    return $this
      ->buildMessageContainer($message, 'defaults');
  }

  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, FormStateInterface $form_state) {

    // \Drupal\Core\Entity\EntityForm::buildEntity() clones the entity object.
    // Keep it in sync with the one used by the section storage.
    $this
      ->setEntity($this->sectionStorage
      ->getContextValue('display'));
    $entity = parent::buildEntity($form, $form_state);
    $this->sectionStorage
      ->setContextValue('display', $entity);
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    $route_parameters = $route_match
      ->getParameters()
      ->all();
    return $this->entityTypeManager
      ->getStorage('entity_view_display')
      ->load($route_parameters['entity_type_id'] . '.' . $route_parameters['bundle'] . '.' . $route_parameters['view_mode_name']);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    return $this
      ->buildActions($actions);
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $return = $this->sectionStorage
      ->save();
    $this
      ->saveTasks($form_state, $this
      ->t('The layout has been saved.'));
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultsEntityForm::$entityTypeBundleInfo protected property The entity type bundle info service.
DefaultsEntityForm::$layoutTempstoreRepository protected property Layout tempstore repository.
DefaultsEntityForm::$sectionStorage protected property The section storage.
DefaultsEntityForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
DefaultsEntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityForm::buildEntity
DefaultsEntityForm::buildForm public function Form constructor. Overrides EntityForm::buildForm
DefaultsEntityForm::buildMessage protected function Renders a message to display at the top of the layout builder.
DefaultsEntityForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
DefaultsEntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityForm::getEntityFromRouteMatch
DefaultsEntityForm::layoutBuilderElementGetKeys public static function Form element #process callback.
DefaultsEntityForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
DefaultsEntityForm::__construct public function Constructs a new DefaultsEntityForm.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 8
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service. 2
EntityForm::$operation protected property The name of the current operation.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties. 9
EntityForm::form public function Gets the actual form array to be built. 29
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 12
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 2
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 2
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form stateā€¦ Overrides FormInterface::submitForm 15
FormBase::$configFactory protected property The config factory. 2
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 2
FormBase::container private function Returns the service container.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 47
LayoutBuilderEntityFormTrait::buildActions protected function Builds the actions for the form.
LayoutBuilderEntityFormTrait::buildMessageContainer protected function Build the message container.
LayoutBuilderEntityFormTrait::getBaseFormId public function
LayoutBuilderEntityFormTrait::getSectionStorage public function Retrieves the section storage object.
LayoutBuilderEntityFormTrait::redirectOnSubmit public function Form submission handler.
LayoutBuilderEntityFormTrait::saveTasks protected function Performs tasks that are needed during the save process.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 8
MessengerTrait::messenger public function Gets the messenger. 8
MessengerTrait::setMessenger public function Sets the messenger.
PreviewToggleTrait::buildContentPreviewToggle protected function Builds the content preview toggle input.
PreviewToggleTrait::currentUser abstract protected function Gets the current user.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 1
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.