Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Plugin/Field/FieldFormatter/TypedRelationFormatterRaw.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Drupal\controlled_access_terms\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
use Drupal\Core\Field\FieldItemListInterface;

/**
* Formatter to output raw values, e.g. rel_type=target_id.
*
* @FieldFormatter(
* id = "typed_relation_raw",
* label = @Translation("Typed Relation Formatter (Raw)"),
* field_types = {
* "typed_relation"
* }
* )
*/
class TypedRelationFormatterRaw extends EntityReferenceLabelFormatter {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);

foreach ($items as $delta => $item) {
$elements[$delta]['#plain_text'] = $item->rel_type . '=' . $item->target_id;
}
Comment on lines +27 to +29

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$elements = parent::viewElements($items, $langcode); does entity access checking on the referenced entities. So it's possible an element in $items doesn't exist in $elements and we leak its relator and term ID. Not a big deal, but maybe we should iterate over $elements? Maybe something like

Suggested change
foreach ($items as $delta => $item) {
$elements[$delta]['#plain_text'] = $item->rel_type . '=' . $item->target_id;
}
foreach ($elements as $delta => &$element) {
$item = $items[$delta];
$element['#plain_text'] = $item->rel_type . '=' . $item->target_id;
}


return $elements;
}

}