class WorkspaceInformation

Same name and namespace in other branches
  1. 10 core/modules/workspaces/src/WorkspaceInformation.php \Drupal\workspaces\WorkspaceInformation

General service for workspace support information.

Hierarchy

  • class \Drupal\workspaces\WorkspaceInformation implements \Drupal\workspaces\WorkspaceInformationInterface

Expanded class hierarchy of WorkspaceInformation

1 string reference to 'WorkspaceInformation'
workspaces.services.yml in core/modules/workspaces/workspaces.services.yml
core/modules/workspaces/workspaces.services.yml
1 service uses WorkspaceInformation
workspaces.information in core/modules/workspaces/workspaces.services.yml
Drupal\workspaces\WorkspaceInformation

File

core/modules/workspaces/src/WorkspaceInformation.php, line 14

Namespace

Drupal\workspaces
View source
class WorkspaceInformation implements WorkspaceInformationInterface {
    
    /**
     * An array of workspace-support statuses, keyed by entity type ID.
     *
     * @var bool[]
     */
    protected array $supported = [];
    
    /**
     * An array of workspace-ignored statuses, keyed by entity type ID.
     *
     * @var bool[]
     */
    protected array $ignored = [];
    public function __construct(EntityTypeManagerInterface $entityTypeManager, WorkspaceAssociationInterface $workspaceAssociation) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function isEntitySupported(EntityInterface $entity) : bool {
        $entity_type = $entity->getEntityType();
        if (!$this->isEntityTypeSupported($entity_type)) {
            return FALSE;
        }
        $handler = $this->entityTypeManager
            ->getHandler($entity_type->id(), 'workspace');
        return $handler->isEntitySupported($entity);
    }
    
    /**
     * {@inheritdoc}
     */
    public function isEntityTypeSupported(EntityTypeInterface $entity_type) : bool {
        if (!isset($this->supported[$entity_type->id()])) {
            if ($entity_type->hasHandlerClass('workspace')) {
                $supported = !is_a($entity_type->getHandlerClass('workspace'), IgnoredWorkspaceHandler::class, TRUE);
            }
            else {
                // Fallback for cases when entity type info hasn't been altered yet, for
                // example when the Workspaces module is being installed.
                $supported = $entity_type->entityClassImplements(EntityPublishedInterface::class) && $entity_type->isRevisionable();
            }
            $this->supported[$entity_type->id()] = $supported;
        }
        return $this->supported[$entity_type->id()];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSupportedEntityTypes() : array {
        $entity_types = [];
        foreach ($this->entityTypeManager
            ->getDefinitions() as $entity_type_id => $entity_type) {
            if ($this->isEntityTypeSupported($entity_type)) {
                $entity_types[$entity_type_id] = $entity_type;
            }
        }
        return $entity_types;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isEntityIgnored(EntityInterface $entity) : bool {
        $entity_type = $entity->getEntityType();
        if ($this->isEntityTypeIgnored($entity_type)) {
            return TRUE;
        }
        if ($entity_type->hasHandlerClass('workspace')) {
            $handler = $this->entityTypeManager
                ->getHandler($entity_type->id(), 'workspace');
            return !$handler->isEntitySupported($entity);
        }
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isEntityTypeIgnored(EntityTypeInterface $entity_type) : bool {
        if (!isset($this->ignored[$entity_type->id()])) {
            $this->ignored[$entity_type->id()] = $entity_type->hasHandlerClass('workspace') && is_a($entity_type->getHandlerClass('workspace'), IgnoredWorkspaceHandler::class, TRUE);
        }
        return $this->ignored[$entity_type->id()];
    }
    
    /**
     * {@inheritdoc}
     */
    public function isEntityDeletable(EntityInterface $entity, WorkspaceInterface $workspace) : bool {
        $initial_revisions = $this->workspaceAssociation
            ->getAssociatedInitialRevisions($workspace->id(), $entity->getEntityTypeId());
        return in_array($entity->id(), $initial_revisions, TRUE);
    }

}

Members

Title Sort descending Modifiers Object type Summary
WorkspaceInformation::$ignored protected property An array of workspace-ignored statuses, keyed by entity type ID.
WorkspaceInformation::$supported protected property An array of workspace-support statuses, keyed by entity type ID.
WorkspaceInformation::getSupportedEntityTypes public function
WorkspaceInformation::isEntityDeletable public function
WorkspaceInformation::isEntityIgnored public function
WorkspaceInformation::isEntitySupported public function
WorkspaceInformation::isEntityTypeIgnored public function
WorkspaceInformation::isEntityTypeSupported public function
WorkspaceInformation::__construct public function

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