Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
drupal/melt_modal / src / Form / MeltModalForm.php
Size: Mime:
<?php

namespace Drupal\melt_modal\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Condition\ConditionManager;
/**
 * Class modalForm.
 */
class MeltModalForm extends ConfigFormBase {

  protected $condition;

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Drupal\Core\Condition\ConditionManager definition.
   *
   * @var \Drupal\Core\Condition\ConditionManager
   */
  protected $pluginManagerCondition;

  /**
   * Constructs a new DefaultForm object.
   */
  public function __construct(
    ConfigFactoryInterface $config_factory,
    ConditionManager $plugin_manager_condition
  ) {
    parent::__construct($config_factory);
    $this->configFactory = $config_factory;
    $this->pluginManagerCondition = $plugin_manager_condition;
    $this->condition = $this->pluginManagerCondition->createInstance('request_path');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('plugin.manager.condition')
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'melt_modal.modal',
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('melt_modal.modal');

    $this->condition->setConfiguration($config->get('request_path'));

    $form['external_link_settings'] = [
      '#type' => 'details',
      '#title' => $this->t('External Link Modal Settings'),
      '#description' => $this->t('Settings for external link modal'),
      // Controls the HTML5 'open' attribute. Defaults to FALSE.
      '#open' => TRUE,
    ];

    $form['external_link_settings']['extlink_enable'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable'),
      '#description' => $this->t('Enable if you want all 3rd party links to trigger a global modal.'),
      '#default_value' => $config->get('extlink_enable'),
    ];

    $form['external_link_settings']['extlink_modal_selector'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Modal ID'),
      '#description' => $this->t('CSS selector for modal. Defaults to <b>#external-link-modal</b>'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config->get('extlink_modal_selector'),
    ];

    $form['external_link_settings']['extlink_continue_selector'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Modal Continue ID'),
      '#description' => $this->t('CSS selector for the continue link. Defaults to <b>.external-link-modal--continue</b>'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config->get('extlink_continue_selector'),
    ];

    $form['external_link_settings']['extlink_whitelist'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Whitelist Domains'),
      '#description' => $this->t('Domains to ignore. Enter 1 domain per line.'),
      '#default_value' => $config->get('extlink_whitelist'),
    ];

    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this->t('Page Visiblity Settings'),
      '#description' => $this->t('List of paths to enable melt Modal. If none are listed, all paths are enabled.'),
      // Controls the HTML5 'open' attribute. Defaults to FALSE.
      '#open' => FALSE,
    ];

    // Add the configuration form for the RequestPath config form.
    $form += $this->condition->buildConfigurationForm($form, $form_state);

    // Add Page visibility settings to details field set.
    $form['advanced']['pages'] = $form['pages'];
    $form['advanced']['negate'] = $form['negate'];
    unset($form['pages']);
    unset($form['negate']);

    return parent::buildForm($form, $form_state);
  }

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

    $this->condition->submitConfigurationForm($form, $form_state);

    $this->config('melt_modal.modal')
      ->set('extlink_modal_selector', $form_state->getValue('extlink_modal_selector'))
      ->set('extlink_continue_selector', $form_state->getValue('extlink_continue_selector'))
      ->set('extlink_whitelist', $form_state->getValue('extlink_whitelist'))
      ->set('extlink_enable', $form_state->getValue('extlink_enable'))
      ->set('request_path', $this->condition->getConfiguration())
      ->save();
  }

}