You are here

ModalForm.php in Modal 5.0.x

File

src/Form/ModalForm.php
View source
<?php

namespace Drupal\modal_page\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class ModalForm.
 */
class ModalForm extends EntityForm {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Construct of Modal Page.
   */
  public function __construct(LanguageManagerInterface $language_manager) {
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('language_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    /* @var \Drupal\modal_page\Entity\Modal $modal */
    $modal = $this->entity;
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Title'),
      '#maxlength' => 255,
      '#default_value' => $modal
        ->label(),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $modal
        ->id(),
      '#machine_name' => [
        'exists' => '\\Drupal\\modal_page\\Entity\\Modal::load',
      ],
      '#disabled' => !$modal
        ->isNew(),
    ];
    $body = '';
    if (!empty($modal
      ->getBody())) {
      $body = $modal
        ->getBody();
    }
    if (!empty($body['value'])) {
      $body = $body['value'];
    }
    $form['body'] = [
      '#title' => $this
        ->t('Body'),
      '#required' => TRUE,
      '#type' => 'text_format',
      '#format' => 'full_html',
      '#default_value' => $body,
    ];
    $form['pages'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Pages'),
      '#format' => 'full_html',
      '#default_value' => $modal
        ->getPages(),
      '#description' => $this
        ->t("One per line. The '*' character is a wildcard. An example path is /admin/* for every admin pages. Leave in blank to show in all pages. <front> is used to front page"),
    ];
    $form['pages']['#states']['visible'][] = [
      ':input[id="edit-type"]' => [
        'value' => 'page',
      ],
    ];
    $form['parameters'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Parameters'),
      '#format' => 'full_html',
      '#default_value' => $modal
        ->getParameters(),
      '#description' => $this
        ->t("Parameters for the Modal appear. One per line. An example path is welcome for show in this parameter. In URL should be /page?modal=welcome"),
    ];
    $form['parameters']['#states']['visible'][] = [
      ':input[id="edit-type"]' => [
        'value' => 'parameter',
      ],
    ];
    $autoOpen = $modal
      ->getAutoOpen();
    if ($modal
      ->isNew()) {
      $autoOpen = TRUE;
    }
    $form['auto_open'] = [
      '#title' => $this
        ->t('Auto Open'),
      '#type' => 'checkbox',
      '#default_value' => $autoOpen,
    ];
    $descriptionOpenModalOnElementClick = $this
      ->t('Example: <b>@example_class@</b>. Default is <b>@default_class@</b>', [
      '@example_class@' => '.open-modal-welcome',
      '@default_class@' => '.open-modal-page',
    ]);
    $form['open_modal_on_element_click'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Open this modal clicking on this element'),
      '#default_value' => $modal
        ->getOpenModalOnElementClick(),
      '#description' => $descriptionOpenModalOnElementClick,
    ];
    $languages = $this->languageManager
      ->getLanguages();
    $languagesOptions = [];
    foreach ($languages as $lid => $language) {
      $languagesOptions[$lid] = $language
        ->getName();
    }
    $langCode = $modal
      ->getLangCode();
    if ($modal
      ->isNew()) {
      $langCode = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
    }
    $form['langcode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Language'),
      '#options' => $languagesOptions,
      '#default_value' => $langCode,
    ];
    if (count($languages) <= 1) {
      $disabled = [
        'disabled' => 'disabled',
      ];
      $form['langcode']['#attributes'] = $disabled;
      unset($form['langcode']);
    }
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced'),
    ];
    $form['advanced']['modal_customization'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Customization'),
    ];
    $form['advanced']['modal_customization']['ok_button'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('OK Button'),
    ];
    $okLabelButton = $modal
      ->getOkLabelButton();
    if ($modal
      ->isNew()) {
      $okLabelButton = $this
        ->t('OK');
    }
    $form['advanced']['modal_customization']['ok_button']['ok_label_button'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('OK Label Button'),
      '#default_value' => $okLabelButton,
      '#description' => $this
        ->t('If blank the value will be <b>@default_label@</b>', [
        '@default_label@' => 'OK',
      ]),
    ];
    $form['advanced']['modal_customization']['dont_show_again'] = [
      '#type' => 'details',
      '#title' => $this
        ->t("Don't show again"),
    ];
    $enableDontShowAgainOption = $modal
      ->getEnableDontShowAgainOption();
    if ($modal
      ->isNew()) {
      $enableDontShowAgainOption = TRUE;
    }
    $form['advanced']['modal_customization']['dont_show_again']['enable_dont_show_again_option'] = [
      '#title' => $this
        ->t('Enable option <b>@dont_show_again_label@</b>', [
        '@dont_show_again_label@' => "Don't show again",
      ]),
      '#type' => 'checkbox',
      '#default_value' => $enableDontShowAgainOption,
    ];
    $dontShowAgainLabel = $modal
      ->getDontShowAgainLabel();
    if ($modal
      ->isNew()) {
      $dontShowAgainLabel = $this
        ->t("Don't show again");
    }
    $form['advanced']['modal_customization']['dont_show_again']['dont_show_again_label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#default_value' => $dontShowAgainLabel,
      '#description' => $this
        ->t('If blank the value will be <b>@dont_show_again_label@</b>', [
        '@dont_show_again_label@' => "Don't show again",
      ]),
    ];
    $form['advanced']['modal_customization']['modal_size'] = [
      '#type' => 'details',
      '#title' => $this
        ->t("Modal Size"),
    ];
    $modalSizeOptions = [
      'modal-sm' => 'Small',
      'modal-md' => 'Medium',
      'modal-lg' => 'Large',
    ];
    $modalSizeDefaultValue = $modal
      ->getModalSize();
    if ($modal
      ->isNew()) {
      $modalSizeDefaultValue = 'modal-md';
    }
    $form['advanced']['modal_customization']['modal_size']['modal_size'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Modal Size'),
      '#options' => $modalSizeOptions,
      '#default_value' => $modalSizeDefaultValue,
    ];
    $form['advanced']['modal_customization']['close_modal_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t("Close Modal"),
    ];
    $closeModalEscKey = $modal
      ->getCloseModalEscKey();
    if ($modal
      ->isNew()) {
      $closeModalEscKey = TRUE;
    }
    $form['advanced']['modal_customization']['close_modal_options']['close_modal_esc_key'] = [
      '#title' => $this
        ->t('Close Modal pressing ESC key'),
      '#type' => 'checkbox',
      '#default_value' => $closeModalEscKey,
    ];
    $closeModalClickingOutside = $modal
      ->getCloseModalClickingOutside();
    if ($modal
      ->isNew()) {
      $closeModalClickingOutside = TRUE;
    }
    $form['advanced']['modal_customization']['close_modal_options']['close_modal_clicking_outside'] = [
      '#title' => $this
        ->t('Close Modal clicking outside the modal'),
      '#type' => 'checkbox',
      '#default_value' => $closeModalClickingOutside,
    ];
    $form['advanced']['roles_restriction'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Roles Restriction'),
    ];
    $roles = $modal
      ->getRoles();
    if ($modal
      ->isNew()) {
      $roles = [];
    }
    $form['advanced']['roles_restriction']['roles'] = [
      '#title' => $this
        ->t('Who can access this Modal'),
      '#type' => 'checkboxes',
      '#options' => user_role_names(),
      '#default_value' => $roles,
      '#description' => $this
        ->t('If no role is selected this Modal will be visible to everyone.'),
    ];
    $form['advanced']['extras'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Extras'),
    ];
    $modalTypeOptions = [
      'page' => 'Page',
      'parameter' => 'Parameter',
    ];
    $type = $modal
      ->getType();
    if ($modal
      ->isNew()) {
      $type = 'page';
    }
    $form['advanced']['extras']['type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Modal By'),
      '#options' => $modalTypeOptions,
      '#default_value' => $type,
    ];
    $defaultValueDelayDisplay = $modal
      ->getDelayDisplay();
    if ($modal
      ->isNew()) {
      $defaultValueDelayDisplay = 0;
    }
    $form['advanced']['extras']['delay_display'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Delay to display'),
      '#default_value' => $defaultValueDelayDisplay,
      '#description' => $this
        ->t('Value in seconds.'),
    ];
    $published = $modal
      ->getPublished();
    if ($modal
      ->isNew()) {
      $published = TRUE;
    }
    $form['advanced']['extras']['published'] = [
      '#title' => $this
        ->t('Published'),
      '#type' => 'checkbox',
      '#default_value' => $published,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    if (empty($values['pages'])) {
      return FALSE;
    }
    $pages = $values['pages'];
    $urlList = [];
    $urlList = explode(PHP_EOL, $pages);
    foreach ($urlList as $url) {
      $trimUrl = trim($url);

      // Validate Slash.
      if ($trimUrl !== '<front>' && $trimUrl[0] !== '/' && $trimUrl[0] !== '') {
        $form_state
          ->setErrorByName('pages', $this
          ->t("@url path needs to start with a slash.", [
          '@url' => $trimUrl,
        ]));
      }

      // Validate wildcard.
      if (strpos($trimUrl, '*') !== FALSE && substr($trimUrl, -1) != '*') {
        $form_state
          ->setErrorByName('pages', $this
          ->t("The wildcard * must be used at the end of the path. E.g. /admin/*"));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $modal = $this->entity;
    $body = '';
    if (!empty($form_state
      ->getValue('body'))) {
      $body = $form_state
        ->getValue('body');
    }
    $modal
      ->setBody($body);
    $pages = '';
    if (!empty($form_state
      ->getValue('pages'))) {
      $pages = $form_state
        ->getValue('pages');
    }
    $modal
      ->setPages($pages);
    $status = $modal
      ->save();
    switch ($status) {
      case SAVED_NEW:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Created the %label Modal.', [
          '%label' => $modal
            ->label(),
        ]));
        break;
      default:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Saved the %label Modal.', [
          '%label' => $modal
            ->label(),
        ]));
    }
    $form_state
      ->setRedirectUrl($modal
      ->toUrl('collection'));

    // Clear Views' cache.
    drupal_flush_all_caches();
  }

}

Classes

Namesort descending Description
ModalForm Class ModalForm.