Commit 41cc210c authored by alexpott's avatar alexpott

Issue #2500013 by Berdir, Wim Leers, plach: Add cacheability metadata...

Issue #2500013 by Berdir, Wim Leers, plach: Add cacheability metadata information to translation overview
parent f57b659d
......@@ -8,6 +8,7 @@
namespace Drupal\content_translation\Controller;
use Drupal\content_translation\ContentTranslationManagerInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Language\LanguageInterface;
......@@ -92,6 +93,10 @@ public function overview(RouteMatchInterface $route_match, $entity_type_id = NUL
$manager = $this->manager;
$entity_type = $entity->getEntityType();
// Start collecting the cacheability metadata, starting with the entity and
// later merge in the access result cacheability metadata.
$cacheability = CacheableMetadata::createFromObject($entity);
$languages = $this->languageManager()->getLanguages();
$original = $entity->getUntranslated()->language()->getId();
$translations = $entity->getTranslationLanguages();
......@@ -180,11 +185,16 @@ public function overview(RouteMatchInterface $route_match, $entity_type_id = NUL
// If the user is allowed to edit the entity we point the edit link to
// the entity form, otherwise if we are not dealing with the original
// language we point the link to the translation form.
if ($entity->access('update') && $entity_type->hasLinkTemplate('edit-form')) {
$update_access = $entity->access('update', NULL, TRUE);
$translation_access = $handler->getTranslationAccess($entity, 'update');
$cacheability = $cacheability
->merge(CacheableMetadata::createFromObject($update_access))
->merge(CacheableMetadata::createFromObject($translation_access));
if ($update_access->isAllowed() && $entity_type->hasLinkTemplate('edit-form')) {
$links['edit']['url'] = $entity->urlInfo('edit-form');
$links['edit']['language'] = $language;
}
elseif (!$is_original && $handler->getTranslationAccess($entity, 'update')->isAllowed()) {
elseif (!$is_original && $translation_access->isAllowed()) {
$links['edit']['url'] = $edit_url;
}
......@@ -206,6 +216,11 @@ public function overview(RouteMatchInterface $route_match, $entity_type_id = NUL
}
else {
$source_name = isset($languages[$source]) ? $languages[$source]->getName() : $this->t('n/a');
$delete_access = $entity->access('delete', NULL, TRUE);
$translation_access = $handler->getTranslationAccess($entity, 'delete');
$cacheability = $cacheability
->merge(CacheableMetadata::createFromObject($delete_access))
->merge(CacheableMetadata::createFromObject($translation_access));
if ($entity->access('delete') && $entity_type->hasLinkTemplate('delete-form')) {
$links['delete'] = array(
'title' => $this->t('Delete'),
......@@ -213,7 +228,7 @@ public function overview(RouteMatchInterface $route_match, $entity_type_id = NUL
'language' => $language,
);
}
elseif ($handler->getTranslationAccess($entity, 'delete')->isAllowed()) {
elseif ($translation_access->isAllowed()) {
$links['delete'] = array(
'title' => $this->t('Delete'),
'url' => $delete_url,
......@@ -226,7 +241,10 @@ public function overview(RouteMatchInterface $route_match, $entity_type_id = NUL
$row_title = $source_name = $this->t('n/a');
$source = $entity->language()->getId();
if ($source != $langcode && $handler->getTranslationAccess($entity, 'create')->isAllowed()) {
$create_translation_access = $handler->getTranslationAccess($entity, 'create');
$cacheability = $cacheability
->merge(CacheableMetadata::createFromObject($create_translation_access));
if ($source != $langcode && $create_translation_access->isAllowed()) {
if ($translatable) {
$links['add'] = array(
'title' => $this->t('Add'),
......@@ -284,6 +302,9 @@ public function overview(RouteMatchInterface $route_match, $entity_type_id = NUL
// Add metadata to the build render array to let other modules know about
// which entity this is.
$build['#entity'] = $entity;
$cacheability
->addCacheTags($entity->getCacheTags())
->applyTo($build);
$build['content_translation_overview'] = array(
'#theme' => 'table',
......
......@@ -113,6 +113,13 @@ function testWorkflows() {
$this->drupalLogin($user);
$this->drupalGet($translations_url);
// Make sure that the user.permissions cache context and the cache tags
// for the entity are present.
$this->assertCacheContext('user.permissions');
foreach ($this->entity->getCacheTags() as $cache_tag) {
$this->assertCacheTag($cache_tag);
}
foreach ($ops as $op => $label) {
if ($op != $current_op) {
$this->assertNoLink($label, format_string('No %op link found.', array('%op' => $label)));
......
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