You are here

public function ModalForm::buildForm in Editor Notes 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/ModalForm.php, line 110

Class

ModalForm
Implements the ModalForm form controller.

Namespace

Drupal\editor_note\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, EditorNote $editor_note = NULL, $nojs = NULL) {

  // Add the core AJAX library.
  $form['#attached']['library'][] = 'core/drupal.ajax';

  // Add a link to show this form in a modal dialog if we're not already in
  // one.
  if ($nojs == 'nojs') {
    $form['use_ajax_container'] = [
      '#type' => 'details',
      '#open' => TRUE,
    ];
    $form['use_ajax_container']['description'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t('In order to show a modal dialog by clicking on a link, that link has to have class <code>use-ajax</code> and <code>data-dialog-type="modal"</code>. This link has those attributes.'),
    ];
    $form['use_ajax_container']['use_ajax'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Edit in modal.'),
      '#url' => Url::fromRoute('editor_note.modal_form', [
        'editor_note' => $editor_note
          ->id(),
        'nojs' => 'ajax',
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode(static::getDataDialogOptions()),
        // Add this id so that we can test this form.
        'id' => 'ajax-example-modal-link',
      ],
    ];
    $form['use_ajax_container']['delete'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Delete in modal.'),
      '#url' => Url::fromRoute('editor_note.confirm_delete_editor_note_form', [
        'nojs' => 'ajax',
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => json_encode(static::getDataDialogOptions()),
        // Add this id so that we can test this form.
        'id' => 'ajax-example-modal-link',
      ],
    ];
  }

  // This element is responsible for displaying form errors in the AJAX
  // dialog.
  if ($nojs == 'ajax') {
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -999,
    ];
  }
  $note_value = $editor_note->note->value;
  $user_input = $form_state
    ->getUserInput();
  if (!empty($user_input['editor_note'])) {
    $note_input = $user_input['editor_note'];
    if (isset($note_input['value'])) {
      $note_value = $note_input['value'];
    }
  }
  $form['editor_note'] = [
    '#type' => 'textarea',
    '#default_value' => $note_value,
    '#required' => TRUE,
    '#title' => $this
      ->t('Update note'),
    '#weight' => '0',
  ];

  // Group submit handlers in an actions element with a key of "actions" so
  // that it gets styled correctly, and so that other modules may add actions
  // to the form.
  $form['actions'] = [
    '#type' => 'actions',
  ];

  // Add a submit button that handles the submission of the form.
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Update'),
    '#ajax' => [
      'callback' => '::ajaxSubmitForm',
      'event' => 'click',
    ],
  ];

  // Set the form to not use AJAX if we're on a nojs path. When this form is
  // within the modal dialog, Drupal will make sure we're using an AJAX path
  // instead of a nojs one.
  if ($nojs == 'nojs') {
    unset($form['actions']['submit']['#ajax']);
  }
  return $form;
}