You are here

public function BlockForm::form in Layout Builder Browser 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/BlockForm.php, line 39

Class

BlockForm
Form handler for the block add and edit forms.

Namespace

Drupal\layout_builder_browser\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $definitions = \Drupal::service('plugin.manager.block')
    ->getFilteredDefinitions('layout_builder', NULL, [
    'list' => 'inline_blocks',
  ]);
  $blocks = [];
  $provider_options = [];
  $block_provider_map = [];
  foreach ($definitions as $id => $definition) {
    if ($definition["category"] instanceof TranslatableMarkup) {
      $catid = $definition["category"]
        ->getUntranslatedString();
      $catlabel = $definition["category"]
        ->render();
    }
    else {
      $catid = $catlabel = $definition["category"];
    }
    $blocks[$catid][$id] = $definition['admin_label'];
    $provider_options[$catid] = $catlabel;
    $block_provider_map[$id] = $catid;
  }
  $browser_block = $this->entity;
  $block_id = $browser_block->block_id;
  $provider = isset($provider_options['Inline blocks']) ? 'Inline blocks' : '';
  if ($block_id) {
    $provider = $block_provider_map[$block_id];
  }
  if ($form_state
    ->getValue('provider')) {
    $provider = $form_state
      ->getValue('provider');
  }
  $form['provider'] = [
    '#title' => $this
      ->t('Provider'),
    '#type' => 'select',
    '#options' => $provider_options,
    '#default_value' => $provider,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::providerChanged',
      'event' => 'change',
      'wrapper' => 'block-id-wrapper',
    ],
  ];
  $form['block_id'] = [
    '#suffix' => '</div>',
    '#prefix' => '<div id="block-id-wrapper">',
    '#title' => $this
      ->t('Block'),
    '#type' => 'select',
    '#options' => $blocks[$provider],
    '#default_value' => $browser_block->block_id,
    '#required' => TRUE,
  ];
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $browser_block
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $browser_block
      ->id(),
    '#machine_name' => [
      'exists' => [
        $this,
        'exist',
      ],
    ],
    '#disabled' => !$browser_block
      ->isNew(),
  ];
  $form['image_path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Image path'),
    '#maxlength' => 255,
    '#default_value' => $browser_block->image_path,
    '#description' => $this
      ->t("Preview image path. E.g. /themes/mycustomtheme/images/lbb/text.jpg"),
  ];
  $form['image_alt'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Image alt'),
    '#maxlength' => 255,
    '#default_value' => $browser_block->image_alt,
  ];
  $blockcat_prefill = \Drupal::request()->query
    ->get('blockcat');
  $block_categories = $this->entityTypeManager
    ->getStorage('layout_builder_browser_blockcat')
    ->loadMultiple();
  uasort($block_categories, [
    'Drupal\\Core\\Config\\Entity\\ConfigEntityBase',
    'sort',
  ]);
  $blockcatoptions = [];
  foreach ($block_categories as $block_category) {
    $blockcatoptions[$block_category->id] = $block_category->label;
  }
  $form['category'] = [
    '#title' => $this
      ->t('Block category'),
    '#type' => 'select',
    '#options' => $blockcatoptions,
    '#default_value' => $blockcat_prefill ?: $browser_block->category,
    '#required' => TRUE,
  ];
  return $form;
}