You are here

public function ContentItemBlock::blockForm in Panopoly Widgets 8.2

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/ContentItemBlock.php, line 103

Class

ContentItemBlock
Block that displays a node.

Namespace

Drupal\panopoly_widgets\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $entity = $this
    ->loadEntity();
  $content_types = $this
    ->getContentTypes();
  $form['#attached'] = [
    'library' => [
      'panopoly_widgets/content-item',
    ],
  ];
  $form['content_type'] = [
    '#type' => 'select',
    '#options' => array_merge([
      'all' => 'Any',
    ], $content_types),
    '#title' => $this
      ->t('Content type'),
    '#default_value' => $entity ? $entity
      ->bundle() : 'all',
    '#ajax' => [
      'callback' => [
        $this,
        'autocompleteGetNodes',
      ],
    ],
  ];
  $form['node'] = [
    '#prefix' => '<div id="node-selector-wrapper">',
    '#type' => 'entity_autocomplete',
    '#title' => $this
      ->t('Piece of content'),
    '#target_type' => 'node',
    '#default_value' => $entity,
    '#required' => TRUE,
    // @todo Properly update this to any AJAX value for `content_type`.
    //   There are some complications as $form_state->getValues() breaks due
    //   to Layout Builder leveraging subform states. This requires us to
    //   use a #process callback, but that does not seem to effect the
    //   selection settings passed to the autocomplete, since AJAX was
    //   triggered by a regular element and not a button. Without a button
    //   triggering the rebuild, these changes are not respected.
    //
    //   This element needs to be rebuild with a new selection settings key in
    //   its autocomplete-path property.
    '#selection_settings' => [
      'target_bundles' => array_keys($content_types),
    ],
    '#suffix' => '</div>',
    '#attributes' => [
      'class' => [
        'js-panopoly-widgets-content-item-autocomplete',
      ],
    ],
  ];
  $form['view_mode'] = [
    '#type' => 'radios',
    '#options' => $this->entityDisplayRepository
      ->getViewModeOptions('node'),
    '#title' => $this
      ->t('View mode'),
    '#default_value' => $this->configuration['view_mode'],
  ];
  return $form;
}