Commit 03b3bfc7 authored by Crell's avatar Crell

Add alternate moderation-aware action implementations.

parent 3803b758
<?php
namespace Drupal\workbench_moderation\Plugin\Action;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\node\Plugin\Action\PublishNode;
use Drupal\workbench_moderation\ModerationInformationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Alternate action plugin that knows to opt-out of modifying moderated entites.
*
* @see PublishNode
*/
class ModerationOptOutPublishNode extends PublishNode implements ContainerFactoryPluginInterface {
/**
* @var \Drupal\workbench_moderation\ModerationInformationInterface
*/
protected $moderationInfo;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModerationInformationInterface $mod_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moderationInfo = $mod_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration, $plugin_id, $plugin_definition,
$container->get('workbench_moderation.moderation_information')
);
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if ($entity && $this->moderationInfo->isModeratableEntity($entity)) {
drupal_set_message($this->t('One or more entities were skipped as they are under moderation and may not be directly published or unpublished.'));
return;
}
parent::execute($entity);
}
}
<?php
namespace Drupal\workbench_moderation\Plugin\Action;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\node\Plugin\Action\UnpublishNode;
use Drupal\workbench_moderation\ModerationInformationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Alternate action plugin that knows to opt-out of modifying moderated entites.
*
* @see UnpublishNode
*/
class ModerationOptOutUnpublishNode extends UnpublishNode implements ContainerFactoryPluginInterface {
/**
* @var \Drupal\workbench_moderation\ModerationInformationInterface
*/
protected $moderationInfo;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModerationInformationInterface $mod_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moderationInfo = $mod_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration, $plugin_id, $plugin_definition,
$container->get('workbench_moderation.moderation_information')
);
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
if ($entity && $this->moderationInfo->isModeratableEntity($entity)) {
drupal_set_message($this->t('One or more entities were skipped as they are under moderation and may not be directly published or unpublished.'));
return;
}
parent::execute($entity);
}
}
......@@ -17,6 +17,10 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\node\Plugin\Action\PublishNode;
use Drupal\node\Plugin\Action\UnpublishNode;
use Drupal\workbench_moderation\Plugin\Action\ModerationOptOutPublishNode;
use Drupal\workbench_moderation\Plugin\Action\ModerationOptOutUnpublishNode;
use Drupal\workbench_moderation\Plugin\Menu\EditTab;
/**
......@@ -193,6 +197,23 @@ function workbench_moderation_theme($existing, $type, $theme, $path) {
return $themes;
}
/**
* Implements hook_action_info_alter().
*/
function workbench_moderation_action_info_alter(&$definitions) {
// The publish/unpublish actions are not valid on moderated entities. So swap
// their implementations out for alternates that will become a no-op on a
// moderated node. If another module has already swapped out those classes,
// though, we'll be polite and do nothing.
if (isset($definitions['node_publish_action']['class']) && $definitions['node_publish_action']['class'] == PublishNode::class) {
$definitions['node_publish_action']['class'] = ModerationOptOutPublishNode::class;
}
if (isset($definitions['node_unpublish_action']['class']) && $definitions['node_unpublish_action']['class'] == UnpublishNode::class) {
$definitions['node_unpublish_action']['class'] = ModerationOptOutUnpublishNode::class;
}
}
/**
* Implements hook_views_data_alter().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment