GeysirModalParagraphAddSelectTypeForm.php in Geysir 8
File
src/Form/GeysirModalParagraphAddSelectTypeForm.php
View source
<?php
namespace Drupal\geysir\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class GeysirModalParagraphAddSelectTypeForm extends FormBase {
public function getFormId() {
return 'geysir_modal_add_select_type_form';
}
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;
}
protected function getAllowedBundles($allowed_bundles) {
$bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo('paragraph');
if (is_array($allowed_bundles) && count($allowed_bundles)) {
$bundles = array_intersect_key(array_replace($allowed_bundles, $bundles), $allowed_bundles);
}
foreach ($bundles as $bundle => $props) {
$label = empty($props['label']) ? ucfirst($bundle) : $props['label'];
$bundles[$bundle] = $label;
}
return $bundles;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
return [];
}
}