public function FieldCollectionEditableFormatter::viewElements in Field collection 8
Same name and namespace in other branches
- 8.3 src/Plugin/Field/FieldFormatter/FieldCollectionEditableFormatter.php \Drupal\field_collection\Plugin\Field\FieldFormatter\FieldCollectionEditableFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ FieldCollectionEditableFormatter.php, line 22
Class
- FieldCollectionEditableFormatter
- Plugin implementation of the 'field_collection_editable' formatter.
Namespace
Drupal\field_collection\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$count = 0;
// TODO: Is there a better way to get an accurate count of the
// items from the FileItemList that doesn't count blank items?
$render_items = [];
foreach ($items as $delta => $item) {
if ($item->value !== NULL) {
$count++;
$to_render = \Drupal::entityTypeManager()
->getViewBuilder('field_collection_item')
->view($item
->getFieldCollectionItem());
$to_render['#suffix'] = $this
->getEditLinks($item);
$builder = $to_render['#pre_render'][0][0];
unset($to_render['#pre_render']);
$render_items[] = $builder
->build($to_render);
}
}
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
if ($cardinality == -1 || $count < $cardinality) {
$render_items['#suffix'] = '<ul class="action-links action-links-field-collection-add"><li>';
$render_items['#suffix'] .= $this
->getAddLink($items
->getEntity());
$render_items['#suffix'] .= '</li></ul>';
}
return $render_items;
}