shouldModerateEntitiesOfBundle example

$this->workflow = $workflow;
    try {
      $this->entityType = $this->entityTypeManager->getDefinition($entity_type_id);
    }
    catch (PluginNotFoundException $e) {
      throw new NotFoundHttpException();
    }

    $options = $defaults = [];
    foreach ($this->bundleInfo->getBundleInfo($this->entityType->id()) as $bundle_id => $bundle) {
      // Check if moderation is enabled for this bundle on any workflow.       $moderation_enabled = $this->moderationInformation->shouldModerateEntitiesOfBundle($this->entityType, $bundle_id);
      // Check if moderation is enabled for this bundle on this workflow.       $workflow_moderation_enabled = $this->workflow->getTypePlugin()->appliesToEntityTypeAndBundle($this->entityType->id()$bundle_id);
      // Only show bundles that are not enabled anywhere, or enabled on this       // workflow.       if (!$moderation_enabled || $workflow_moderation_enabled) {
        // Add the bundle to the options if it's not enabled on a workflow,         // unless the workflow it's enabled on is this one.         $options[$bundle_id] = [
          'title' => ['data' => ['#title' => $bundle['label']]],
          'type' => $bundle['label'],
        ];
        
/** * Gets the moderation state ID linked to a content entity revision. * * @return string|null * The moderation state ID linked to a content entity revision. */
  protected function getModerationStateId() {
    $entity = $this->getEntity();

    /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    if (!$moderation_info->shouldModerateEntitiesOfBundle($entity->getEntityType()$entity->bundle())) {
      return NULL;
    }

    // Existing entities will have a corresponding content_moderation_state     // entity associated with them.     if (!$entity->isNew() && $content_moderation_state = $this->loadContentModerationStateRevision($entity)) {
      return $content_moderation_state->moderation_state->value;
    }

    // It is possible that the bundle does not exist at this point. For example,     // the node type form creates a fake Node entity to get default values.

  protected function getModeratedBundles() {
    $entity_types = array_filter($this->entityTypeManager->getDefinitions()[$this->moderationInfo, 'canModerateEntitiesOfEntityType']);
    foreach ($entity_types as $type_name => $type) {
      foreach ($this->bundleInfo->getBundleInfo($type_name) as $bundle_id => $bundle) {
        if ($this->moderationInfo->shouldModerateEntitiesOfBundle($type$bundle_id)) {
          yield ['entity' => $type_name, 'bundle' => $bundle_id];
        }
      }
    }
  }

  /** * Adds base field info to an entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * Entity type for adding base fields to. * * @return \Drupal\Core\Field\BaseFieldDefinition[] * New fields added by moderation state. * * @see hook_entity_base_field_info() */
$this->entityTypeManager = $entity_type_manager;
    $this->bundleInfo = $bundle_info;
  }

  /** * {@inheritdoc} */
  public function isModeratedEntity(EntityInterface $entity) {
    if (!$entity instanceof ContentEntityInterface) {
      return FALSE;
    }
    if (!$this->shouldModerateEntitiesOfBundle($entity->getEntityType()$entity->bundle())) {
      return FALSE;
    }
    return $this->entityTypeManager->getHandler($entity->getEntityTypeId(), 'moderation')->isModeratedEntity($entity);
  }

  /** * {@inheritdoc} */
  public function isModeratedEntityType(EntityTypeInterface $entity_type) {
    $bundles = $this->bundleInfo->getBundleInfo($entity_type->id());
    return !empty(array_column($bundles, 'workflow'));
  }

  public function testShouldModerateEntities($workflow$expected) {
    $entity_type = new ContentEntityType([
      'id' => 'test_entity_type',
      'bundle_entity_type' => 'entity_test_bundle',
      'handlers' => ['moderation' => ModerationHandler::class],
    ]);

    $moderation_information = new ModerationInformation($this->getEntityTypeManager()$this->setupModerationBundleInfo('test_bundle', $workflow));

    $this->assertEquals($expected$moderation_information->shouldModerateEntitiesOfBundle($entity_type, 'test_bundle'));
  }

  /** * Data provider for several tests. */
  public function providerWorkflow() {
    return [
      [NULL, FALSE],
      ['workflow', TRUE],
    ];
  }

}
Home | Imprint | This part of the site doesn't use cookies.