You are here

public function PublishLatestRevisionAction::execute in moderated content bulk publish 2.0.x

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

Executes the plugin.

Overrides ExecutableInterface::execute

File

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

Class

PublishLatestRevisionAction

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('Publish bulk operation by moderated_content_bulk_publish module'));
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('moderated content bulk publish')) {
    \Drupal::logger('moderated_content_bulk_publish')
      ->notice("Executing publish latest revision of " . $entity
      ->label());
    $adminModeration = new AdminModeration($entity, \Drupal\node\NodeInterface::PUBLISHED);
    $entity = $adminModeration
      ->publish();

    // Check if published
    if (!$entity
      ->isPublished()) {
      $msg = "Something went wrong, the entity must be published by this point.  Review your content moderation configuration make sure you have archive state which sets current revision and a published 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;
  }
}