Commit 41560b32 authored by alexpott's avatar alexpott

Issue #2484619 by borisson_, Wim Leers, lauriii, larowlan, dawehner: Forum...

Issue #2484619 by borisson_, Wim Leers, lauriii, larowlan, dawehner: Forum responses don't set cache tags
parent 78a423e4
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityAccessControlHandlerInterface; use Drupal\Core\Entity\EntityAccessControlHandlerInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Render\RendererInterface; use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
...@@ -74,6 +75,20 @@ class ForumController extends ControllerBase { ...@@ -74,6 +75,20 @@ class ForumController extends ControllerBase {
*/ */
protected $renderer; protected $renderer;
/**
* Node entity type, we need to get cache tags from here.
*
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $nodeEntityTypeDefinition;
/**
* Comment entity type, we need to get cache tags from here.
*
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $commentEntityTypeDefinition;
/** /**
* Constructs a ForumController object. * Constructs a ForumController object.
* *
...@@ -93,8 +108,12 @@ class ForumController extends ControllerBase { ...@@ -93,8 +108,12 @@ class ForumController extends ControllerBase {
* Node type storage handler. * Node type storage handler.
* @param \Drupal\Core\Render\RendererInterface $renderer * @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer. * The renderer.
* @param \Drupal\Core\Entity\EntityTypeInterface $node_entity_type_definition
* Node entity type definition object
* @param \Drupal\Core\Entity\EntityTypeInterface $comment_entity_type_definition
* Comment entity type definition object
*/ */
public function __construct(ForumManagerInterface $forum_manager, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, AccountInterface $current_user, EntityAccessControlHandlerInterface $node_access, array $field_map, EntityStorageInterface $node_type_storage, RendererInterface $renderer) { public function __construct(ForumManagerInterface $forum_manager, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, AccountInterface $current_user, EntityAccessControlHandlerInterface $node_access, array $field_map, EntityStorageInterface $node_type_storage, RendererInterface $renderer, EntityTypeInterface $node_entity_type_definition, EntityTypeInterface $comment_entity_type_definition) {
$this->forumManager = $forum_manager; $this->forumManager = $forum_manager;
$this->vocabularyStorage = $vocabulary_storage; $this->vocabularyStorage = $vocabulary_storage;
$this->termStorage = $term_storage; $this->termStorage = $term_storage;
...@@ -103,6 +122,8 @@ public function __construct(ForumManagerInterface $forum_manager, VocabularyStor ...@@ -103,6 +122,8 @@ public function __construct(ForumManagerInterface $forum_manager, VocabularyStor
$this->fieldMap = $field_map; $this->fieldMap = $field_map;
$this->nodeTypeStorage = $node_type_storage; $this->nodeTypeStorage = $node_type_storage;
$this->renderer = $renderer; $this->renderer = $renderer;
$this->nodeEntityTypeDefinition = $node_entity_type_definition;
$this->commentEntityTypeDefinition = $comment_entity_type_definition;
} }
/** /**
...@@ -111,6 +132,7 @@ public function __construct(ForumManagerInterface $forum_manager, VocabularyStor ...@@ -111,6 +132,7 @@ public function __construct(ForumManagerInterface $forum_manager, VocabularyStor
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
/** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */ /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
$entity_manager = $container->get('entity.manager'); $entity_manager = $container->get('entity.manager');
return new static( return new static(
$container->get('forum_manager'), $container->get('forum_manager'),
$entity_manager->getStorage('taxonomy_vocabulary'), $entity_manager->getStorage('taxonomy_vocabulary'),
...@@ -119,7 +141,9 @@ public static function create(ContainerInterface $container) { ...@@ -119,7 +141,9 @@ public static function create(ContainerInterface $container) {
$entity_manager->getAccessControlHandler('node'), $entity_manager->getAccessControlHandler('node'),
$entity_manager->getFieldMap(), $entity_manager->getFieldMap(),
$entity_manager->getStorage('node_type'), $entity_manager->getStorage('node_type'),
$container->get('renderer') $container->get('renderer'),
$entity_manager->getDefinition('node'),
$entity_manager->getDefinition('comment')
); );
} }
...@@ -143,8 +167,8 @@ public function forumPage(TermInterface $taxonomy_term) { ...@@ -143,8 +167,8 @@ public function forumPage(TermInterface $taxonomy_term) {
$header = $build['header']; $header = $build['header'];
} }
else { else {
$topics = ''; $topics = [];
$header = array(); $header = [];
} }
return $this->build($taxonomy_term->forums, $taxonomy_term, $topics, $taxonomy_term->parents, $header); return $this->build($taxonomy_term->forums, $taxonomy_term, $topics, $taxonomy_term->parents, $header);
} }
...@@ -205,9 +229,23 @@ protected function build($forums, TermInterface $term, $topics = array(), $paren ...@@ -205,9 +229,23 @@ protected function build($forums, TermInterface $term, $topics = array(), $paren
} }
$this->renderer->addCacheableDependency($build, $config); $this->renderer->addCacheableDependency($build, $config);
foreach ($forums as $forum) {
$this->renderer->addCacheableDependency($build, $forum);
}
foreach ($topics as $topic) {
$this->renderer->addCacheableDependency($build, $topic);
}
foreach ($parents as $parent) {
$this->renderer->addCacheableDependency($build, $parent);
}
$this->renderer->addCacheableDependency($build, $term);
return [ return [
'action' => $this->buildActionLinks($config->get('vocabulary'), $term), 'action' => $this->buildActionLinks($config->get('vocabulary'), $term),
'forum' => $build, 'forum' => $build,
'#cache' => [
'tags' => Cache::mergeTags($this->nodeEntityTypeDefinition->getListCacheTags(), $this->commentEntityTypeDefinition->getListCacheTags()),
],
]; ];
} }
...@@ -259,6 +297,7 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) { ...@@ -259,6 +297,7 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
// Loop through all bundles for forum taxonomy vocabulary field. // Loop through all bundles for forum taxonomy vocabulary field.
foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) { foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) {
if ($this->nodeAccess->createAccess($type)) { if ($this->nodeAccess->createAccess($type)) {
$node_type = $this->nodeTypeStorage->load($type);
$links[$type] = [ $links[$type] = [
'#attributes' => ['class' => ['action-links']], '#attributes' => ['class' => ['action-links']],
'#theme' => 'menu_local_action', '#theme' => 'menu_local_action',
...@@ -268,6 +307,9 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) { ...@@ -268,6 +307,9 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
]), ]),
'url' => Url::fromRoute('node.add', ['node_type' => $type]), 'url' => Url::fromRoute('node.add', ['node_type' => $type]),
], ],
'#cache' => [
'tags' => $node_type->getCacheTags(),
],
]; ];
if ($forum_term && $forum_term->bundle() == $vid) { if ($forum_term && $forum_term->bundle() == $vid) {
// We are viewing a forum term (specific forum), append the tid to // We are viewing a forum term (specific forum), append the tid to
......
...@@ -27,7 +27,7 @@ protected function setUp() { ...@@ -27,7 +27,7 @@ protected function setUp() {
parent::setUp(); parent::setUp();
// Create a test user. // Create a test user.
$web_user = $this->drupalCreateUser(array('create forum content', 'edit own forum content', 'edit any forum content', 'administer nodes')); $web_user = $this->drupalCreateUser(['create forum content', 'edit own forum content', 'edit any forum content', 'administer nodes', 'administer forums']);
$this->drupalLogin($web_user); $this->drupalLogin($web_user);
} }
...@@ -55,9 +55,25 @@ function testForumIndexStatus() { ...@@ -55,9 +55,25 @@ function testForumIndexStatus() {
$node = $this->drupalGetNodeByTitle($title); $node = $this->drupalGetNodeByTitle($title);
$this->assertTrue(!empty($node), 'New forum node found in database.'); $this->assertTrue(!empty($node), 'New forum node found in database.');
// Create a child forum.
$edit = array(
'name[0][value]' => $this->randomMachineName(20),
'description[0][value]' => $this->randomMachineName(200),
'parent[0]' => $tid,
);
$this->drupalPostForm('admin/structure/forum/add/forum', $edit, t('Save'));
$tid_child = $tid + 1;
// Verify that the node appears on the index. // Verify that the node appears on the index.
$this->drupalGet('forum/' . $tid); $this->drupalGet('forum/' . $tid);
$this->assertText($title, 'Published forum topic appears on index.'); $this->assertText($title, 'Published forum topic appears on index.');
$this->assertCacheTag('node_list');
$this->assertCacheTag('config:node.type.forum');
$this->assertCacheTag('comment_list');
$this->assertCacheTag('node:' . $node->id());
$this->assertCacheTag('taxonomy_term:' . $tid);
$this->assertCacheTag('taxonomy_term:' . $tid_child);
// Unpublish the node. // Unpublish the node.
$this->drupalPostForm('node/' . $node->id() . '/edit', array(), t('Save and unpublish')); $this->drupalPostForm('node/' . $node->id() . '/edit', array(), t('Save and unpublish'));
......
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