EntityReferenceRenderedActive.php in Entityreference Rendered Widget 8
File
src/Plugin/Field/FieldWidget/EntityReferenceRenderedActive.php
View source
<?php
namespace Drupal\entityreference_rendered_widget\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class EntityReferenceRenderedActive extends EntityReferenceRenderedBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$target_ids = array_keys($this
->getOptions($items
->getEntity()));
$targets = $this->entityTypeManager
->getStorage($this->targetEntityType)
->loadMultiple($target_ids);
$view_builder = $this->entityTypeManager
->getViewBuilder($this->targetEntityType);
foreach ($targets as $target) {
$gen_view = $view_builder
->view($target, $this
->getSetting('display_mode'), $target
->language()
->getId());
$element['#options'][$target
->id()] = render($gen_view);
}
return $element;
}
}