You are here

public function StateChange::execute in Workbench Moderation Actions 8

Executes the plugin.

Overrides ExecutableInterface::execute

File

src/Plugin/Action/StateChange.php, line 82

Class

StateChange
Publishes a media entity.

Namespace

Drupal\workbench_moderation_actions\Plugin\Action

Code

public function execute(ContentEntityInterface $entity = NULL) {
  if ($entity && !$this->moderationInfo
    ->isModeratableEntity($entity)) {
    $this->messenger
      ->addMessage($this
      ->t('One or more entities were skipped as they are under moderation and may not be directly published or unpublished.'));
    return;
  }

  /** @var \Drupal\Core\Entity\ContentEntityInterface $object */
  $original_entity = $entity;
  $entity = $this
    ->loadLatestRevision($entity);

  // Support the advanced_scheduler/scheduler modules.
  if ($original_entity->publish_on->value === NULL && !empty($entity->publish_on->value)) {
    $entity->publish_on->value = NULL;
  }
  $entity
    ->get('moderation_state')->target_id = $this->pluginDefinition['state'];
  $violations = $entity
    ->validate();
  if (($moderation_violations = $violations
    ->getByField('moderation_state')) && count($moderation_violations)) {

    /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
    foreach ($moderation_violations as $violation) {
      $this->messenger
        ->addError($violation
        ->getMessage());
    }
    return;
  }
  $entity
    ->isDefaultRevision(TRUE);
  $entity
    ->save();
}