You are here

public function AddExternalPackageForm::buildForm in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/AddExternalPackageForm.php \Drupal\opigno_module\Form\AddExternalPackageForm::buildForm()

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/AddExternalPackageForm.php, line 26

Class

AddExternalPackageForm
Add External package form.

Namespace

Drupal\opigno_module\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $mode = NULL) {
  $is_ppt = $mode && $mode == 'ppt' ? TRUE : FALSE;
  if ($is_ppt) {
    $form_state
      ->set('mode', $mode);
  }
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#required' => TRUE,
  ];
  $form['package'] = [
    '#title' => $this
      ->t('Package'),
    '#type' => 'file',
    '#description' => !$is_ppt ? $this
      ->t('Here you can upload external package. Allowed extensions: zip h5p') : $this
      ->t('Here you can upload PowerPoint presentation file. Allowed extensions: ppt pptx'),
  ];
  $ajax_id = "ajax-form-entity-external-package";
  $form['#attributes']['class'][] = $ajax_id;
  $form['#attached']['library'][] = 'opigno_module/ajax_form';
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Next'),
    '#ajax' => [
      'callback' => 'Drupal\\opigno_module\\Controller\\ExternalPackageController::ajaxFormExternalPackageCallback',
      'wrapper' => $ajax_id,
      'effect' => 'fade',
    ],
  ];
  $form['actions']['submit']['#submit'][] = 'Drupal\\opigno_module\\Controller\\ExternalPackageController::ajaxFormExternalPackageFormSubmit';
  $form['ajax_form_entity'] = [
    '#type' => 'hidden',
    '#value' => [
      'view_mode' => 'default',
      'reload' => TRUE,
      'content_selector' => ".{$ajax_id}",
      'form_selector' => ".{$ajax_id}",
    ],
  ];
  return $form;
}