Issue #2866656 by alexpott, yoroy, timmillwood: Clicking on "New Draft" tab allows you to archive the content
parent
d7d8103806
commit
eee757d9be
|
@ -10,7 +10,6 @@ use Drupal\content_moderation\EntityTypeInfo;
|
|||
use Drupal\content_moderation\ContentPreprocess;
|
||||
use Drupal\content_moderation\Plugin\Action\ModerationOptOutPublishNode;
|
||||
use Drupal\content_moderation\Plugin\Action\ModerationOptOutUnpublishNode;
|
||||
use Drupal\content_moderation\Plugin\Menu\EditTab;
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
|
@ -99,22 +98,6 @@ function content_moderation_entity_update(EntityInterface $entity) {
|
|||
->entityUpdate($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_local_tasks_alter().
|
||||
*/
|
||||
function content_moderation_local_tasks_alter(&$local_tasks) {
|
||||
$content_entity_type_ids = array_keys(array_filter(\Drupal::entityTypeManager()->getDefinitions(), function (EntityTypeInterface $entity_type) {
|
||||
return $entity_type->isRevisionable();
|
||||
}));
|
||||
|
||||
foreach ($content_entity_type_ids as $content_entity_type_id) {
|
||||
if (isset($local_tasks["entity.$content_entity_type_id.edit_form"])) {
|
||||
$local_tasks["entity.$content_entity_type_id.edit_form"]['class'] = EditTab::class;
|
||||
$local_tasks["entity.$content_entity_type_id.edit_form"]['entity_type_id'] = $content_entity_type_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_alter().
|
||||
*/
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\content_moderation\Plugin\Menu;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\Core\Menu\LocalTaskDefault;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
use Drupal\content_moderation\ModerationInformation;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Defines a class for making the edit tab use 'Edit draft' or 'New draft'.
|
||||
*/
|
||||
class EditTab extends LocalTaskDefault implements ContainerFactoryPluginInterface {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* The moderation information service.
|
||||
*
|
||||
* @var \Drupal\content_moderation\ModerationInformation
|
||||
*/
|
||||
protected $moderationInfo;
|
||||
|
||||
/**
|
||||
* The entity if determinable from the route or FALSE.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\ContentEntityInterface|FALSE
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* Constructs a new EditTab object.
|
||||
*
|
||||
* @param array $configuration
|
||||
* Plugin configuration.
|
||||
* @param string $plugin_id
|
||||
* Plugin ID.
|
||||
* @param mixed $plugin_definition
|
||||
* Plugin definition.
|
||||
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
|
||||
* The translation service.
|
||||
* @param \Drupal\content_moderation\ModerationInformation $moderation_information
|
||||
* The moderation information.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $string_translation, ModerationInformation $moderation_information) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$this->stringTranslation = $string_translation;
|
||||
$this->moderationInfo = $moderation_information;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('string_translation'),
|
||||
$container->get('content_moderation.moderation_information')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRouteParameters(RouteMatchInterface $route_match) {
|
||||
$entity_parameter = $route_match->getParameter($this->pluginDefinition['entity_type_id']);
|
||||
$this->entity = $entity_parameter instanceof ContentEntityInterface ? $route_match->getParameter($this->pluginDefinition['entity_type_id']) : FALSE;
|
||||
return parent::getRouteParameters($route_match);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTitle() {
|
||||
// If the entity couldn't be loaded or moderation isn't enabled.
|
||||
if (!$this->entity || !$this->moderationInfo->isModeratedEntity($this->entity)) {
|
||||
return parent::getTitle();
|
||||
}
|
||||
|
||||
return $this->moderationInfo->isLiveRevision($this->entity)
|
||||
? $this->t('New draft')
|
||||
: $this->t('Edit draft');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCacheTags() {
|
||||
$tags = parent::getCacheTags();
|
||||
// Tab changes if node or node-type is modified.
|
||||
if ($this->entity) {
|
||||
$tags = array_merge($tags, $this->entity->getCacheTags());
|
||||
$tags[] = $this->entity->getEntityType()->getBundleEntityType() . ':' . $this->entity->bundle();
|
||||
}
|
||||
return $tags;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\content_moderation\Functional;
|
||||
|
||||
use Drupal\simpletest\ContentTypeCreationTrait;
|
||||
use Drupal\simpletest\NodeCreationTrait;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
use Drupal\workflows\Entity\Workflow;
|
||||
|
||||
/**
|
||||
* Test the content moderation local task.
|
||||
*
|
||||
* @group content_moderation
|
||||
*/
|
||||
class LocalTaskTest extends BrowserTestBase {
|
||||
|
||||
use ContentTypeCreationTrait;
|
||||
use NodeCreationTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = [
|
||||
'content_moderation_test_local_task',
|
||||
'content_moderation',
|
||||
'block',
|
||||
];
|
||||
|
||||
/**
|
||||
* A test node.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $testNode;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block']);
|
||||
$this->drupalLogin($this->createUser(['bypass node access']));
|
||||
|
||||
$node_type = $this->createContentType([
|
||||
'type' => 'test_content_type',
|
||||
]);
|
||||
|
||||
// Now enable moderation for subsequent nodes.
|
||||
$workflow = Workflow::load('editorial');
|
||||
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', $node_type->id());
|
||||
$workflow->save();
|
||||
|
||||
$this->testNode = $this->createNode([
|
||||
'type' => $node_type->id(),
|
||||
'moderation_state' => 'draft',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests local tasks behave with content_moderation enabled.
|
||||
*/
|
||||
public function testLocalTasks() {
|
||||
// The default state is a draft.
|
||||
$this->drupalGet(sprintf('node/%s', $this->testNode->id()));
|
||||
$this->assertTasks('Edit draft');
|
||||
|
||||
// When published as the live revision, the label changes.
|
||||
$this->testNode->moderation_state = 'published';
|
||||
$this->testNode->save();
|
||||
$this->drupalGet(sprintf('node/%s', $this->testNode->id()));
|
||||
$this->assertTasks('New draft');
|
||||
|
||||
$tags = $this->drupalGetHeader('X-Drupal-Cache-Tags');
|
||||
$this->assertContains('node:1', $tags);
|
||||
$this->assertContains('node_type:test_content_type', $tags);
|
||||
|
||||
// Without an upcast node, the state cannot be determined.
|
||||
$this->clickLink('Task Without Upcast Node');
|
||||
$this->assertTasks('Edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the correct tasks appear.
|
||||
*
|
||||
* @param string $edit_tab_label
|
||||
* The edit tab label to assert.
|
||||
*/
|
||||
protected function assertTasks($edit_tab_label) {
|
||||
$this->assertSession()->linkExists('View');
|
||||
$this->assertSession()->linkExists('Task Without Upcast Node');
|
||||
$this->assertSession()->linkExists($edit_tab_label);
|
||||
$this->assertSession()->linkExists('Delete');
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue