You are here

public static function EntityBrowserBlock::processTable in Entity Browser Block 8

Render API callback: Processes the table element.

File

src/Plugin/Block/EntityBrowserBlock.php, line 151

Class

EntityBrowserBlock
Defines a generic entity browser block type.

Namespace

Drupal\entity_browser_block\Plugin\Block

Code

public static function processTable(&$element, FormStateInterface $form_state, &$complete_form) {
  $parents = array_slice($element['#array_parents'], -3, 2);
  $entity_ids = $form_state
    ->getValue(array_merge($parents, [
    'entity_browser',
    'entity_ids',
  ]), '');
  $entities = empty($entity_ids) ? [] : self::loadEntitiesByIDs(explode(' ', $entity_ids));
  $display_repository = \Drupal::service('entity_display.repository');
  $delta = 0;
  foreach ($entities as $id => $entity) {
    $element[$id] = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
        'data-entity-id' => $id,
      ],
      'title' => [
        '#markup' => $entity
          ->label(),
      ],
      'view_mode' => [
        '#type' => 'select',
        '#options' => $display_repository
          ->getViewModeOptionsByBundle($entity
          ->getEntityTypeId(), $entity
          ->bundle()),
      ],
      'operations' => [
        'remove' => [
          '#type' => 'button',
          '#value' => t('Remove'),
          '#op' => 'remove',
          '#name' => 'remove_' . $id,
          '#ajax' => [
            'callback' => [
              self::class,
              'updateCallback',
            ],
            'wrapper' => 'entity-browser-block-form',
          ],
        ],
      ],
      '_weight' => [
        '#type' => 'weight',
        '#title' => t('Weight for row @number', [
          '@number' => $delta + 1,
        ]),
        '#title_display' => 'invisible',
        '#delta' => count($entities),
        '#default_value' => $delta,
        '#attributes' => [
          'class' => [
            'entity-browser-block-delta-order',
          ],
        ],
      ],
    ];
    if (isset($element['#default_view_modes'][$id])) {
      $element[$id]['view_mode']['#default_value'] = $element['#default_view_modes'][$id];
    }
    $delta++;
  }
  return $element;
}