class WorkspaceInformation

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

General service for workspace support information.

Hierarchy

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(protected readonly EntityTypeManagerInterface $entityTypeManager, protected readonly 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 Overriden Title
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 Returns an array of entity types that can belong to workspaces. Overrides WorkspaceInformationInterface::getSupportedEntityTypes
WorkspaceInformation::isEntityDeletable public function Determines whether an entity can be deleted in the given workspace. Overrides WorkspaceInformationInterface::isEntityDeletable
WorkspaceInformation::isEntityIgnored public function Determines whether CRUD operations for an entity are allowed. Overrides WorkspaceInformationInterface::isEntityIgnored
WorkspaceInformation::isEntitySupported public function Determines whether an entity can belong to a workspace. Overrides WorkspaceInformationInterface::isEntitySupported
WorkspaceInformation::isEntityTypeIgnored public function Determines whether CRUD operations for an entity type are allowed. Overrides WorkspaceInformationInterface::isEntityTypeIgnored
WorkspaceInformation::isEntityTypeSupported public function Determines whether an entity type can belong to a workspace. Overrides WorkspaceInformationInterface::isEntityTypeSupported
WorkspaceInformation::__construct public function

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