Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
entity_usage
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
entity_usage
Merge requests
!136
Resolve
#3002332
"Track paragraphs on host"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Resolve
#3002332
"Track paragraphs on host"
issue/entity_usage-3002332:3002332-track-paragraphs-on-host
into
8.x-2.x
Overview
0
Commits
14
Pipelines
6
Changes
12
Open
Raf Philtjens
requested to merge
issue/entity_usage-3002332:3002332-track-paragraphs-on-host
into
8.x-2.x
3 weeks ago
Overview
0
Commits
14
Pipelines
6
Changes
12
Expand
Closes
#3002332
0
0
Merge request reports
Compare
8.x-2.x
version 5
ffe3a1be
3 weeks ago
version 4
88d4f7be
3 weeks ago
version 3
fdc246d5
3 weeks ago
version 2
693e2277
3 weeks ago
version 1
11abb2d8
3 weeks ago
8.x-2.x (HEAD)
and
latest version
latest version
545eafc0
14 commits,
3 weeks ago
version 5
ffe3a1be
13 commits,
3 weeks ago
version 4
88d4f7be
11 commits,
3 weeks ago
version 3
fdc246d5
10 commits,
3 weeks ago
version 2
693e2277
9 commits,
3 weeks ago
version 1
11abb2d8
8 commits,
3 weeks ago
12 files
+
537
−
42
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
12
Search (e.g. *.vue) (Ctrl+P)
src/Plugin/EntityUsage/Track/ParagraphInherited.php
0 → 100644
+
161
−
0
Options
<?php
namespace
Drupal\entity_usage\Plugin\EntityUsage\Track
;
use
Drupal\Core\Entity\TranslatableInterface
;
use
Drupal\Core\Field\FieldItemInterface
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\entity_usage
\EntityUpdateManagerInterface
;
use
Drupal\entity_usage
\EntityUsageTrackBase
;
use
Drupal\entity_usage
\EntityUsageTrackMultipleLoadInterface
;
use
Drupal\entity_usage
\ParagraphInheritedUsageUpdater
;
use
Drupal\paragraphs\ParagraphInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Tracks usage of entities referenced in referenced paragraphs.
*
* @EntityUsageTrack(
* id = "paragraph_inherited",
* label = @Translation("Paragraph (inherited)"),
* description = @Translation("Tracks entity relationships found inside direct and nested paragraphs as if they belong to the (outer) host entity."),
* field_types = {
* "entity_reference_revisions",
* },
* source_entity_class = "Drupal\Core\Entity\FieldableEntityInterface",
* )
*/
class
ParagraphInherited
extends
EntityUsageTrackBase
implements
EntityUsageTrackMultipleLoadInterface
{
/**
* The entity update manager.
*/
protected
EntityUpdateManagerInterface
$entityUpdateManager
;
/**
* The paragraph inherited usage updated.
*/
protected
ParagraphInheritedUsageUpdater
$paragraphInheritedUsageUpdater
;
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
):
static
{
$tracker
=
parent
::
create
(
$container
,
$configuration
,
$plugin_id
,
$plugin_definition
);
$tracker
->
entityUpdateManager
=
$container
->
get
(
'entity_usage.entity_update_manager'
);
$tracker
->
paragraphInheritedUsageUpdater
=
$container
->
get
(
'entity_usage.paragraph_inherited_usage_updater'
);
return
$tracker
;
}
/**
* {@inheritdoc}
*/
public
function
getTargetEntities
(
FieldItemInterface
$item
):
array
{
$parent
=
$item
->
getParent
();
return
$parent
instanceof
FieldItemListInterface
?
$this
->
doGetTargetEntities
(
$parent
,
$item
)
:
[];
}
/**
* {@inheritdoc}
*/
public
function
getTargetEntitiesFromField
(
FieldItemListInterface
$field
):
array
{
return
$this
->
doGetTargetEntities
(
$field
);
}
/**
* Gets the target entities.
*
* @param \Drupal\Core\Field\FieldItemListInterface<\Drupal\Core\Field\FieldItemInterface> $field
* The field.
* @param \Drupal\Core\Field\FieldItemInterface|null $field_item
* (optional) The field item.
*
* @return array<int, string|array{target_type: string, target_id: string|int, field_name?: string, method?: string}>
* An indexed array where each value can be a string or an array:
* - if a string: the target entity type and ID concatenated with a "|"
* character.
* - if an array: an array with the target_type and target_id keys, and
* optionally the method and field_name keys.
* Will return an empty array if no target entities could be retrieved from
* the received field item value.
*/
private
function
doGetTargetEntities
(
FieldItemListInterface
$field
,
?FieldItemInterface
$field_item
=
NULL
):
array
{
$target_entities
=
[];
$target_type_id
=
$field
->
getFieldDefinition
()
->
getSetting
(
'target_type'
);
if
(
$target_type_id
!==
'paragraph'
)
{
return
$target_entities
;
}
/** @var \Drupal\Core\Entity\EntityInterface $parent_entity */
$parent_entity
=
$field
->
getParent
()
->
getValue
();
/** @var \Drupal\paragraphs\ParagraphInterface[] $referenced_paragraphs */
$referenced_paragraphs
=
$field_item
instanceof
FieldItemInterface
?
array_filter
([
$field_item
->
entity
??
NULL
])
:
$field
->
referencedEntities
();
foreach
(
$referenced_paragraphs
as
$referenced_paragraph
)
{
// @todo Is this logic correct?
if
(
$referenced_paragraph
instanceof
TranslatableInterface
&&
$referenced_paragraph
->
language
()
->
getId
()
!==
$parent_entity
->
language
()
->
getId
()
&&
$referenced_paragraph
->
hasTranslation
(
$parent_entity
->
language
()
->
getId
())
)
{
$referenced_paragraph
=
$referenced_paragraph
->
getTranslation
(
$parent_entity
->
language
()
->
getId
());
}
foreach
(
$this
->
entityUpdateManager
->
getEnabledPlugins
()
as
$plugin
)
{
$trackable_field_types
=
$plugin
->
getApplicableFieldTypes
();
$fields
=
array_keys
(
$this
->
getReferencingFields
(
$referenced_paragraph
,
$trackable_field_types
));
foreach
(
$fields
as
$field_name
)
{
if
(
!
$referenced_paragraph
->
hasField
(
$field_name
)
||
$referenced_paragraph
->
{
$field_name
}
->
isEmpty
())
{
continue
;
}
$field_target_entities
=
[];
try
{
if
(
$plugin
instanceof
EntityUsageTrackMultipleLoadInterface
)
{
$field_target_entities
=
$plugin
->
getTargetEntitiesFromField
(
$referenced_paragraph
->
{
$field_name
});
}
else
{
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
foreach
(
$referenced_paragraph
->
get
(
$field_name
)
as
$field_item
)
{
$field_target_entities
=
$plugin
->
getTargetEntities
(
$field_item
);
}
}
}
catch
(
\Exception
$e
)
{
$this
->
logTrackingException
(
$e
,
$referenced_paragraph
,
$field_name
);
continue
;
}
foreach
(
$field_target_entities
as
$field_target_entity
)
{
$target_entities
[]
=
$this
->
normalizeTargetEntity
(
$field_target_entity
)
+
[
'method'
=>
"
{
$this
->
getPluginId
()
}
|
{
$plugin
->
getPluginId
()
}
"
,
'field_name'
=>
"
{
$referenced_paragraph
->
getRevisionId
()
}
:
{
$field_name
}
"
,
];
}
}
}
// Since this paragraph has now been processed, we can safely remove it
// from the usage updater queue. This prevents the usage tracking from
// happening multiple times (for example, when a user saves a paragraph
// through the paragraphs field widget).
$this
->
paragraphInheritedUsageUpdater
->
removeEntityFromQueue
(
$referenced_paragraph
);
}
foreach
(
$target_entities
as
&
$target_entity
)
{
$field_name
=
$field
->
getName
();
if
(
$parent_entity
instanceof
ParagraphInterface
)
{
$field_name
=
"
{
$parent_entity
->
getRevisionId
()
}
:
{
$field_name
}
"
;
}
$target_entity
[
'field_name'
]
=
"
$field_name
|
{
$target_entity
[
'field_name'
]
}
"
;
}
return
$target_entities
;
}
}
Loading