Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
token_custom
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
token_custom
Commits
3ba200fa
Commit
3ba200fa
authored
5 months ago
by
Julian Pustkuchen
Browse files
Options
Downloads
Plain Diff
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
Branches containing commit
Tags
2.0.13
Tags containing commit
No related merge requests found
Pipeline
#369911
skipped
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Entity/TokenCustomType.php
+10
-0
10 additions, 0 deletions
src/Entity/TokenCustomType.php
token_custom.module
+51
-9
51 additions, 9 deletions
token_custom.module
with
61 additions
and
9 deletions
src/Entity/TokenCustomType.php
+
10
−
0
View file @
3ba200fa
...
...
@@ -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'
);
}
}
This diff is collapsed.
Click to expand it.
token_custom.module
+
51
−
9
View file @
3ba200fa
...
...
@@ -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
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment