You are here

public function WebformImageSelectImagesForm::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_image_select/src/WebformImageSelectImagesForm.php \Drupal\webform_image_select\WebformImageSelectImagesForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

modules/webform_image_select/src/WebformImageSelectImagesForm.php, line 71

Class

WebformImageSelectImagesForm
Provides a form to set webform image select images.

Namespace

Drupal\webform_image_select

Code

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

  /** @var \Drupal\webform_image_select\WebformImageSelectImagesInterface $webform_images */
  $webform_images = $this->entity;

  /** @var \Drupal\webform_image_select\WebformImageSelectImagesStorageInterface $webform_image_select_images_storage */
  $webform_image_select_images_storage = $this->entityTypeManager
    ->getStorage('webform_image_select_images');
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#attributes' => $webform_images
      ->isNew() ? [
      'autofocus' => 'autofocus',
    ] : [],
    '#default_value' => $webform_images
      ->label(),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => '\\Drupal\\webform_image_select\\Entity\\WebformImageSelectImages::load',
      'label' => '<br/>' . $this
        ->t('Machine name'),
    ],
    '#maxlength' => 32,
    '#field_suffix' => $webform_images
      ->isNew() ? ' (' . $this
      ->t('Maximum @max characters', [
      '@max' => 32,
    ]) . ')' : '',
    '#required' => TRUE,
    '#disabled' => !$webform_images
      ->isNew(),
    '#default_value' => $webform_images
      ->id(),
  ];
  $form['category'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Category'),
    '#options' => $webform_image_select_images_storage
      ->getCategories(),
    '#empty_option' => $this
      ->t('- None -'),
    '#default_value' => $webform_images
      ->get('category'),
  ];
  if ($this
    ->getRouteMatch()
    ->getRouteName() === 'entity.webform_image_select_images.source_form') {
    $form['images'] = [
      '#type' => 'webform_codemirror',
      '#mode' => 'yaml',
      '#title' => $this
        ->t('Images (YAML)'),
      '#attributes' => [
        'style' => 'min-height: 200px',
      ],
      '#default_value' => $this
        ->getImages(),
    ];
  }
  else {
    $form['images'] = [
      '#type' => 'webform_image_select_images',
      '#title' => $this
        ->t('Images'),
      '#title_display' => 'invisible',
      '#empty_options' => 10,
      '#add_more_items' => 10,
      '#default_value' => $this
        ->getImages(),
    ];
  }

  // Display message if images are altered.
  if (!$webform_images
    ->isNew()) {
    $hook_name = 'webform_image_select_images_' . $webform_images
      ->id() . '_alter';
    $alter_hooks = $this->moduleHandler
      ->getImplementations($hook_name);
    $module_info = $this->moduleExtensionList
      ->getAllInstalledInfo();
    $module_names = [];
    foreach ($alter_hooks as $options_alter_hook) {
      $module_name = str_replace($hook_name, '', $options_alter_hook);
      $module_names[] = $module_info[$module_name]['name'];
    }
    if (count($module_names) && !$form_state
      ->getUserInput()) {
      $t_args = [
        '%title' => $webform_images
          ->label(),
        '%module_names' => WebformArrayHelper::toString($module_names),
        '@module' => new PluralTranslatableMarkup(count($module_names), $this
          ->t('module'), $this
          ->t('modules')),
      ];
      $this
        ->messenger()
        ->addWarning($this
        ->t('The %title images are being altered by the %module_names @module.', $t_args));
    }
  }
  return parent::form($form, $form_state);
}