You are here

public function ContentEmbedBlock::browserForm in Content Browser 8

Constructs parts of the form needed to use Entity Browser.

Parameters

array $nids: An array of Node IDs.

Return value

array A render array representing Entity Browser components.

1 call to ContentEmbedBlock::browserForm()
ContentEmbedBlock::blockForm in src/Plugin/Block/ContentEmbedBlock.php

File

src/Plugin/Block/ContentEmbedBlock.php, line 100

Class

ContentEmbedBlock
Provides the "Content Embed" block.

Namespace

Drupal\content_browser\Plugin\Block

Code

public function browserForm(array $nids) {
  $selection = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'content-embed-block-browser',
    ],
  ];
  $selection['nids'] = [
    '#type' => 'entity_browser',
    '#entity_browser' => 'browse_content',
    '#entity_browser_validators' => [
      'entity_type' => [
        'type' => 'node',
      ],
    ],
    '#process' => [
      [
        '\\Drupal\\entity_browser\\Element\\EntityBrowserElement',
        'processEntityBrowser',
      ],
      [
        get_called_class(),
        'processEntityBrowser',
      ],
    ],
  ];
  $order_class = 'content-embed-block-delta-order';
  $selection['table'] = [
    '#type' => 'table',
    '#header' => [
      t('Title'),
      t('Type'),
      t('Order', [], [
        'context' => 'Sort order',
      ]),
    ],
    '#empty' => t('No content yet'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => $order_class,
      ],
    ],
  ];
  $delta = 0;
  $bundle_info = \Drupal::service('entity_type.bundle.info')
    ->getBundleInfo('node');
  foreach ($nids as $nid) {

    /** @var \Drupal\node\Entity\Node $node */
    $node = Node::load($nid);
    $selection['table'][$nid] = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
        'data-entity-id' => $node
          ->getEntityTypeId() . ':' . $nid,
      ],
      'title' => [
        '#markup' => $node
          ->label(),
      ],
      'type' => [
        '#markup' => $bundle_info[$node
          ->bundle()]['label'],
      ],
      '_weight' => [
        '#type' => 'weight',
        '#title' => t('Weight for row @number', [
          '@number' => $delta + 1,
        ]),
        '#title_display' => 'invisible',
        '#delta' => count($nids),
        '#default_value' => $delta,
        '#attributes' => [
          'class' => [
            $order_class,
          ],
        ],
      ],
    ];
    $delta++;
  }
  return $selection;
}