Skip to content
Snippets Groups Projects
Commit 3ba200fa authored by Julian Pustkuchen's avatar Julian Pustkuchen
Browse files

Merge branch '2731425-lots-of-unnecessary' into '8.x-1.x'

Issue #2731425 by james.williams, steven jones, anybody: Lots of unnecessary...

See merge request !4
parents 19a57b79 40e2fc2b
Branches
Tags 2.0.13
No related merge requests found
Pipeline #369911 skipped
......@@ -3,6 +3,7 @@
namespace Drupal\token_custom\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\token_custom\TokenCustomTypeInterface;
/**
......@@ -91,4 +92,13 @@ class TokenCustomType extends ConfigEntityBundleBase implements TokenCustomTypeI
return $this;
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
drupal_static_reset('token_custom_type_allowlist');
}
}
......@@ -5,6 +5,7 @@
* Hooks for Custom Tokens module.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\token_custom\Entity\TokenCustom;
......@@ -43,17 +44,58 @@ function token_custom_token_info() {
*/
function token_custom_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$token_customs = TokenCustom::loadMultiple(array_keys($tokens));
foreach ($token_customs as $machine_name => $token_custom) {
if (($type == $token_custom->bundle()) && array_key_exists($machine_name, $tokens)) {
if ($token_custom instanceof TranslatableInterface) {
$langcode = isset($options['langcode']) ? $options['langcode'] : Language::LANGCODE_DEFAULT;
$token_custom = \Drupal::service('entity.repository')->getTranslationFromContext($token_custom, $langcode);
}
$allowlist = token_custom_type_allowlist();
if (isset($allowlist[$type])) {
$token_customs = TokenCustom::loadMultiple(array_keys($tokens));
foreach ($token_customs as $machine_name => $token_custom) {
if (($type == $token_custom->bundle()) && array_key_exists($machine_name, $tokens)) {
if ($token_custom instanceof TranslatableInterface) {
$langcode = isset($options['langcode']) ? $options['langcode'] : Language::LANGCODE_DEFAULT;
$token_custom = \Drupal::service('entity.repository')->getTranslationFromContext($token_custom, $langcode);
}
$replacements[$tokens[$machine_name]] = $token_custom->getFormattedContent();
$bubbleable_metadata->addCacheableDependency($token_custom);
$replacements[$tokens[$machine_name]] = $token_custom->getFormattedContent();
$bubbleable_metadata->addCacheableDependency($token_custom);
}
}
}
return $replacements;
}
/**
* Rebuild the token type allow list.
*/
function token_custom_type_allowlist($rebuild = FALSE) {
$allowlist = &drupal_static(__FUNCTION__, NULL);
if (!$rebuild) {
if (!isset($allowlist)) {
$cache = \Drupal::cache()->get('token_custom.allowlist');
if ($cache && isset($cache->data)) {
$allowlist = $cache->data;
}
else {
$rebuild = TRUE;
}
}
}
if ($rebuild) {
$token_type_storage = \Drupal::entityTypeManager()
->getStorage('token_custom_type');
// @TODO Only return types with tokens that actually exist.
$allowlist = $token_type_storage->getQuery()
->accessCheck(FALSE)
->execute();
\Drupal::cache()->set(
'token_custom.allowlist',
$allowlist,
Cache::PERMANENT,
$token_type_storage->getEntityType()->getListCacheTags()
);
}
return $allowlist;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment