You are here

class SlickSettingsForm in Slick Carousel 8.2

Same name and namespace in other branches
  1. 8 slick_ui/src/Form/SlickSettingsForm.php \Drupal\slick_ui\Form\SlickSettingsForm
  2. 7.3 slick_ui/src/Form/SlickSettingsForm.php \Drupal\slick_ui\Form\SlickSettingsForm

Defines the Slick admin settings form.

Hierarchy

Expanded class hierarchy of SlickSettingsForm

1 file declares its use of SlickSettingsForm
SlickSettingsFormTest.php in tests/src/Kernel/Form/SlickSettingsFormTest.php
1 string reference to 'SlickSettingsForm'
slick_ui.routing.yml in slick_ui/slick_ui.routing.yml
slick_ui/slick_ui.routing.yml

File

slick_ui/src/Form/SlickSettingsForm.php, line 12

Namespace

Drupal\slick_ui\Form
View source
class SlickSettingsForm extends ConfigFormBase {

  /**
   * Drupal\Core\Asset\LibraryDiscoveryInterface definition.
   *
   * @var Drupal\Core\Asset\LibraryDiscoveryInterface
   */
  protected $libraryDiscovery;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /**
     * @var \Drupal\slick_ui\Form\SlickSettingsForm
     */
    $instance = parent::create($container);
    $instance->libraryDiscovery = $container
      ->get('library.discovery');
    return $instance;
  }

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

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

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('slick.settings');
    $form['library'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Library to use'),
      '#description' => $this
        ->t('<a href=":url1">Slick</a> is the original library by Ken Wheeler. <a href=":url2">Accessible Slick</a> is a forked library with accessibility enhancements from Accessibility360. Be sure to clear cache if things broken when changing this. <b>Warning</b>! Accessible Slick has breaking changes, <a href=":url3">read more</a>.', [
        ':url1' => 'https://kenwheeler.github.io/slick/',
        ':url2' => 'https://accessible360.github.io/accessible-slick/',
        ':url3' => 'https://www.drupal.org/project/slick/issues/3196529',
      ]),
      '#options' => [
        'slick' => $this
          ->t('Slick'),
        'accessible-slick' => $this
          ->t('Accessible Slick'),
      ],
      '#default_value' => $config
        ->get('library'),
    ];
    $form['module_css'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Slick module slick.theme.css'),
      '#description' => $this
        ->t('Uncheck to permanently disable the module slick.theme.css, normally included along with skins.'),
      '#default_value' => $config
        ->get('module_css'),
      '#prefix' => $this
        ->t("Note! Slick doesn't need Slick UI to run. It is always safe to uninstall Slick UI once done with optionsets."),
    ];
    $form['slick_css'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Slick library slick-theme.css'),
      '#description' => $this
        ->t('Uncheck to permanently disable the optional slick-theme.css, normally included along with skins.'),
      '#default_value' => $config
        ->get('slick_css'),
    ];
    $form['disable_old_skins'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Disable deprecated skins'),
      '#description' => $this
        ->t('Deprecated skins are registered via the <a href=":url">to-be-deprecated hook_hook_info</a>. Now Slick uses plugin system to store its skins. Leave it unchecked if things are broken, or (y/our) sub-modules are not updated with the new plugin system, yet. If you are sure things are not broken, or never register a skin nor using Slick examples, you can check this to reduce extra join. At any rate, skins are permanently cached once, so should not impact much.', [
        ':url' => 'https://www.drupal.org/node/2233261',
      ]),
      '#default_value' => $config
        ->get('disable_old_skins'),
    ];
    $form['sitewide'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Load slick globally'),
      '#empty_option' => $this
        ->t('- None -'),
      '#options' => [
        1 => $this
          ->t('With default initializer'),
        2 => $this
          ->t('With vanilla initializer'),
        3 => $this
          ->t('Without initializer'),
      ],
      '#description' => $this
        ->t('Warning! Not compatible with BigPipe module due to assets re-ordering issue, see https://drupal.org/node/3211873. Meaning may break any stylings provided by this module.<ol><li><b>With default initializer</b> will include the module slick.load.min.js as the initializer normally used by the module formatters or views identified by <code>.slick</code> selector. Only if you need consistent styling/ skins, classes, media player, lightboxes, and markups. Works immediately at body texts.</li><li><b>With vanilla initializer</b> will include the module slick.vanilla.min.js as the minimal initializer identified by <code>.slick-vanilla</code> selector. Default skins, media player, lightboxes are unusable. Be sure to add CSS class <code>.slick-vanilla</code> to your Slick. Recommended to not interfere or co-exist with module formatters/ views. Works immediately at body texts.</li><li><b>Without initializer</b> will load only the main libraries. No module skins, no module JS. It is all yours -- broken unless you initialize it.</li></ol> This will include Slick anywhere except admin pages. Only do this if you need Slick where PHP or Twig is not available such as at body texts. Otherwise use the provided API instead. Implements <code>hook_slick_attach_alter</code> to include additional libraries such as skins, mousewheel, colorbox, etc. At any rate, you can inject options via <code>data-slick</code> attribute, or custom JavaScript. You can also include them at your theme, it is just a convenient way to avoid hard-coding at every theme changes. Check out slick.html.twig for more markups.'),
      '#default_value' => $config
        ->get('sitewide'),
    ];
    $default = $config
      ->get('sitewide') == 0 || $config
      ->get('sitewide') == 1;
    $form['preview'] = $default ? $this
      ->withInitializer() : $this
      ->withoutInitializer();
    return parent::buildForm($form, $form_state);
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::submitForm().
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory
      ->getEditable('slick.settings')
      ->set('library', $form_state
      ->getValue('library'))
      ->set('slick_css', $form_state
      ->getValue('slick_css'))
      ->set('module_css', $form_state
      ->getValue('module_css'))
      ->set('disable_old_skins', $form_state
      ->getValue('disable_old_skins'))
      ->set('sitewide', (int) $form_state
      ->getValue('sitewide'))
      ->save();

    // Invalidate the library discovery cache to update new assets.
    $this->libraryDiscovery
      ->clearCachedDefinitions();
    $this->configFactory
      ->clearStaticCache();
    parent::submitForm($form, $form_state);
  }

  /**
   * Provides sample with default Slick markups.
   */
  private function withInitializer() {
    $items = [];
    foreach ([
      'One',
      'Two',
      'Three',
    ] as $key) {
      $img = '<img src="https://drupal.org/files/' . $key . '.gif" />';
      $items[] = [
        'slide' => [
          '#markup' => $img,
        ],
        'caption' => [
          'title' => $key,
        ],
      ];
    }
    $build = [
      'items' => $items,
      'settings' => [
        'skin' => 'classic',
        'layout' => 'bottom',
      ],
      'options' => [
        'arrows' => TRUE,
        'dots' => TRUE,
      ],
    ];
    $content = \slick()
      ->build($build);
    return $this
      ->preview($content);
  }

  /**
   * Provides sample without default Slick markups.
   */
  private function withoutInitializer() {
    $config = $this
      ->config('slick.settings');
    $vanilla = $config
      ->get('sitewide') == 2;
    $items = [];
    foreach ([
      'One',
      'Two',
      'Three',
    ] as $key) {
      $items[] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => '<img src="https://drupal.org/files/' . $key . '.gif" />',
      ];
    }
    $class = $vanilla ? 'vanilla' : 'whatever';
    $config = "{'arrows': true, 'dots': true}";
    $prefix = 'class="slick-' . $class . '" data-slick="' . $config . '"';
    $suffix = "<blockquote><pre>&lt;div class=&quot;slick-" . $class . "&quot; data-slick=&quot;{'arrows': true, 'dots': true}&quot;&gt;\n    &lt;div&gt;&lt;img src=&quot;https://drupal.org/files/One.gif&quot; /&gt;&lt;/div&gt;\n    &lt;div&gt;&lt;img src=&quot;https://drupal.org/files/Two.gif&quot; /&gt;&lt;/div&gt;\n    &lt;div&gt;&lt;img src=&quot;https://drupal.org/files/Three.gif&quot; /&gt;&lt;/div&gt;\n&lt;/div&gt;</pre></blockquote>";
    return $this
      ->preview($items, $prefix, $suffix);
  }

  /**
   * Provides sample w/o default Slick markups.
   */
  private function preview($content, $prefix = '', $suffix = '') {
    $config = $this
      ->config('slick.settings');
    $unload = $config
      ->get('sitewide') == 2 || $config
      ->get('sitewide') == 3;
    $attach = \slick()
      ->attach([
      '_unload' => $unload,
      '_vanilla' => $config
        ->get('sitewide') == 2,
    ]);
    if (empty($suffix)) {
      $suffix = "<blockquote><pre>&lt;div class=&quot;slick&quot; data-slick=&quot;{'arrows': true, 'dots': true}&quot;&gt;\n  &lt;div class=&quot;slick__slider&quot;&gt;\n    &lt;div class=&quot;slick__slide&quot;&gt;&lt;img src=&quot;https://drupal.org/files/One.gif&quot; /&gt;&lt;/div&gt;\n    &lt;div class=&quot;slick__slide&quot;&gt;&lt;img src=&quot;https://drupal.org/files/Two.gif&quot; /&gt;&lt;/div&gt;\n    &lt;div class=&quot;slick__slide&quot;&gt;&lt;img src=&quot;https://drupal.org/files/Three.gif&quot; /&gt;&lt;/div&gt;\n  &lt;/div&gt;\n  &lt;nav class=&quot;slick__arrow&quot; &gt; &lt;/nav&gt;\n&lt;/div&gt;</pre></blockquote>";
    }
    return [
      '#type' => 'inline_template',
      '#template' => '{{ prefix | raw }}{{ stage }}{{ suffix | raw }}',
      '#context' => [
        'stage' => $content,
        'prefix' => '<div style="background: rgb(52, 152, 219);"><div style="margin: 30px auto; max-width: 350px; min-height: 240px; text-align: center;" ' . $prefix . '>',
        'suffix' => '</div></div>' . $suffix,
      ],
      '#attached' => $attach,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SlickSettingsForm::$libraryDiscovery protected property Drupal\Core\Asset\LibraryDiscoveryInterface definition.
SlickSettingsForm::buildForm public function Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides ConfigFormBase::buildForm
SlickSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SlickSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SlickSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SlickSettingsForm::preview private function Provides sample w/o default Slick markups.
SlickSettingsForm::submitForm public function Implements \Drupal\Core\Form\FormInterface::submitForm(). Overrides ConfigFormBase::submitForm
SlickSettingsForm::withInitializer private function Provides sample with default Slick markups.
SlickSettingsForm::withoutInitializer private function Provides sample without default Slick markups.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.