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 @@
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityAccessControlHandlerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
......@@ -74,6 +75,20 @@ class ForumController extends ControllerBase {
*/
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.
*
......@@ -93,8 +108,12 @@ class ForumController extends ControllerBase {
* Node type storage handler.
* @param \Drupal\Core\Render\RendererInterface $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->vocabularyStorage = $vocabulary_storage;
$this->termStorage = $term_storage;
......@@ -103,6 +122,8 @@ public function __construct(ForumManagerInterface $forum_manager, VocabularyStor
$this->fieldMap = $field_map;
$this->nodeTypeStorage = $node_type_storage;
$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
public static function create(ContainerInterface $container) {
/** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
$entity_manager = $container->get('entity.manager');
return new static(
$container->get('forum_manager'),
$entity_manager->getStorage('taxonomy_vocabulary'),
......@@ -119,7 +141,9 @@ public static function create(ContainerInterface $container) {
$entity_manager->getAccessControlHandler('node'),
$entity_manager->getFieldMap(),
$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) {
$header = $build['header'];
}
else {
$topics = '';
$header = array();
$topics = [];
$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
}
$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 [
'action' => $this->buildActionLinks($config->get('vocabulary'), $term),
'forum' => $build,
'#cache' => [
'tags' => Cache::mergeTags($this->nodeEntityTypeDefinition->getListCacheTags(), $this->commentEntityTypeDefinition->getListCacheTags()),
],
];
}
......@@ -259,6 +297,7 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
// Loop through all bundles for forum taxonomy vocabulary field.
foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) {
if ($this->nodeAccess->createAccess($type)) {
$node_type = $this->nodeTypeStorage->load($type);
$links[$type] = [
'#attributes' => ['class' => ['action-links']],
'#theme' => 'menu_local_action',
......@@ -268,6 +307,9 @@ protected function buildActionLinks($vid, TermInterface $forum_term = NULL) {
]),
'url' => Url::fromRoute('node.add', ['node_type' => $type]),
],
'#cache' => [
'tags' => $node_type->getCacheTags(),
],
];
if ($forum_term && $forum_term->bundle() == $vid) {
// We are viewing a forum term (specific forum), append the tid to
......
......@@ -27,7 +27,7 @@ protected function setUp() {
parent::setUp();
// 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);
}
......@@ -55,9 +55,25 @@ function testForumIndexStatus() {
$node = $this->drupalGetNodeByTitle($title);
$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.
$this->drupalGet('forum/' . $tid);
$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.
$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