public function FieldCollectionListFormatter::viewElements in Field collection 8
Same name and namespace in other branches
- 8.3 src/Plugin/Field/FieldFormatter/FieldCollectionListFormatter.php \Drupal\field_collection\Plugin\Field\FieldFormatter\FieldCollectionListFormatter::viewElements()
TODO: Use $langcode.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ FieldCollectionListFormatter.php, line 27
Class
- FieldCollectionListFormatter
- Plugin implementation of the 'field_collection_list' formatter.
Namespace
Drupal\field_collection\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$settings = $this
->getFieldSettings();
$count = 0;
// TODO: Is there a better way to get an accurate count of the
// items from the FieldItemList that doesn't count blank items?
// Possibly \Countable->count()?
$storage = \Drupal::entityTypeManager()
->getStorage('field_collection_item');
foreach ($items as $delta => $item) {
if ($item->value !== NULL) {
$count++;
$field_collection_item = $storage
->loadRevision($item->revision_id);
if ($field_collection_item
->isDefaultRevision()) {
$links = Link::fromTextAndUrl($this->fieldDefinition
->getName() . ' ' . $delta, Url::FromRoute('entity.field_collection_item.canonical', [
'field_collection_item' => $item->value,
]))
->toString();
$links .= ' ' . $this
->getEditLinks($item);
}
else {
$links = Link::fromTextAndUrl($this->fieldDefinition
->getName() . ' ' . $delta, Url::FromRoute('field_collection_item.revision_show', [
'field_collection_item' => $item->value,
'field_collection_item_revision' => $item->revision_id,
]))
->toString();
}
$element[$delta] = [
'#markup' => $links,
];
}
}
$cardinality = $this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality();
$entity = $items
->getEntity();
if ($entity
->id() && ($cardinality == -1 || $count < $cardinality)) {
$element['#suffix'] = '<ul class="action-links action-links-field-collection-add"><li>';
$element['#suffix'] .= $this
->getAddLink($entity);
$element['#suffix'] .= '</li></ul>';
}
return $element;
}