You are here

class ScrollEffects in Bootstrap Styles 1.0.x

Class Effect.

@package Drupal\bootstrap_styles\Plugin\Style

Plugin annotation


@Style(
  id = "scroll_effects",
  title = @Translation("Scroll Effects"),
  group_id = "animation",
  weight = 3
)

Hierarchy

Expanded class hierarchy of ScrollEffects

File

src/Plugin/BootstrapStyles/Style/ScrollEffects.php, line 20

Namespace

Drupal\bootstrap_styles\Plugin\BootstrapStyles\Style
View source
class ScrollEffects extends StylePluginBase {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config();

    // Library assets.
    $form['animation']['scroll_effects_library_type'] = [
      '#type' => 'select',
      '#default_value' => $config
        ->get('scroll_effects_library_type'),
      '#title' => $this
        ->t('How should we load the animation library?'),
      '#required' => TRUE,
      '#options' => [
        'local' => $this
          ->t('Do nothing, my theme handle it'),
        'external' => $this
          ->t('Add the library for me please'),
      ],
      '#description' => $this
        ->t('<p>Default uses the AOS library: <a href="https://michalsnik.github.io/aos" target="_blank">https://michalsnik.github.io/aos</a><br/> You can override the animation library in your theme by using <br /><code>libraries-override:<br />&nbsp;&nbsp;bootstrap_styles/plugin.scroll_effects.build: your_theme/your_new_library_definition</code><br /><small>For more information, please check: <a href="https://www.drupal.org/node/2497313" target="_blank">https://www.drupal.org/node/2497313</a></small></p>'),
    ];
    $form['animation']['scroll_effects'] = [
      '#type' => 'textarea',
      '#default_value' => $config
        ->get('scroll_effects'),
      '#title' => $this
        ->t('Scroll Effects'),
      '#description' => $this
        ->t('<p>Enter one value per line, in the format <b>key|label</b> where <em>key</em> is the attribute\'s value, and <em>label</em> is the human readable name of the effect.</p>'),
      '#cols' => 60,
      '#rows' => 5,
    ];
    $form['animation']['scroll_effects_attr_type'] = [
      '#type' => 'checkbox',
      '#default_value' => $config
        ->get('scroll_effects_attr_type'),
      '#title' => $this
        ->t('Use data attribute instead of class.'),
    ];
    $form['animation']['scroll_effects_data_key'] = [
      '#type' => 'textfield',
      '#default_value' => $config
        ->get('scroll_effects_data_key'),
      '#title' => $this
        ->t('Data Key'),
      '#description' => $this
        ->t('<p>The <strong>data_key</strong> will be used as the data attribute. Example: <code>data_key="key"</code></p>'),
      '#states' => [
        'visible' => [
          [
            [
              ':input[name="scroll_effects_attr_type"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config()
      ->set('scroll_effects_library_type', $form_state
      ->getValue('scroll_effects_library_type'))
      ->set('scroll_effects_attr_type', $form_state
      ->getValue('scroll_effects_attr_type'))
      ->set('scroll_effects_data_key', $form_state
      ->getValue('scroll_effects_data_key'))
      ->set('scroll_effects', $form_state
      ->getValue('scroll_effects'))
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function buildStyleFormElements(array &$form, FormStateInterface $form_state, $storage) {
    $form['scroll_effects'] = [
      '#type' => 'radios',
      '#options' => $this
        ->getStyleOptions('scroll_effects'),
      '#title' => $this
        ->t('Scroll Effects'),
      '#default_value' => $storage['scroll_effects']['class'] ?? NULL,
      '#validated' => TRUE,
      '#attributes' => [
        'class' => [
          'field-scroll-effects',
        ],
      ],
      '#prefix' => '<span class="input-icon"></span>',
    ];

    // Add icons to the effets.
    foreach ($form['scroll_effects']['#options'] as $key => $value) {
      $form['scroll_effects']['#options'][$key] = '<span class="input-icon ' . $key . '"></span>' . $value;
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitStyleFormElements(array $group_elements) {
    return [
      'scroll_effects' => [
        'class' => $group_elements['scroll_effects'],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function build(array $build, array $storage, $theme_wrapper = NULL) {
    $library_type = $this
      ->config()
      ->get('scroll_effects_library_type');
    $attribute_type = $this
      ->config()
      ->get('scroll_effects_attr_type');
    $data_key = $this
      ->config()
      ->get('scroll_effects_data_key');

    // Assign the style to element or its theme wrapper if exist.
    if ($theme_wrapper && isset($build['#theme_wrappers'][$theme_wrapper])) {
      if (isset($attribute_type) && $attribute_type === 1) {

        // Output some sort of data attribute.
        $build['#theme_wrappers'][$theme_wrapper]['#attributes'][$data_key][] = $storage['scroll_effects']['class'];
      }
      else {

        // Output classes.
        $build['#theme_wrappers'][$theme_wrapper]['#attributes']['class'][] = $storage['scroll_effects']['class'];
      }
    }
    else {
      if (isset($attribute_type) && $attribute_type === 1) {

        // Output some sort of data attribute.
        $build['#attributes'][$data_key][] = $storage['scroll_effects']['class'];
      }
      else {

        // Output classes.
        $build['#attributes']['class'][] = $storage['scroll_effects']['class'];
      }
    }
    if (isset($library_type) && $library_type === 'external') {
      $build['#attached']['library'][] = 'bootstrap_styles/plugin.scroll_effects.build';
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ScrollEffects::build public function Overrides StylePluginBase::build
ScrollEffects::buildConfigurationForm public function Overrides StylePluginBase::buildConfigurationForm
ScrollEffects::buildStyleFormElements public function Overrides StylePluginBase::buildStyleFormElements
ScrollEffects::submitConfigurationForm public function Overrides StylePluginBase::submitConfigurationForm
ScrollEffects::submitStyleFormElements public function Overrides StylePluginBase::submitStyleFormElements
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
StylePluginBase::$configFactory protected property The config factory.
StylePluginBase::addClassesToBuild public function Helper function to add the classes to the build.
StylePluginBase::config public function
StylePluginBase::CONFIG constant Config settings.
StylePluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
StylePluginBase::getStyleOptionClassByIndex public function Helper function to get the class from the options list.
StylePluginBase::getStyleOptionIndexByClass public function Helper function to get the index of the class at options list.
StylePluginBase::getStyleOptions public function Helper function to get the options of given style name.
StylePluginBase::getStyleOptionsCount public function Helper function to get the options of given style name.
StylePluginBase::getSvgIconMarkup public function Helper function to get SVG Markup.
StylePluginBase::getTitle public function Return the name of the Styles Group form plugin. Overrides StylePluginInterface::getTitle
StylePluginBase::validateConfigurationForm public function
StylePluginBase::__construct public function Constructs a StylePluginBase object. Overrides PluginBase::__construct 1