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_sticky_drawer / src / Form / StickyDrawerForm.php
Size: Mime:
<?php

namespace Drupal\melt_sticky_drawer\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Condition\ConditionManager;

/**
 * Class StickyDrawerForm.
 */
class StickyDrawerForm 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_sticky_drawer.stickydrawer',
    ];
  }

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

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

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

    $form['target_selector'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Target'),
      '#description' => $this->t('CSS selector for element to be fixed. Defaults to <b>.sticky-drawer</b>.'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config->get('target_selector'),
    ];
    $form['target_wrapper_selector'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Target Wrapper'),
      '#description' => $this->t('CSS selector for wrapper element. Defaults to <b>.sticky-drawer-wrapper</b>.'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config->get('target_wrapper_selector'),
    ];

    $form['scroll_settings'] = [
      '#type' => 'details',
      '#title' => $this->t('Scroll Settings'),
      '#description' => $this->t('Settings for scroll animation'),
      // Controls the HTML5 'open' attribute. Defaults to FALSE.
      '#open' => FALSE,
    ];

    $form['scroll_settings']['toggle_selector'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Scroll To Button'),
      '#description' => $this->t('CSS selector for button that smooth scrolls to the targets original location. Defaults to <b>.sticky-drawer-toggle</b>'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config->get('toggle_selector'),
    ];

    $form['scroll_settings']['scrollToContainer_selector'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Scroll To Container'),
      '#description' => $this->t('CSS selector that targets the element to scroll to when the toggle is clicked. Defaults to <b>.sticky-drawer-wrapper</b>'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config->get('scrollToContainer_selector'),
    ];

    $form['scroll_settings']['scrollTo_yoffset'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Scroll To Offset'),
      '#description' => $this->t('How far from the top do you want the element to stop scrolling? By default the element will scroll to the top of the viewport.'),
      '#maxlength' => 64,
      '#size' => 64,
      '#default_value' => $config->get('scrollTo_yoffset'),
    ];

    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this->t('Page Visiblity Settings'),
      '#description' => $this->t('List of paths that the Sticky Drawer does not get executed on.'),
      // 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'];
    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_sticky_drawer.stickydrawer')
      ->set('target_selector', $form_state->getValue('target_selector'))
      ->set('target_wrapper_selector', $form_state->getValue('target_wrapper_selector'))
      ->set('toggle_selector', $form_state->getValue('toggle_selector'))
      ->set('scrollToContainer_selector', $form_state->getValue('scrollToContainer_selector'))
      ->set('scrollTo_yoffset', $form_state->getValue('scrollTo_yoffset'))
      ->set('page_restrictions', $form_state->getValue('page_restrictions'))
      ->set('request_path', $this->condition->getConfiguration())
      ->save();
  }

}