You are here

protected function EntityReferenceBrowserWidget::displayCurrentSelection in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::displayCurrentSelection()

Builds the render array for displaying the current results.

Parameters

string $details_id: The ID for the details element.

string[] $field_parents: Field parents.

\Drupal\Core\Entity\ContentEntityInterface[] $entities: Array of referenced entities.

Return value

array The render array for the current selection.

1 call to EntityReferenceBrowserWidget::displayCurrentSelection()
EntityReferenceBrowserWidget::formElement in src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php
Returns the form for a single field widget.
1 method overrides EntityReferenceBrowserWidget::displayCurrentSelection()
FileBrowserWidget::displayCurrentSelection in src/Plugin/Field/FieldWidget/FileBrowserWidget.php
Builds the render array for displaying the current results.

File

src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php, line 569

Class

EntityReferenceBrowserWidget
Plugin implementation of the 'entity_reference' widget for entity browser.

Namespace

Drupal\entity_browser\Plugin\Field\FieldWidget

Code

protected function displayCurrentSelection($details_id, array $field_parents, array $entities) {
  $target_entity_type = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getSetting('target_type');
  $field_widget_display = $this->fieldDisplayManager
    ->createInstance($this
    ->getSetting('field_widget_display'), $this
    ->getSetting('field_widget_display_settings') + [
    'entity_type' => $this->fieldDefinition
      ->getFieldStorageDefinition()
      ->getSetting('target_type'),
  ]);
  $classes = [
    'entities-list',
    Html::cleanCssIdentifier("entity-type--{$target_entity_type}"),
  ];
  if ($this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality() != 1) {
    $classes[] = 'sortable';
  }

  // The "Replace" button will only be shown if this setting is enabled in the
  // widget, and there is only one entity in the current selection.
  $replace_button_access = $this
    ->getSetting('field_widget_replace') && count($entities) === 1;
  return [
    '#theme_wrappers' => [
      'container',
    ],
    '#attributes' => [
      'class' => $classes,
    ],
    '#prefix' => '<p>' . $this
      ->getCardinalityMessage($entities) . '</p>',
    'items' => array_map(function (ContentEntityInterface $entity, $row_id) use ($field_widget_display, $details_id, $field_parents, $replace_button_access) {
      $display = $field_widget_display
        ->view($entity);
      $edit_button_access = $this
        ->getSetting('field_widget_edit') && $entity
        ->access('update', $this->currentUser);
      if ($entity
        ->getEntityTypeId() == 'file') {

        // On file entities, the "edit" button shouldn't be visible unless
        // the module "file_entity" is present, which will allow them to be
        // edited on their own form.
        $edit_button_access &= $this->moduleHandler
          ->moduleExists('file_entity');
      }
      if (is_string($display)) {
        $display = [
          '#markup' => $display,
        ];
      }
      return [
        '#theme_wrappers' => [
          'container',
        ],
        '#attributes' => [
          'class' => [
            'item-container',
            Html::getClass($field_widget_display
              ->getPluginId()),
          ],
          'data-entity-id' => $entity
            ->getEntityTypeId() . ':' . $entity
            ->id(),
          'data-row-id' => $row_id,
        ],
        'display' => $display,
        'remove_button' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Remove'),
          '#ajax' => [
            'callback' => [
              get_class($this),
              'updateWidgetCallback',
            ],
            'wrapper' => $details_id,
          ],
          '#submit' => [
            [
              get_class($this),
              'removeItemSubmit',
            ],
          ],
          '#name' => $this->fieldDefinition
            ->getName() . '_remove_' . $entity
            ->id() . '_' . $row_id . '_' . md5(json_encode($field_parents)),
          '#limit_validation_errors' => [
            array_merge($field_parents, [
              $this->fieldDefinition
                ->getName(),
            ]),
          ],
          '#attributes' => [
            'data-entity-id' => $entity
              ->getEntityTypeId() . ':' . $entity
              ->id(),
            'data-row-id' => $row_id,
            'class' => [
              'remove-button',
            ],
          ],
          '#access' => (bool) $this
            ->getSetting('field_widget_remove'),
        ],
        'replace_button' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Replace'),
          '#ajax' => [
            'callback' => [
              get_class($this),
              'updateWidgetCallback',
            ],
            'wrapper' => $details_id,
          ],
          '#submit' => [
            [
              get_class($this),
              'removeItemSubmit',
            ],
          ],
          '#name' => $this->fieldDefinition
            ->getName() . '_replace_' . $entity
            ->id() . '_' . $row_id . '_' . md5(json_encode($field_parents)),
          '#limit_validation_errors' => [
            array_merge($field_parents, [
              $this->fieldDefinition
                ->getName(),
            ]),
          ],
          '#attributes' => [
            'data-entity-id' => $entity
              ->getEntityTypeId() . ':' . $entity
              ->id(),
            'data-row-id' => $row_id,
            'class' => [
              'replace-button',
            ],
          ],
          '#access' => $replace_button_access,
        ],
        'edit_button' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Edit'),
          '#name' => $this->fieldDefinition
            ->getName() . '_edit_button_' . $entity
            ->id() . '_' . $row_id . '_' . md5(json_encode($field_parents)),
          '#ajax' => [
            'url' => Url::fromRoute('entity_browser.edit_form', [
              'entity_type' => $entity
                ->getEntityTypeId(),
              'entity' => $entity
                ->id(),
            ]),
            'options' => [
              'query' => [
                'details_id' => $details_id,
              ],
            ],
          ],
          '#attributes' => [
            'class' => [
              'edit-button',
            ],
          ],
          '#access' => $edit_button_access,
        ],
      ];
    }, $entities, empty($entities) ? [] : range(0, count($entities) - 1)),
  ];
}