You are here

public function EntityReferenceBrowserTableWidget::buildTableRows in Entity Browser - Table Layout 8

1 call to EntityReferenceBrowserTableWidget::buildTableRows()
EntityReferenceBrowserTableWidget::displayCurrentSelection in src/Plugin/Field/FieldWidget/EntityReferenceBrowserTableWidget.php
Builds the render array for displaying the current results as a table.

File

src/Plugin/Field/FieldWidget/EntityReferenceBrowserTableWidget.php, line 196

Class

EntityReferenceBrowserTableWidget
Plugin implementation of the 'entity_reference_browser_table_widget' widget.

Namespace

Drupal\entity_browser_table\Plugin\Field\FieldWidget

Code

public function buildTableRows(array $entities, $details_id, $field_parents) {
  $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'),
  ]);

  // 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;
  $entities = array_filter($entities, function ($entity) {
    return $entity instanceof EntityInterface;
  });
  $rowData = [];
  foreach ($entities as $row_id => $entity) {
    if ($entity
      ->hasTranslation($this->currentLanguage) == TRUE) {
      $entity = $entity
        ->getTranslation($this->currentLanguage);
    }
    $rowData[] = array_filter([
      'handle' => $this
        ->buildSortableHandle(),
      'title-preview' => $this
        ->getFirstColumn($entity),
      'status' => $this
        ->getAdditionalFieldsColumn($entity),
      'actions' => [
        'edit_button' => $this
          ->buildEditButton($entity, $details_id, $row_id, $field_parents),
        'replace_button' => $this
          ->buildReplaceButton($entity, $entities, $details_id, $row_id, $field_parents),
        'remove_button' => $this
          ->buildRemoveButton($entity, $details_id, $row_id, $field_parents),
      ],
      '#attributes' => [
        'class' => [
          'item-container',
          Html::getClass($field_widget_display
            ->getPluginId()),
        ],
        'data-entity-id' => $entity
          ->getEntityTypeId() . ':' . $entity
          ->id(),
        'data-row-id' => $row_id,
      ],
    ]);
  }
  return $rowData;
}