You are here

protected function EntityReferenceDragDropWidget::optionsToRenderableArray in Entity Reference Drag & Drop 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/EntityReferenceDragDropWidget.php \Drupal\entityreference_dragdrop\Plugin\Field\FieldWidget\EntityReferenceDragDropWidget::optionsToRenderableArray()

Converts list of options to renderable array.

Parameters

array $options:

$key:

$list_title:

array $classes:

array $wrapper_classes:

Return value

array

2 calls to EntityReferenceDragDropWidget::optionsToRenderableArray()
EntityReferenceDragDropWidget::availableOptionsToRenderableArray in src/Plugin/Field/FieldWidget/EntityReferenceDragDropWidget.php
Converts list of available options to renderable array.
EntityReferenceDragDropWidget::selectedOptionsToRenderableArray in src/Plugin/Field/FieldWidget/EntityReferenceDragDropWidget.php
Converts list of selected options to renderable array.

File

src/Plugin/Field/FieldWidget/EntityReferenceDragDropWidget.php, line 249

Class

EntityReferenceDragDropWidget
Plugin implementation of the 'entityreference_dragdrop' widget.

Namespace

Drupal\entityreference_dragdrop\Plugin\Field\FieldWidget

Code

protected function optionsToRenderableArray(array $options, $key, $list_title, array $classes = [], array $wrapper_classes = []) {

  // view mode is of the form 'node.full_content'
  $view_mode_name = $this
    ->getSetting('view_mode');
  $view_mode_name = explode('.', $view_mode_name);
  $view_mode = end($view_mode_name);
  $target_type_id = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getSetting('target_type');
  $items = [];
  $entities = [];
  if ($view_mode !== static::VIEW_MODE_TITLE) {
    $entities = $this->entityTypeManager
      ->getStorage($target_type_id)
      ->loadMultiple(array_keys($options));
  }
  foreach ($options as $id => $entity_title) {
    $item = [
      '#wrapper_attributes' => [
        'data-key' => $key,
        'data-id' => $id,
        'data-label' => $entity_title,
      ],
    ];
    if ($view_mode !== static::VIEW_MODE_TITLE) {
      $item += $this->entityTypeManager
        ->getViewBuilder($target_type_id)
        ->view($entities[$id], $view_mode);
    }
    else {
      $item += [
        '#markup' => $options[$id],
      ];
    }
    $items[] = $item;
  }
  return [
    '#theme' => 'entityreference_dragdrop_options_list',
    '#items' => $items,
    '#title' => $list_title,
    '#display_filter' => $this
      ->getSetting('display_filter'),
    '#attributes' => [
      'data-key' => $key,
      'class' => array_merge([
        'entityreference-dragdrop',
      ], $classes),
    ],
    '#wrapper_attributes' => [
      'class' => array_merge([
        'entityreference-dragdrop-container',
      ], $wrapper_classes),
    ],
  ];
}