Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gorkdesign/a1c84f83ca4dcacbc1c3f185e5a57667 to your computer and use it in GitHub Desktop.
Save gorkdesign/a1c84f83ca4dcacbc1c3f185e5a57667 to your computer and use it in GitHub Desktop.

Revisions

  1. @edwardchan edwardchan created this gist Mar 10, 2017.
    6 changes: 6 additions & 0 deletions modal_form_example_modal_form_example.info.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    name: Modal Form Example
    type: module
    description: 'Modal Form Example module'
    package: Example
    version: VERSION
    core: 8.x
    17 changes: 17 additions & 0 deletions modal_form_example_modal_form_example.routing.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    modal_form_example.form:
    path: '/admin/config/example_form'
    defaults:
    _form: 'Drupal\modal_form_example\Form\ExampleForm'
    _title: 'Example Form'
    requirements:
    _permission: 'administer site configuration'

    modal_form_example.open_modal_form:
    path: '/admin/config/modal_form'
    defaults:
    _title: 'Modal Form'
    _controller: '\Drupal\modal_form_example\Controller\ModalFormExampleController::openModalForm'
    requirements:
    _permission: 'administer site configuration'
    options:
    _admin_route: TRUE
    62 changes: 62 additions & 0 deletions modal_form_example_src_Controller_ModalFormExampleController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?php

    namespace Drupal\modal_form_example\Controller;

    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Drupal\Core\Ajax\AjaxResponse;
    use Drupal\Core\Ajax\OpenModalDialogCommand;
    use Drupal\Core\Controller\ControllerBase;
    use Drupal\Core\Form\FormBuilder;

    /**
    * ModalFormExampleController class.
    */
    class ModalFormExampleController extends ControllerBase {

    /**
    * The form builder.
    *
    * @var \Drupal\Core\Form\FormBuilder
    */
    protected $formBuilder;

    /**
    * The ModalFormExampleController constructor.
    *
    * @param \Drupal\Core\Form\FormBuilder $formBuilder
    * The form builder.
    */
    public function __construct(FormBuilder $formBuilder) {
    $this->formBuilder = $formBuilder;
    }

    /**
    * {@inheritdoc}
    *
    * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
    * The Drupal service container.
    *
    * @return static
    */
    public static function create(ContainerInterface $container) {
    return new static(
    $container->get('form_builder')
    );
    }

    /**
    * Callback for opening the modal form.
    */
    public function openModalForm() {
    $response = new AjaxResponse();

    // Get the modal form using the form builder.
    $modal_form = $this->formBuilder->getForm('Drupal\modal_form_example\Form\ModalForm');

    // Add an AJAX command to open a modal dialog with the form as the content.
    $response->addCommand(new OpenModalDialogCommand('My Modal Form', $modal_form, ['width' => '800']));

    return $response;
    }

    }
    59 changes: 59 additions & 0 deletions modal_form_example_src_Form_ExampleForm.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <?php

    namespace Drupal\modal_form_example\Form;

    use Drupal\Core\Form\FormBase;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\Url;

    /**
    * ExampleForm class.
    */
    class ExampleForm extends FormBase {

    /**
    * {@inheritdoc}
    */
    public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
    $form['open_modal'] = [
    '#type' => 'link',
    '#title' => $this->t('Open Modal'),
    '#url' => Url::fromRoute('modal_form_example.open_modal_form'),
    '#attributes' => [
    'class' => [
    'use-ajax',
    'button',
    ],
    ],
    ];

    // Attach the library for pop-up dialogs/modals.
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';

    return $form;
    }

    /**
    * {@inheritdoc}
    */
    public function submitForm(array &$form, FormStateInterface $form_state) {}

    /**
    * {@inheritdoc}
    */
    public function getFormId() {
    return 'modal_form_example_form';
    }

    /**
    * Gets the configuration names that will be editable.
    *
    * @return array
    * An array of configuration object names that are editable if called in
    * conjunction with the trait's config() method.
    */
    protected function getEditableConfigNames() {
    return ['config.modal_form_example_form'];
    }

    }
    103 changes: 103 additions & 0 deletions modal_form_example_src_Form_ModalForm.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    <?php

    namespace Drupal\modal_form_example\Form;

    use Drupal\Core\Form\FormBase;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\Ajax\AjaxResponse;
    use Drupal\Core\Ajax\OpenModalDialogCommand;
    use Drupal\Core\Ajax\HtmlCommand;
    use Drupal\Core\Ajax\ReplaceCommand;

    /**
    * SendToDestinationsForm class.
    */
    class ModalForm extends FormBase {

    /**
    * {@inheritdoc}
    */
    public function getFormId() {
    return 'modal_form_example_modal_form';
    }

    /**
    * {@inheritdoc}
    */
    public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
    $form['#prefix'] = '<div id="modal_example_form">';
    $form['#suffix'] = '</div>';

    // The status messages that will contain any form errors.
    $form['status_messages'] = [
    '#type' => 'status_messages',
    '#weight' => -10,
    ];

    // A required checkboxes field.
    $form['select'] = [
    '#type' => 'checkboxes',
    '#title' => $this->t('Select Destination(s)'),
    '#options' => ['random_value' => 'Some random value'],
    '#required' => TRUE,
    ];

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['send'] = [
    '#type' => 'submit',
    '#value' => $this->t('Submit modal form'),
    '#attributes' => [
    'class' => [
    'use-ajax',
    ],
    ],
    '#ajax' => [
    'callback' => [$this, 'submitModalFormAjax'],
    'event' => 'click',
    ],
    ];

    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';

    return $form;
    }

    /**
    * AJAX callback handler that displays any errors or a success message.
    */
    public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    // If there are any form errors, AJAX replace the form.
    if ($form_state->hasAnyErrors()) {
    $response->addCommand(new ReplaceCommand('#modal_example_form', $form));
    }
    else {
    $response->addCommand(new OpenModalDialogCommand("Success!", 'The modal form has been submitted.', ['width' => 700]));
    }

    return $response;
    }

    /**
    * {@inheritdoc}
    */
    public function validateForm(array &$form, FormStateInterface $form_state) {}

    /**
    * {@inheritdoc}
    */
    public function submitForm(array &$form, FormStateInterface $form_state) {}

    /**
    * Gets the configuration names that will be editable.
    *
    * @return array
    * An array of configuration object names that are editable if called in
    * conjunction with the trait's config() method.
    */
    protected function getEditableConfigNames() {
    return ['config.modal_form_example_modal_form'];
    }

    }