You are here

public function ContentEmbedBlock::blockForm in Content Browser 8

Overrides BlockPluginTrait::blockForm

File

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

Class

ContentEmbedBlock
Provides the "Content Embed" block.

Namespace

Drupal\content_browser\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {

  // This method receives a sub form state instead of the full form state.
  // There is an ongoing discussion around this which could result in the
  // passed form state going back to a full form state. In order to prevent
  // future breakage because of a core update we'll just check which type of
  // FormStateInterface we've been passed and act accordingly.
  // @See https://www.drupal.org/node/2798261
  if ($form_state instanceof SubformStateInterface) {
    $form_state = $form_state
      ->getCompleteFormState();
  }
  $entities = $form_state
    ->getValue([
    'settings',
    'selection',
    'nids',
    'entities',
  ], []);
  $nids = [];
  foreach ($entities as $entity) {
    $nids[] = $entity
      ->id();
  }
  if (empty($nids)) {
    $nids = $this
      ->getDefaultNids();
  }
  $form['selection'] = $this
    ->browserForm($nids);

  /** @var \Drupal\Core\Entity\EntityDisplayRepository $entity_display_repository */
  $entity_display_repository = \Drupal::service('entity_display.repository');
  $view_mode_options = $entity_display_repository
    ->getViewModeOptions('node');
  $form['view_mode'] = [
    '#type' => 'select',
    '#options' => $view_mode_options,
    '#title' => t('View mode'),
    '#default_value' => $this->configuration['view_mode'],
  ];
  return $form;
}