Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
computed_field
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
computed_field
Merge requests
!20
Add a views field handle rthat is NOT broken and will handle all computed fields for nodes
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Add a views field handle rthat is NOT broken and will handle all computed fields for nodes
issue/computed_field-3349082:3349082-access-computed-field
into
4.0.x
Overview
0
Commits
6
Pipelines
2
Changes
3
Open
Benjamin Melançon
requested to merge
issue/computed_field-3349082:3349082-access-computed-field
into
4.0.x
2 weeks ago
Overview
0
Commits
6
Pipelines
2
Changes
3
Expand
Closes
#3349082
0
0
Merge request reports
Compare
4.0.x
version 1
cf0d762a
2 weeks ago
4.0.x (HEAD)
and
latest version
latest version
0a3c7baf
6 commits,
2 weeks ago
version 1
cf0d762a
5 commits,
2 weeks ago
3 files
+
128
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
src/Plugin/views/field/NodeComputedField.php
0 → 100644
+
93
−
0
Options
<?php
/**
* @file
* Definition of Drupal\computed_field\Plugin\views\field\NodeComputedField
*/
namespace
Drupal\computed_field\Plugin\views\field
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\computed_field
\Entity\ComputedField
;
use
Drupal\views\Plugin\views\field\FieldPluginBase
;
use
Drupal\views\ResultRow
;
use
Stringable
;
/**
* Field handler to display a selected computed field.
*
* @ingroup views_field_handlers
*
* @ViewsField("node_computed_field")
*/
class
NodeComputedField
extends
FieldPluginBase
{
/**
* @{inheritdoc}
*/
public
function
query
()
{
// Leave empty to avoid a query on this field.
}
/**
* Define the available options
* @return array
*/
protected
function
defineOptions
()
{
$options
=
parent
::
defineOptions
();
$options
[
'computed_field'
]
=
[
'default'
=>
''
];
return
$options
;
}
/**
* Provide the options form.
*/
public
function
buildOptionsForm
(
&
$form
,
FormStateInterface
$form_state
)
{
/** @var \Drupal\computed_field\ComputedFieldManager $computedFieldManager */
$computedFieldManager
=
\Drupal
::
service
(
'plugin.manager.computed_field'
);
// $entity_type_node = \Drupal::entityTypeManager()->getDefinition('node');
// $automatic_plugins = $computedFieldManager->getAutomaticPlugins($entity_type_node);
$plugins
=
$computedFieldManager
->
getDefinitions
();
$options
=
[];
foreach
(
$plugins
as
$id
=>
$definition
)
{
$options
[
$id
]
=
$definition
[
'label'
];
}
$form
[
'computed_field'
]
=
array
(
'#title'
=>
$this
->
t
(
'Computed field to use'
),
'#type'
=>
'select'
,
'#default_value'
=>
$this
->
options
[
'computed_field'
],
'#options'
=>
$options
,
'#description'
=>
$this
->
t
(
'Computed fields not relevant to the content types available on this view will still be shown here but will not work. No configuration for the computed field is currently possible.'
)
);
parent
::
buildOptionsForm
(
$form
,
$form_state
);
}
/**
* @{inheritdoc}
*/
public
function
render
(
ResultRow
$values
)
{
$node
=
$this
->
getEntity
(
$values
);
$computed_field_plugin_id
=
$this
->
options
[
'computed_field'
];
$computedFieldManager
=
\Drupal
::
service
(
'plugin.manager.computed_field'
);
$instance
=
$computedFieldManager
->
createInstance
(
$computed_field_plugin_id
);
$computed
=
$instance
->
computeValue
(
$node
,
new
ComputedField
([],
'computed_field'
));
$build
=
[];
foreach
(
$computed
as
$index
=>
$value
)
{
if
(
is_string
(
$value
)
||
is_object
(
$value
))
{
// Protect from fatal errors by making sure any object we get is stringable.
if
(
is_object
(
$value
)
&&
!
(
$value
instanceof
Stringable
))
{
continue
;
}
// All strings need to be renderable arrays.
$build
[
$index
]
=
[
'#markup'
=>
$value
];
}
else
{
// It is probably a render array or hopefully somehow renderable.
$build
[
$index
]
=
$value
;
}
}
return
$build
;
}
}
Loading