You are here

public function UnpublishCurrentRevisionAction::execute in moderated content bulk publish 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Action/UnpublishCurrentRevisionAction.php \Drupal\moderated_content_bulk_publish\Plugin\Action\UnpublishCurrentRevisionAction::execute()
  2. 1.0.x src/Plugin/Action/UnpublishCurrentRevisionAction.php \Drupal\moderated_content_bulk_publish\Plugin\Action\UnpublishCurrentRevisionAction::execute()

Executes the plugin.

Overrides ExecutableInterface::execute

File

src/Plugin/Action/UnpublishCurrentRevisionAction.php, line 35

Class

UnpublishCurrentRevisionAction

Namespace

Drupal\moderated_content_bulk_publish\Plugin\Action

Code

public function execute($entity = NULL) {

  /*
   * All config resides in $this->configuration.
   * Passed view rows will be available in $this->context.
   * Data about the view used to select results and optionally
   * the batch context are available in $this->context or externally
   * through the public getContext() method.
   * The entire ViewExecutable object  with selected result
   * rows is available in $this->view or externally through
   * the public getView() method.
   */

  // Do some processing..
  // ...

  //\Drupal::Messenger()->addStatus(utf8_encode('Begin unpublish bulk operation by moderated_content_bulk_publish module plugin'));
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('moderated content bulk unpublish')) {
    \Drupal::logger('moderated_content_bulk_publish')
      ->notice("Executing unpublish latest revision of " . $entity
      ->label());
    $adminModeration = new AdminModeration($entity, \Drupal\node\NodeInterface::NOT_PUBLISHED);
    $entity = $adminModeration
      ->unpublish();

    //check if published
    if ($entity
      ->isPublished()) {
      $msg = "Something went wrong, the entity must be unpublished by this point.  Review your content moderation configuration make sure you have archive state which sets current revision and a draft state and try again.";
      \Drupal::Messenger()
        ->addError(utf8_encode($msg));
      \Drupal::logger('moderated_content_bulk_publish')
        ->warning($msg);
      return $msg;
    }
    return sprintf('Example action (configuration: %s)', print_r($this->configuration, TRUE));
  }
  else {
    \Drupal::messenger()
      ->addWarning(t("You don't have access to execute this operation!"));
    return;
  }
}