trait WorkspaceSafeFormTrait

Same name in this branch
  1. 11.x core/modules/layout_builder/src/Form/WorkspaceSafeFormTrait.php \Drupal\layout_builder\Form\WorkspaceSafeFormTrait

Provides helpers for checking whether objects in forms are workspace-safe.

Hierarchy

2 files declare their use of WorkspaceSafeFormTrait
BulkForm.php in core/modules/views/src/Plugin/views/field/BulkForm.php
WorkspaceSafeFormTrait.php in core/modules/layout_builder/src/Form/WorkspaceSafeFormTrait.php

File

core/lib/Drupal/Core/Entity/Form/WorkspaceSafeFormTrait.php, line 12

Namespace

Drupal\Core\Entity\Form
View source
trait WorkspaceSafeFormTrait {
    
    /**
     * The workspace information service.
     */
    protected ?WorkspaceInformationInterface $workspaceInfo = NULL;
    
    /**
     * Determines whether an entity used in a form is workspace-safe.
     *
     * @param \Drupal\Core\Entity\EntityInterface $entity
     *   An entity object.
     *
     * @return bool
     *   TRUE if the entity is workspace-safe, FALSE otherwise.
     */
    protected function isWorkspaceSafeEntity(EntityInterface $entity) : bool {
        if (!\Drupal::hasService('workspaces.information')) {
            return FALSE;
        }
        $is_supported = $this->getWorkspaceInfo()
            ->isEntitySupported($entity);
        $is_ignored = $this->getWorkspaceInfo()
            ->isEntityIgnored($entity);
        return $is_supported || $is_ignored;
    }
    
    /**
     * Determines whether an entity type used in a form is workspace-safe.
     *
     * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
     *   An entity type object.
     *
     * @return bool
     *   TRUE if the entity type is workspace-safe, FALSE otherwise.
     */
    protected function isWorkspaceSafeEntityType(EntityTypeInterface $entity_type) : bool {
        if (!\Drupal::hasService('workspaces.information')) {
            return FALSE;
        }
        $is_supported = $this->getWorkspaceInfo()
            ->isEntityTypeSupported($entity_type);
        $is_ignored = $this->getWorkspaceInfo()
            ->isEntityTypeIgnored($entity_type);
        return $is_supported || $is_ignored;
    }
    
    /**
     * Retrieves the workspace information service.
     *
     * @return \Drupal\workspaces\WorkspaceInformationInterface
     *   The workspace information service.
     */
    protected function getWorkspaceInfo() : WorkspaceInformationInterface {
        if (!$this->workspaceInfo) {
            $this->workspaceInfo = \Drupal::service('workspaces.information');
        }
        return $this->workspaceInfo;
    }

}

Members

Title Sort descending Modifiers Object type Summary
WorkspaceSafeFormTrait::$workspaceInfo protected property The workspace information service.
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.

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