Closed (fixed)
Project:
Custom Elements
Version:
8.x-2.x-dev
Component:
Code
Priority:
Critical
Category:
Task
Assigned:
Reporter:
Created:
4 Apr 2022 at 11:19 UTC
Updated:
1 Jul 2022 at 13:24 UTC
Jump to comment: Most recent
We need to respect field-access for security and provide a good way to do it easily for custom-code that wants to access specific fields.
before:
/**
* {@inheritdoc}
*/
public function addtoElement($paragraph, CustomElement $element, $viewMode) {
assert($paragraph instanceof ParagraphInterface);
// Always add a title attribute if field_title is there.
if (isset($paragraph->field_title) && $paragraph->field_title->value) {
$element->setAttribute('title', $paragraph->field_title->value);
}
/** @var \Drupal\media_entity\Entity\Media $media_entity */
$media_entity = $paragraph->field_image->entity;
if (!$media_entity) {
return;
}
$element->setAttribute('src', $media_entity->field_image->entity->uri->url);
if (!$media_entity->field_copyright->isEmpty()) {
$element->setAttribute('copyright', $media_entity->field_copyright->value);
}
if (!$media_entity->field_source->isEmpty()) {
$element->setAttribute('source', $media_entity->field_source->value);
}
if (!$media_entity->field_description->isEmpty()) {
$element->setAttribute('caption', $media_entity->field_description->processed);
}
}
after, maybe something like:
/**
* {@inheritdoc}
*/
public function addtoElement($paragraph, CustomElement $element, $viewMode) {
....
if ($element->hasAccess($media_entity->field_copyright->access('view', NULL, TRUE)) && !$media_entity->field_copyright->isEmpty()) {
$element->setAttribute('copyright', $media_entity->field_copyright->value);
}
....
}
* Figure out a good solution and implement
-
only additions
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
fagomaybe we should add something like
CustomElementsFieldUtilsTraitwhich we could put on the process or class and that one could have helpers that do the access checking for fields + the checking whtether is empty, e.g. ::fieldIsViewable() ?Comment #5
fagothanks for the PR. The cleanup stuff looks mostly good, maybe it would make sense to the clean-up as a separate issue/PR first?
Anyway, on the topic: I don't like the proposed helper for the following reason:
* it hard-codes how values are assigned. When this needs to be customized, you cannot use the helper and are back on your own again
* it's not helpful for entity references
* it hides important details from the processor - I don't see the values being assigned anymore
* the Custom-Elements class is currently decoupled from entities - I'd prefer to keep it like that
I think the helper should focus on the access checking and leave the value-assignement part. Maybe something like that:
Comment #6
vasikeComment #8
fagosry for the long wait here, reviewed now!
Comment #9
vasikeMR requested ... back to Needs review
Comment #10
fagoThx, for changes. this seems mostly good, but did not do a fully review yet. Maybe volodymyr can help out here.
Comment #14
mostepaniukvmFinished changes here and merged. Will tag new release soon