FieldCollectionEditableFormatter.php in Field collection 8.3
File
src/Plugin/Field/FieldFormatter/FieldCollectionEditableFormatter.php
View source
<?php
namespace Drupal\field_collection\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
class FieldCollectionEditableFormatter extends FieldCollectionLinksFormatter {
public function viewElements(FieldItemListInterface $items, $langcode) {
$count = 0;
$render_items = [];
foreach ($items as $item) {
if ($item->target_id !== 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;
}
}