trait WorkspaceSafeFormTrait

Same name in this branch
  1. 10 core/lib/Drupal/Core/Entity/Form/WorkspaceSafeFormTrait.php \Drupal\Core\Entity\Form\WorkspaceSafeFormTrait
Same name and namespace in other branches
  1. 11.x core/modules/layout_builder/src/Form/WorkspaceSafeFormTrait.php \Drupal\layout_builder\Form\WorkspaceSafeFormTrait
  2. 11.x core/lib/Drupal/Core/Entity/Form/WorkspaceSafeFormTrait.php \Drupal\Core\Entity\Form\WorkspaceSafeFormTrait

Provides a trait that marks Layout Builder forms as workspace-safe.

Hierarchy

File

core/modules/layout_builder/src/Form/WorkspaceSafeFormTrait.php, line 14

Namespace

Drupal\layout_builder\Form
View source
trait WorkspaceSafeFormTrait {
  use EntityWorkspaceSafeFormTrait;
  
  /**
   * Determines whether the current form is safe to be submitted in a workspace.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return bool
   *   TRUE if the form is workspace-safe, FALSE otherwise.
   */
  public function isWorkspaceSafeForm(array $form, FormStateInterface $form_state) : bool {
    $section_storage = $this->sectionStorage ?: $this->getSectionStorageFromFormState($form_state);
    if ($section_storage) {
      $context_definitions = $section_storage->getContextDefinitions();
      if (!empty($context_definitions['entity'])) {
        /** @var \Drupal\Core\Entity\EntityInterface $entity */
        $entity = $section_storage->getContextValue('entity');
        return $this->isWorkspaceSafeEntity($entity);
      }
    }
    return FALSE;
  }
  
  /**
   * Retrieves the section storage from a form state object, if it exists.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return \Drupal\layout_builder\SectionStorageInterface|null
   *   The section storage or NULL if it doesn't exist.
   */
  protected function getSectionStorageFromFormState(FormStateInterface $form_state) : ?SectionStorageInterface {
    foreach ($form_state->getBuildInfo()['args'] as $argument) {
      if ($argument instanceof SectionStorageInterface) {
        return $argument;
      }
    }
    return NULL;
  }

}

Members

Title Sort descending Modifiers Object type Summary
WorkspaceSafeFormTrait::$workspaceInfo protected property The workspace information service.
WorkspaceSafeFormTrait::getSectionStorageFromFormState protected function Retrieves the section storage from a form state object, if it exists.
WorkspaceSafeFormTrait::getWorkspaceInfo protected function Retrieves the workspace information service.
WorkspaceSafeFormTrait::isWorkspaceSafeEntity protected function Determines whether an entity used in a form is workspace-safe.
WorkspaceSafeFormTrait::isWorkspaceSafeEntityType protected function Determines whether an entity type used in a form is workspace-safe.
WorkspaceSafeFormTrait::isWorkspaceSafeForm public function Determines whether the current form is safe to be submitted in a workspace.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.