isEntityTypeSupported example

parent::prepare();

    // Do not alter entity revision queries.     // @todo How about queries for the latest revision? Should we alter them to     // look for the latest workspace-specific revision?     if ($this->allRevisions) {
      return $this;
    }

    // Only alter the query if the active workspace is not the default one and     // the entity type is supported.     if ($this->workspaceManager->hasActiveWorkspace() && $this->workspaceManager->isEntityTypeSupported($this->entityType)) {
      $active_workspace = $this->workspaceManager->getActiveWorkspace();
      $this->sqlQuery->addMetaData('active_workspace_id', $active_workspace->id());
      $this->sqlQuery->addMetaData('simple_query', FALSE);

      // LEFT JOIN 'workspace_association' to the base table of the query so we       // can properly include live content along with a possible workspace       // revision.       $id_field = $this->entityType->getKey('id');
      $this->sqlQuery->leftJoin('workspace_association', 'workspace_association', "[%alias].[target_entity_type_id] = '{$this->entityTypeId}' AND [%alias].[target_entity_id] = [base_table].[$id_field] AND [%alias].[workspace] = '{$active_workspace->id()}'");
    }

    

  public function validate($value, Constraint $constraint) {
    // The validator should run only if we are in a active workspace context.     if (!$this->workspaceManager->hasActiveWorkspace()) {
      return;
    }

    $target_entity_type_id = $value->getFieldDefinition()->getFieldStorageDefinition()->getSetting('target_type');
    $target_entity_type = $this->entityTypeManager->getDefinition($target_entity_type_id);

    if ($value->hasNewEntity() && !$this->workspaceManager->isEntityTypeSupported($target_entity_type)) {
      $this->context->addViolation($constraint->message, ['%collection_label' => $target_entity_type->getCollectionLabel()]);
    }
  }

}
/** * Adds the "EntityWorkspaceConflict" constraint to eligible entity types. * * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types * An associative array of all entity type definitions, keyed by the entity * type name. Passed by reference. * * @see hook_entity_type_build() */
  public function entityTypeBuild(array &$entity_types) {
    foreach ($entity_types as $entity_type) {
      if ($this->workspaceManager->isEntityTypeSupported($entity_type)) {
        $entity_type->addConstraint('EntityWorkspaceConflict');
        $entity_type->setRevisionMetadataKey('workspace', 'workspace');
      }
    }
  }

  /** * Removes the 'latest-version' link template provided by Content Moderation. * * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types * An array of entity types. * * @see hook_entity_type_alter() */
$entity_type = $entity->getEntityType();

    // Only run if we are not dealing with an entity type provided by the     // Workspaces module, an internal entity type or if we are in a non-default     // workspace.     if ($this->shouldSkipPreOperations($entity_type)) {
      return;
    }

    // Disallow any change to an unsupported entity when we are not in the     // default workspace.     if (!$this->workspaceManager->isEntityTypeSupported($entity_type)) {
      throw new \RuntimeException('This entity can only be saved in the default workspace.');
    }

    /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
    if (!$entity->isNew() && !$entity->isSyncing()) {
      // Force a new revision if the entity is not replicating.       $entity->setNewRevision(TRUE);

      // All entities in the non-default workspace are pending revisions,       // regardless of their publishing status. This means that when creating       // a published pending revision in a non-default workspace it will also be
$table_data = $this->viewsData->get($info['base']);
      if (empty($table_data['table']['entity type'])) {
        continue;
      }
      $entity_type_id = $table_data['table']['entity type'];
      // This construct ensures each entity type exists only once.       $entity_type_ids[$entity_type_id] = $entity_type_id;
    }

    $entity_type_definitions = $this->entityTypeManager->getDefinitions();
    foreach ($entity_type_ids as $entity_type_id) {
      if ($this->workspaceManager->isEntityTypeSupported($entity_type_definitions[$entity_type_id])) {
        $this->alterQueryForEntityType($query$entity_type_definitions[$entity_type_id]);
      }
    }
  }

  /** * Alters the entity type tables for a Views query. * * This should only be called after determining that this entity type is * involved in the query, and that a non-default workspace is in use. * * @param \Drupal\views\Plugin\views\query\Sql $query * The query plugin object for the query. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type definition. */

  public function entityOperationAccess(EntityInterface $entity$operation, AccountInterface $account) {
    // Workspaces themselves are handled by their own access handler and we     // should not try to do any access checks for entity types that can not     // belong to a workspace.     if ($entity->getEntityTypeId() === 'workspace' || !$this->workspaceManager->isEntityTypeSupported($entity->getEntityType()) || !$this->workspaceManager->hasActiveWorkspace()) {
      return AccessResult::neutral();
    }

    return $this->bypassAccessResult($account);
  }

  /** * Implements a hook bridge for hook_entity_create_access(). * * @param \Drupal\Core\Session\AccountInterface $account * The user account making the to check access for. * @param array $context * The context of the access check. * @param string $entity_bundle * The bundle of the entity. * * @return \Drupal\Core\Access\AccessResult * The result of the access check. * * @see hook_entity_create_access() */
return parent::addJoin($type$table$join_condition$langcode$delta);
  }

  /** * {@inheritdoc} */
  protected function addNextBaseTable(EntityType $entity_type$table$sql_column, FieldStorageDefinitionInterface $field_storage) {
    $next_base_table_alias = parent::addNextBaseTable($entity_type$table$sql_column$field_storage);

    $active_workspace_id = $this->sqlQuery->getMetaData('active_workspace_id');
    if ($active_workspace_id && $this->workspaceManager->isEntityTypeSupported($entity_type)) {
      $this->addWorkspaceAssociationJoin($entity_type->id()$next_base_table_alias$active_workspace_id);
    }

    return $next_base_table_alias;
  }

  /** * Adds a new join to the 'workspace_association' table for an entity base table. * * This method assumes that the active workspace has already been determined * to be a non-default workspace. * * @param string $entity_type_id * The ID of the entity type whose base table we are joining. * @param string $base_table_alias * The alias of the entity type's base table. * @param string $active_workspace_id * The ID of the active workspace. * * @return string * The alias of the joined table. */

  public static function getSubscribedEvents(): array {
    return static::getEntityTypeEvents();
  }

  /** * {@inheritdoc} */
  public function onEntityTypeCreate(EntityTypeInterface $entity_type) {
    // If the entity type is supported by Workspaces, add the revision metadata     // field.     if ($this->workspaceManager->isEntityTypeSupported($entity_type)) {
      $this->addRevisionMetadataField($entity_type);
    }
  }

  /** * {@inheritdoc} */
  public function onFieldableEntityTypeCreate(EntityTypeInterface $entity_type, array $field_storage_definitions) {
    $this->onEntityTypeCreate($entity_type);
  }

  
      $this->supported[$entity_type_id] = $entity_type->entityClassImplements(EntityPublishedInterface::class) && $entity_type->isRevisionable();
    }
    return $this->supported[$entity_type_id];
  }

  /** * {@inheritdoc} */
  public function getSupportedEntityTypes() {
    $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 hasActiveWorkspace() {
    return $this->getActiveWorkspace() !== FALSE;
  }
Home | Imprint | This part of the site doesn't use cookies.