Commit 0391e315 authored by alexpott's avatar alexpott

Issue #2828438 by Adita, timmillwood, Sam152, rachel_norfolk, jp.stacey:...

Issue #2828438 by Adita, timmillwood, Sam152, rachel_norfolk, jp.stacey: Exception when adding tab to a node managed by content moderation
parent f0286aee
......@@ -2,6 +2,7 @@
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;
......@@ -25,9 +26,9 @@ class EditTab extends LocalTaskDefault implements ContainerFactoryPluginInterfac
protected $moderationInfo;
/**
* The entity.
* The entity if determinable from the route or FALSE.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
* @var \Drupal\Core\Entity\ContentEntityInterface|FALSE
*/
protected $entity;
......@@ -69,8 +70,8 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function getRouteParameters(RouteMatchInterface $route_match) {
// Override the node here with the latest revision.
$this->entity = $route_match->getParameter($this->pluginDefinition['entity_type_id']);
$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);
}
......@@ -78,8 +79,8 @@ public function getRouteParameters(RouteMatchInterface $route_match) {
* {@inheritdoc}
*/
public function getTitle() {
if (!$this->moderationInfo->isModeratedEntity($this->entity)) {
// Moderation isn't enabled.
// If the entity couldn't be loaded or moderation isn't enabled.
if (!$this->entity || !$this->moderationInfo->isModeratedEntity($this->entity)) {
return parent::getTitle();
}
......@@ -96,8 +97,10 @@ public function getCacheTags() {
// @todo https://www.drupal.org/node/2779933 write a test for this.
$tags = parent::getCacheTags();
// Tab changes if node or node-type is modified.
$tags = array_merge($tags, $this->entity->getCacheTags());
$tags[] = $this->entity->getEntityType()->getBundleEntityType() . ':' . $this->entity->bundle();
if ($this->entity) {
$tags = array_merge($tags, $this->entity->getCacheTags());
$tags[] = $this->entity->getEntityType()->getBundleEntityType() . ':' . $this->entity->bundle();
}
return $tags;
}
......
name: 'Content moderation test local task'
type: module
description: 'Provides a local task for testing.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- content_moderation
- node
entity.node.test_local_task_without_upcast_node:
route_name: entity.node.test_local_task_without_upcast_node
base_route: entity.node.canonical
title: 'Task Without Upcast Node'
entity.node.test_local_task_without_upcast_node:
path: '/node/{node}/task-without-upcast-node'
defaults:
_title: 'Page Without Upcast Node'
_controller: '\Drupal\content_moderation_test_local_task\Controller\TestLocalTaskController::methodWithoutUpcastNode'
requirements:
_access: 'TRUE'
<?php
namespace Drupal\content_moderation_test_local_task\Controller;
/**
* A test controller.
*/
class TestLocalTaskController {
/**
* A method which does not hint the node parameter to avoid upcasting.
*/
public function methodWithoutUpcastNode($node) {
return ['#markup' => 'It works!'];
}
}
<?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();
// 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() {
$this->drupalGet(sprintf('node/%s', $this->testNode->id()));
$this->assertTasks(TRUE);
$this->clickLink('Task Without Upcast Node');
$this->assertTasks(FALSE);
}
/**
* Assert the correct tasks appear.
*/
protected function assertTasks($with_upcast_node) {
$this->assertSession()->linkExists('View');
$this->assertSession()->linkExists('Task Without Upcast Node');
$this->assertSession()->linkExists($with_upcast_node ? 'Edit draft' : 'Edit');
$this->assertSession()->linkExists('Delete');
}
}
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