You are here

public function GeysirModalParagraphAddSelectTypeForm::buildForm in Geysir 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/GeysirModalParagraphAddSelectTypeForm.php, line 24

Class

GeysirModalParagraphAddSelectTypeForm
Functionality to select a paragraph type.

Namespace

Drupal\geysir\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#prefix'] = '<div id="geysir-modal-form">';
  $form['#suffix'] = '</div>';
  $routeParams = $form_state
    ->getBuildInfo()['args'][0];
  $bundles = $form_state
    ->getBuildInfo()['args'][1];
  $parent_entity_type = $routeParams['parent_entity_type'];
  $parent_entity_bundle = $routeParams['parent_entity_bundle'];
  $form_mode = 'default';
  $field = $routeParams['field'];
  $parent_field_settings = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load($parent_entity_type . '.' . $parent_entity_bundle . '.' . $form_mode)
    ->getComponent($field);
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $bundles = $this
    ->getAllowedBundles($bundles);
  $paragraphs_type_storage = \Drupal::entityTypeManager()
    ->getStorage('paragraphs_type');
  $default_icon = drupal_get_path('module', 'geysir') . '/images/geysir-puzzle.svg';
  foreach ($bundles as $bundle => $label) {
    $icon_url = $default_icon;
    if ($paragraphs_type_storage
      ->load($bundle)
      ->getIconUrl()) {
      $icon_url = $paragraphs_type_storage
        ->load($bundle)
        ->getIconUrl();
    }
    $routeParams['bundle'] = $bundle;
    $form['description'][$bundle] = [
      '#type' => 'image_button',
      '#prefix' => '<div class="geysir-add-type">',
      '#suffix' => '<span>' . $label . '</span></div>',
      '#src' => $icon_url,
      '#value' => $label,
      '#ajax' => [
        'url' => Url::fromRoute(isset($routeParams['paragraph']) ? 'geysir.modal.add_form' : 'geysir.modal.add_form_first', $routeParams),
        'wrapper' => 'geysir-modal-form',
      ],
    ];
  }
  return $form;
}