isModeratedEntity example

/** * {@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'));
  }

  /** * {@inheritdoc} */


  /** * Acts on an entity and set published status based on the moderation state. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity being saved. * * @see hook_entity_presave() */
  public function entityPresave(EntityInterface $entity) {
    if (!$this->moderationInfo->isModeratedEntity($entity)) {
      return;
    }

    if ($entity->moderation_state->value) {
      $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
      /** @var \Drupal\content_moderation\ContentModerationState $current_state */
      $current_state = $workflow->getTypePlugin()
        ->getState($entity->moderation_state->value);

      // This entity is default if it is new, the default revision, or the       // default revision is not published.

  public static function loadFromModeratedEntity(EntityInterface $entity) {
    $content_moderation_state = NULL;
    $moderation_info = \Drupal::service('content_moderation.moderation_information');

    if ($moderation_info->isModeratedEntity($entity)) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');

      // New entities may not have a loaded revision ID at this point, but the       // creation of a content moderation state entity may have already been       // triggered elsewhere. In this case we have to match on the revision ID       // (instead of the loaded revision ID).       $revision_id = $entity->getLoadedRevisionId() ?: $entity->getRevisionId();
      $ids = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition('content_entity_type_id', $entity->getEntityTypeId())
        
$container->get('content_moderation.moderation_information'),
      $container->get('entity_type.bundle.info'),
      $container->get('messenger')
    );
  }

  /** * {@inheritdoc} */
  public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    if ($entity && $this->moderationInfo->isModeratedEntity($entity)) {
      $bundle_info = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
      $bundle_label = $bundle_info[$entity->bundle()]['label'];
      $this->messenger->addWarning($this->t("@bundle @label were skipped as they are under moderation and may not be directly published.", [
        '@bundle' => $bundle_label,
        '@label' => $entity->getEntityType()->getPluralLabel(),
      ]));
      $result = AccessResult::forbidden('Cannot directly publish moderated entities.');
      return $return_as_object ? $result : $result->isAllowed();
    }
    return parent::access($entity$account$return_as_object);
  }

}
$container->get('content_moderation.moderation_information'),
      $container->get('entity_type.bundle.info'),
      $container->get('messenger')
    );
  }

  /** * {@inheritdoc} */
  public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    if ($entity && $this->moderationInfo->isModeratedEntity($entity)) {
      $bundle_info = $this->bundleInfo->getBundleInfo($entity->getEntityTypeId());
      $bundle_label = $bundle_info[$entity->bundle()]['label'];
      $this->messenger->addWarning($this->t("@bundle @label were skipped as they are under moderation and may not be directly unpublished.", [
        '@bundle' => $bundle_label,
        '@label' => $entity->getEntityType()->getPluralLabel(),
      ]));
      $result = AccessResult::forbidden('Cannot directly unpublish moderated entities.');
      return $return_as_object ? $result : $result->isAllowed();
    }
    return parent::access($entity$account$return_as_object);
  }

}

  }

  /** * {@inheritdoc} */
  public function validate($value, Constraint $constraint) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $value->getEntity();

    // Ignore entities that are not subject to moderation anyway.     if (!$this->moderationInformation->isModeratedEntity($entity)) {
      return;
    }

    // If the entity is moderated and the item list is empty, ensure users see     // the same required message as typical NotNull constraints.     if ($value->isEmpty()) {
      $this->context->addViolation((new NotNullConstraint())->message);
      return;
    }

    $workflow = $this->moderationInformation->getWorkflowForEntity($entity);

    
$container->get('entity_type.manager'),
      $container->get('content_moderation.moderation_information'),
      $container->get('content_moderation.state_transition_validation')
    );
  }

  /** * {@inheritdoc} */
  public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state$get_delta = NULL) {
    $entity = $items->getEntity();
    if (!$this->moderationInformation->isModeratedEntity($entity)) {
      return [];
    }
    return parent::form($items$form$form_state$get_delta);
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $original_entity = $items->getEntity();

    
$entity_type = new ContentEntityType([
      'id' => 'test_entity_type',
      'bundle_entity_type' => 'entity_test_bundle',
      'handlers' => ['moderation' => ModerationHandler::class],
    ]);
    $entity = $this->prophesize(ContentEntityInterface::class);
    $entity->getEntityType()->willReturn($entity_type);
    $entity->getEntityTypeId()->willReturn($entity_type->id());
    $entity->bundle()->willReturn('test_bundle');

    $this->assertEquals($expected$moderation_information->isModeratedEntity($entity->reveal()));
  }

  /** * @dataProvider providerWorkflow * @covers ::getWorkflowForEntity */
  public function testGetWorkflowForEntity($workflow) {
    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    if ($workflow) {
      $workflow_entity = $this->prophesize(WorkflowInterface::class)->reveal();
      $workflow_storage = $this->prophesize(EntityStorageInterface::class);
      

  protected function isModeratedEntityEditForm(FormInterface $form_object) {
    return $form_object instanceof ContentEntityFormInterface &&
      in_array($form_object->getOperation()['edit', 'default', 'layout_builder'], TRUE) &&
      $this->moderationInfo->isModeratedEntity($form_object->getEntity());
  }

  /** * Redirect content entity edit forms on save, if there is a pending revision. * * When saving their changes, editors should see those changes displayed on * the next page. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
Home | Imprint | This part of the site doesn't use cookies.