Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
insert_view_adv
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
insert_view_adv
Merge requests
!5
Issue
#3463231
: Plugin for tracking entities used in contextual filters from insert view
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3463231
: Plugin for tracking entities used in contextual filters from insert view
issue/insert_view_adv-3463231:3463231-integrate-with-entity
into
2.0.x
Overview
0
Commits
6
Pipelines
0
Changes
5
Open
Eric Smith
requested to merge
issue/insert_view_adv-3463231:3463231-integrate-with-entity
into
2.0.x
11 months ago
Overview
0
Commits
6
Pipelines
0
Changes
5
Expand
0
0
Merge request reports
Compare
2.0.x
version 6
445e49d1
3 months ago
version 5
d0f4af26
4 months ago
version 4
a79bcdbc
4 months ago
version 3
c907f27f
4 months ago
version 2
d65e011b
11 months ago
version 1
49f2666f
11 months ago
2.0.x (HEAD)
and
latest version
latest version
8cf15c64
6 commits,
3 months ago
version 6
445e49d1
6 commits,
3 months ago
version 5
d0f4af26
5 commits,
4 months ago
version 4
a79bcdbc
4 commits,
4 months ago
version 3
c907f27f
3 commits,
4 months ago
version 2
d65e011b
2 commits,
11 months ago
version 1
49f2666f
1 commit,
11 months ago
5 files
+
768
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
src/Plugin/EntityUsage/Track/InsertViewArguments.php
0 → 100644
+
133
−
0
Options
<?php
namespace
Drupal\insert_view_adv\Plugin\EntityUsage\Track
;
use
Drupal\Component\Utility\Html
;
use
Drupal\entity_usage
\Plugin\EntityUsage\Track\TextFieldEmbedBase
;
use
Drupal\taxonomy\Plugin\views\argument\IndexTid
;
use
Drupal\taxonomy\Plugin\views\argument\IndexTidDepth
;
use
Drupal\taxonomy\Plugin\views\argument\IndexTidDepthModifier
;
use
Drupal\views\Plugin\views\argument\EntityReferenceArgument
;
use
Drupal\views\Plugin\views\ViewsHandlerInterface
;
use
Drupal\views\Views
;
/**
* Tracks usage of entities used as contextual filters by advanced insert view.
*
* @EntityUsageTrack(
* id = "insert_view_arguments",
* label = @Translation("Insert view arguments"),
* description = @Translation("Tracks relationships created with 'Advanced Insert View arguments' in formatted text fields."),
* field_types = {"text", "text_long", "text_with_summary"},
* )
*/
class
InsertViewArguments
extends
TextFieldEmbedBase
{
/**
* View argument handlers keyed by view id, display id.
*
* @var array
*/
protected
array
$argumentHandlers
=
[];
/**
* {@inheritdoc}
*/
public
function
parseEntitiesFromText
(
$text
):
array
{
$dom
=
Html
::
load
(
$text
);
$xpath
=
new
\DOMXPath
(
$dom
);
$entities
=
[];
/** @var \DOMElement $node */
foreach
(
$xpath
->
query
(
'//drupal-view[@data-arguments]'
)
as
$node
)
{
// Skip elements with empty data-arguments attributes.
$argumentValues
=
explode
(
'/'
,
$node
->
getAttribute
(
'data-arguments'
));
if
(
empty
(
$argumentValues
))
{
continue
;
}
$viewId
=
$node
->
getAttribute
(
'data-view-id'
);
$displayId
=
$node
->
getAttribute
(
'data-display-id'
);
$handlers
=
$this
->
getArgumentHandlers
(
$viewId
,
$displayId
);
foreach
(
$handlers
as
$index
=>
$handler
)
{
if
(
!
empty
(
$argumentValues
[
$index
])
&&
(
$entityType
=
$this
->
getEntityTargetFromHandler
(
$handler
))
)
{
try
{
$entityStorage
=
$this
->
entityTypeManager
->
getStorage
(
$entityType
);
$entity
=
$entityStorage
->
load
(
$argumentValues
[
$index
]);
$entities
[
$entity
->
uuid
()]
=
$entityType
;
}
catch
(
\Exception
$e
)
{
// Possibility that referenced argument does not exist so continue.
continue
;
}
}
}
}
return
$entities
;
}
/**
* Gets the argument handlers for a view display.
*
* Loading the handlers so we have the full access to the definition which for
* entity references will contain the target_entity_type_id.
*
* @param string $viewId
* View ID.
* @param string $displayId
* View display ID.
*
* @return \Drupal\views\Plugin\views\ViewsHandlerInterface[]
* View argument handlers for the given view display.
*/
protected
function
getArgumentHandlers
(
string
$viewId
,
string
$displayId
):
array
{
if
(
!
isset
(
$this
->
argumentHandlers
[
$viewId
][
$displayId
]))
{
$this
->
argumentHandlers
[
$viewId
][
$displayId
]
=
[];
// Ensure view exists.
if
((
$view
=
Views
::
getView
(
$viewId
)))
{
// Ensure display exists.
$view
->
initDisplay
();
if
(
$view
->
displayHandlers
->
has
(
$displayId
))
{
$view
->
setDisplay
(
$displayId
);
$arguments
=
$view
->
display_handler
->
getOption
(
'arguments'
)
?:
[];
$argumentHandlerManager
=
Views
::
handlerManager
(
'argument'
);
$this
->
argumentHandlers
[
$viewId
][
$displayId
]
=
array_map
([
$argumentHandlerManager
,
'getHandler'
],
array_values
(
$arguments
));
}
}
}
return
$this
->
argumentHandlers
[
$viewId
][
$displayId
];
}
/**
* Get the entity type id of the target from a handler.
*
* @param \Drupal\views\Plugin\views\ViewsHandlerInterface $handler
* The argument handler.
*
* @return string|null
* The entity type id if determined, or NULL.
*/
protected
function
getEntityTargetFromHandler
(
ViewsHandlerInterface
$handler
):
?string
{
if
(
$handler
instanceof
EntityReferenceArgument
)
{
return
$handler
->
definition
[
'target_entity_type_id'
];
}
// Taxonomy arguments do not subclass EntityReferenceArgument and must be
// handled separately.
if
(
$handler
instanceof
IndexTid
||
$handler
instanceof
IndexTidDepth
||
$handler
instanceof
IndexTidDepthModifier
)
{
return
'taxonomy_term'
;
}
return
NULL
;
}
}
Loading