Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cache_tools
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
cache_tools
Commits
b6be07ac
Commit
b6be07ac
authored
4 years ago
by
Petar Bašić
Committed by
Wolfgang Ziegler
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3185895
by petar_basic, fago: Add support for via parent invalidation strategy
parent
e5cb1391
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
composer.json
+3
-1
3 additions, 1 deletion
composer.json
src/Service/CacheInvalidator.php
+54
-7
54 additions, 7 deletions
src/Service/CacheInvalidator.php
with
57 additions
and
8 deletions
composer.json
+
3
−
1
View file @
b6be07ac
...
...
@@ -4,5 +4,7 @@
"type"
:
"drupal-module"
,
"license"
:
"GPL-2.0+"
,
"minimum-stability"
:
"dev"
,
"require"
:
{
}
"require"
:
{
"drunomics/service-utils"
:
"*"
,
}
}
This diff is collapsed.
Click to expand it.
src/Service/CacheInvalidator.php
+
54
−
7
View file @
b6be07ac
...
...
@@ -2,6 +2,7 @@
namespace
Drupal\cache_tools\Service
;
use
drunomics\ServiceUtils\Core\Entity\EntityTypeManagerTrait
;
use
Drupal\Core\Cache\CacheTagsInvalidatorInterface
;
use
Drupal\Core\Entity\EntityPublishedInterface
;
use
Drupal\Core\Entity\EntityInterface
;
...
...
@@ -15,6 +16,8 @@ use Drupal\Core\Entity\FieldableEntityInterface;
*/
class
CacheInvalidator
{
use
EntityTypeManagerTrait
;
/**
* The cache tag invalidator.
*
...
...
@@ -137,17 +140,18 @@ class CacheInvalidator {
$tag_prefix
=
$this
->
getPublishedEntityCacheTag
(
$entity
)
.
':'
;
foreach
(
$this
->
settings
[
'invalidate'
][
$entity_type
]
as
$cache_parameter
)
{
$parts
=
explode
(
':'
,
$cache_parameter
);
if
(
count
(
$parts
)
!=
2
||
$parts
[
0
]
!=
$bundle
)
{
if
(
count
(
$parts
)
<
2
||
$parts
[
0
]
!=
$bundle
)
{
// This setting is not for the current bundle or not field-based.
continue
;
}
$field_name
=
$parts
[
1
];
$invalidate_term_parents
=
isset
(
$parts
[
2
])
&&
$parts
[
2
]
===
'parents'
?
TRUE
:
FALSE
;
if
(
$entity
->
hasField
(
$field_name
))
{
$field_definition
=
$entity
->
getFieldDefinition
(
$field_name
);
$settings
=
$field_definition
->
getSettings
();
$target_type
=
empty
(
$settings
[
'target_type'
])
?
''
:
$settings
[
'target_type'
];
// The name of the value property, e.g. 'value' or 'target_id'.
$key
=
$entity
->
getFieldDefinition
(
$field_name
)
->
getFieldStorageDefinition
()
->
getMainPropertyName
();
$key
=
$field_definition
->
getFieldStorageDefinition
()
->
getMainPropertyName
();
if
(
is_null
(
$key
))
{
// The field has no main value property.
continue
;
...
...
@@ -162,7 +166,7 @@ class CacheInvalidator {
// Add tag for the original field value.
/** @var \Drupal\Core\Field\EntityReferenceFieldItemList $field_items */
foreach
(
$entity_compare
->
get
(
$field_name
)
->
getValue
()
as
$value
)
{
$tags
[]
=
$tag_prefix_field
.
$value
[
$key
]
;
$tags
=
array_merge
(
$tags
,
$this
->
generateTagsBasedOnInvalidationStrategy
(
$target_type
,
$value
[
$key
],
$tag_prefix_field
,
$invalidate_term_parents
))
;
}
}
}
...
...
@@ -170,7 +174,7 @@ class CacheInvalidator {
// Add tag for the new field value.
/** @var \Drupal\Core\Field\EntityReferenceFieldItemList $field_items */
foreach
(
$entity
->
get
(
$field_name
)
->
getValue
()
as
$value
)
{
$tags
[]
=
$tag_prefix_field
.
$value
[
$key
]
;
$tags
=
array_merge
(
$tags
,
$this
->
generateTagsBasedOnInvalidationStrategy
(
$target_type
,
$value
[
$key
],
$tag_prefix_field
,
$invalidate_term_parents
))
;
}
}
}
...
...
@@ -179,6 +183,49 @@ class CacheInvalidator {
return
$tags
;
}
/**
* Returns parent tids for a given tid, or $tid if no parent exists.
*
* @param int $tid
* A taxonomy term id.
*
* @return array
* Returns $tid if no parents are found, else parents tids.
*/
public
function
taxonomyGetParents
(
$tid
)
{
$ancestors
=
$this
->
getEntityTypeManager
()
->
getStorage
(
'taxonomy_term'
)
->
loadAllParents
(
$tid
);
return
empty
(
$ancestors
)
?
[
$tid
]
:
array_keys
(
$ancestors
);
}
/**
* Returns an array of cache tags to invalidate.
*
* @param string $target_type
* Type of the referenced entity.
* @param mixed $entity_id
* An entity id for which to generate tags.
* @param string $tag_prefix_field
* Prefix for generated tags.
* @param bool $invalidate_term_parents
* Flag to indicate invalidation strategy.
* Only applicable for entities of type taxonomy_term.
*
* @return array
* An array of cache tags to invalidate.
*/
public
function
generateTagsBasedOnInvalidationStrategy
(
$target_type
,
$entity_id
,
$tag_prefix_field
,
$invalidate_term_parents
=
FALSE
)
{
$tags
=
[];
if
(
$target_type
===
'taxonomy_term'
&&
$invalidate_term_parents
)
{
foreach
(
$this
->
taxonomyGetParents
(
$entity_id
)
as
$parentKey
)
{
$tags
[]
=
$tag_prefix_field
.
$parentKey
;
}
}
else
{
$tags
[]
=
$tag_prefix_field
.
$entity_id
;
}
return
$tags
;
}
/**
* Invalidates published entity field-based cache tags.
*
...
...
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