Skip to content

Commit

Permalink
OPENEUROPA-1754: Add cacheablemetadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
imanoleguskiza committed Apr 8, 2019
1 parent 225f67a commit d1b33ea
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Plugin/Block/ContentLanguageBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\oe_multilingual\Plugin\Block;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Language\LanguageManagerInterface;
Expand Down Expand Up @@ -82,17 +83,21 @@ public static function create(ContainerInterface $container, array $configuratio
*/
public function build() {
$build = [];
$cache = new CacheableMetadata();

$entity = $this->multilingualHelper->getEntityFromCurrentRoute();
// Bail out if there is no entity or if it's not a content entity.
if (!$entity || !$entity instanceof ContentEntityInterface) {
return $build;
}
$cache->addCacheableDependency($entity);

// Render the links only if the current entity translation language is not
// the same as the current site language.
$translation = $this->multilingualHelper->getCurrentLanguageEntityTranslation($entity);
$cache->addCacheableDependency($translation->language());
if ($translation->language()->getId() === $this->languageManager->getCurrentLanguage()->getId()) {
$cache->applyTo($build);
return $build;
}

Expand All @@ -108,7 +113,7 @@ public function build() {
],
'#set_active_class' => TRUE,
];

$cache->applyTo($build);
return $build;
}

Expand Down

0 comments on commit d1b33ea

Please sign in to comment.