You are here

class BxsliderThs in BxSlider 8

BxSlider thumbnail pager.

Plugin annotation


@FieldFormatter(
 id = "bxslider_ths",
 label = @Translation("BxSlider - Thumbnail slider"),
 field_types = {"image", "media"}
)

Hierarchy

Expanded class hierarchy of BxsliderThs

File

src/Plugin/Field/FieldFormatter/BxsliderThs.php, line 19

Namespace

Drupal\bxslider\Plugin\Field\FieldFormatter
View source
class BxsliderThs extends Bxslider implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    $bxslider_settings = parent::defaultSettings();
    $bxslider_settings['slider']['pager']['pager'] = FALSE;
    return [
      'thumbnail_slider' => [
        'thumbnail_style' => 'thumbnail',
        'general' => [
          'mode' => 'horizontal',
          'speed' => 500,
          'slideMargin' => 0,
          'startSlide' => 0,
          'randomStart' => FALSE,
          'infiniteLoop' => FALSE,
          'hideControlOnEnd' => TRUE,
          'easing' => '',
          'captions' => FALSE,
          'ticker' => FALSE,
          'tickerHover' => FALSE,
          'adaptiveHeight' => FALSE,
          'adaptiveHeightSpeed' => 500,
          'video' => FALSE,
          'responsive' => TRUE,
          'useCSS' => TRUE,
          'preloadImages' => 'visible',
          'touchEnabled' => TRUE,
          'swipeThreshold' => 50,
          'oneToOneTouch' => TRUE,
          'preventDefaultSwipeX' => TRUE,
          'preventDefaultSwipeY' => FALSE,
          'wrapperClass' => 'bx-wrapper',
        ],
        'pager' => [
          'pager' => FALSE,
          'pagerType' => 'full',
          'pagerShortSeparator' => ' / ',
          'pagerSelector' => '',
        ],
        'controls' => [
          'controls' => TRUE,
          'nextText' => 'Next',
          'prevText' => 'Prev',
          'nextSelector' => '',
          'prevSelector' => '',
          'autoControls' => FALSE,
          'startText' => 'Start',
          'stopText' => 'Stop',
          'autoControlsCombine' => FALSE,
          'autoControlsSelector' => '',
        ],
        'auto' => [
          'auto' => FALSE,
          'pause' => 4000,
          'autoStart' => TRUE,
          'autoDirection' => 'next',
          'autoHover' => FALSE,
          'autoDelay' => 0,
        ],
        'carousel' => [
          'minSlides' => 4,
          'maxSlides' => 4,
          'moveSlides' => 1,
          'slideWidth' => 0,
        ],
      ],
    ] + $bxslider_settings;
  }

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

    // Hide Pager options, because here is used Thumbnail image slider.
    $elements['slider']['pager']['#access'] = FALSE;
    $settings = $this
      ->getSettings();
    $image_styles = image_style_options(FALSE);
    $elements['thumbnail_slider'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Thumbnail slider'),
      '#weight' => 10,
    ];
    $elements['thumbnail_slider']['thumbnail_style'] = [
      '#title' => $this
        ->t('Image style'),
      '#type' => 'select',
      '#default_value' => $settings['thumbnail_slider']['thumbnail_style'],
      '#empty_option' => $this
        ->t('None (original image)'),
      '#options' => $image_styles,
    ];
    $elements['thumbnail_slider']['general'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('General'),
      '#weight' => 1,
      '#open' => FALSE,
    ];
    $elements['thumbnail_slider']['general']['mode'] = [
      '#title' => $this
        ->t('Mode'),
      '#type' => 'select',
      '#default_value' => $settings['thumbnail_slider']['general']['mode'],
      '#options' => [
        'horizontal' => 'horizontal',
        'fade' => 'fade',
      ],
    ];
    $elements['thumbnail_slider']['general']['speed'] = [
      '#title' => $this
        ->t('Speed'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['general']['speed'],
    ];
    $elements['thumbnail_slider']['general']['slideMargin'] = [
      '#title' => $this
        ->t('slideMargin'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['general']['slideMargin'],
    ];
    $elements['thumbnail_slider']['general']['startSlide'] = [
      '#title' => $this
        ->t('startSlide'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['general']['startSlide'],
    ];
    $elements['thumbnail_slider']['general']['randomStart'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('randomStart'),
      '#default_value' => $settings['thumbnail_slider']['general']['randomStart'],
    ];
    $elements['thumbnail_slider']['general']['infiniteLoop'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('infiniteLoop'),
      '#default_value' => $settings['thumbnail_slider']['general']['infiniteLoop'],
    ];
    $elements['thumbnail_slider']['general']['hideControlOnEnd'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('hideControlOnEnd'),
      '#default_value' => $settings['thumbnail_slider']['general']['hideControlOnEnd'],
    ];
    $elements['thumbnail_slider']['general']['easing'] = [
      '#title' => $this
        ->t('easing'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['general']['easing'],
    ];
    $elements['thumbnail_slider']['general']['captions'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('captions'),
      '#default_value' => $settings['thumbnail_slider']['general']['captions'],
    ];
    $elements['thumbnail_slider']['general']['ticker'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('ticker'),
      '#default_value' => $settings['thumbnail_slider']['general']['ticker'],
    ];
    $elements['thumbnail_slider']['general']['tickerHover'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('tickerHover'),
      '#default_value' => $settings['thumbnail_slider']['general']['tickerHover'],
    ];
    $elements['thumbnail_slider']['general']['adaptiveHeight'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('adaptiveHeight'),
      '#default_value' => $settings['thumbnail_slider']['general']['adaptiveHeight'],
    ];
    $elements['thumbnail_slider']['general']['adaptiveHeightSpeed'] = [
      '#title' => $this
        ->t('adaptiveHeightSpeed'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['general']['adaptiveHeightSpeed'],
    ];
    $elements['thumbnail_slider']['general']['responsive'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('responsive'),
      '#default_value' => $settings['thumbnail_slider']['general']['responsive'],
    ];
    $elements['thumbnail_slider']['general']['useCSS'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('useCSS'),
      '#default_value' => $settings['thumbnail_slider']['general']['useCSS'],
    ];
    $elements['thumbnail_slider']['general']['preloadImages'] = [
      '#title' => $this
        ->t('preloadImages'),
      '#type' => 'select',
      '#default_value' => $settings['thumbnail_slider']['general']['preloadImages'],
      '#options' => [
        'all' => 'all',
        'visible' => 'visible',
      ],
    ];
    $elements['slider']['general']['touchEnabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('touchEnabled'),
      '#default_value' => $settings['slider']['general']['touchEnabled'],
    ];
    $elements['thumbnail_slider']['general']['swipeThreshold'] = [
      '#title' => $this
        ->t('swipeThreshold'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['general']['swipeThreshold'],
    ];
    $elements['thumbnail_slider']['general']['oneToOneTouch'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('oneToOneTouch'),
      '#default_value' => $settings['thumbnail_slider']['general']['oneToOneTouch'],
    ];
    $elements['thumbnail_slider']['general']['preventDefaultSwipeX'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('preventDefaultSwipeX'),
      '#default_value' => $settings['thumbnail_slider']['general']['preventDefaultSwipeX'],
    ];
    $elements['thumbnail_slider']['general']['preventDefaultSwipeY'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('preventDefaultSwipeY'),
      '#default_value' => $settings['thumbnail_slider']['general']['preventDefaultSwipeY'],
    ];
    $elements['thumbnail_slider']['general']['wrapperClass'] = [
      '#title' => $this
        ->t('wrapperClass'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['general']['wrapperClass'],
    ];
    $elements['thumbnail_slider']['pager']['pager'] = [
      '#type' => 'hidden',
      '#default_value' => $settings['thumbnail_slider']['pager']['pager'],
    ];
    $elements['thumbnail_slider']['controls'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Controls'),
      '#weight' => 3,
      '#open' => FALSE,
    ];
    $elements['thumbnail_slider']['controls']['controls'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('controls'),
      '#default_value' => $settings['thumbnail_slider']['controls']['controls'],
    ];
    $elements['thumbnail_slider']['controls']['nextText'] = [
      '#title' => $this
        ->t('nextText'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['controls']['nextText'],
    ];
    $elements['thumbnail_slider']['controls']['prevText'] = [
      '#title' => $this
        ->t('prevText'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['controls']['prevText'],
    ];
    $elements['thumbnail_slider']['controls']['autoControls'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('autoControls'),
      '#default_value' => $settings['thumbnail_slider']['controls']['autoControls'],
    ];
    $elements['thumbnail_slider']['controls']['startText'] = [
      '#title' => $this
        ->t('startText'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['controls']['startText'],
    ];
    $elements['thumbnail_slider']['controls']['stopText'] = [
      '#title' => $this
        ->t('stopText'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['controls']['stopText'],
    ];
    $elements['thumbnail_slider']['controls']['autoControlsCombine'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Auto'),
      '#default_value' => $settings['thumbnail_slider']['controls']['autoControlsCombine'],
    ];
    $elements['thumbnail_slider']['auto'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Auto'),
      '#weight' => 4,
      '#open' => FALSE,
    ];
    $elements['thumbnail_slider']['auto']['auto'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Auto'),
      '#default_value' => $settings['thumbnail_slider']['auto']['auto'],
    ];
    $elements['thumbnail_slider']['auto']['pause'] = [
      '#title' => $this
        ->t('pause'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['auto']['pause'],
    ];
    $elements['thumbnail_slider']['auto']['autoStart'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('autoStart'),
      '#default_value' => $settings['thumbnail_slider']['auto']['autoStart'],
    ];
    $elements['thumbnail_slider']['auto']['autoDirection'] = [
      '#title' => $this
        ->t('autoDirection'),
      '#type' => 'select',
      '#default_value' => $settings['thumbnail_slider']['auto']['autoDirection'],
      '#options' => [
        'next' => 'next',
        'prev' => 'prev',
      ],
    ];
    $elements['thumbnail_slider']['auto']['autoHover'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('autoHover'),
      '#default_value' => $settings['thumbnail_slider']['auto']['autoHover'],
    ];
    $elements['thumbnail_slider']['auto']['autoDelay'] = [
      '#title' => $this
        ->t('autoDelay'),
      '#type' => 'textfield',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['auto']['autoDelay'],
    ];
    $elements['thumbnail_slider']['carousel'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Carousel'),
      '#weight' => 5,
      '#open' => FALSE,
    ];
    $elements['thumbnail_slider']['carousel']['minSlides'] = [
      '#title' => $this
        ->t('minSlides'),
      '#type' => 'number',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['carousel']['minSlides'],
    ];
    $elements['thumbnail_slider']['carousel']['maxSlides'] = [
      '#title' => $this
        ->t('maxSlides'),
      '#type' => 'number',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['carousel']['maxSlides'],
    ];
    $elements['thumbnail_slider']['carousel']['moveSlides'] = [
      '#title' => $this
        ->t('moveSlides'),
      '#type' => 'number',
      '#size' => 60,
      '#default_value' => $settings['thumbnail_slider']['carousel']['moveSlides'],
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $summary[] = t('BxSlider (with thumbnail slider) configuration');
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $parent_elements = parent::viewElements($items, $langcode);
    $elements = [];
    $settings = $this->settings;
    $files = $this
      ->getEntitiesToView($items, $langcode);

    // Early opt-out if the field is empty.
    if (empty($files)) {
      return $elements;
    }
    $image_style_setting = $settings['thumbnail_slider']['thumbnail_style'];

    // Collect cache tags to be added for each item in the field.
    $base_cache_tags = [];
    if (!empty($image_style_setting)) {
      $image_style = $this->imageStyleStorage
        ->load($image_style_setting);
      $base_cache_tags = $image_style
        ->getCacheTags();
    }
    $rendering_ths_items = [];
    foreach ($files as $delta => $file) {
      $cache_contexts = [];
      if (isset($link_file)) {
        $image_uri = $file
          ->getFileUri();
        $url = Url::fromUri(file_create_url($image_uri));
        $cache_contexts[] = 'url.site';
      }
      $cache_tags = Cache::mergeTags($base_cache_tags, $file
        ->getCacheTags());
      $rendering_ths_items[] = $file->_referringItem;
    }

    // A slider's own pager must be disabled, because for pager is used
    // another bxslider.
    $settings['slider']['pager']['pager'] = FALSE;
    $bxslider_settings['bxslider'] = array_merge($settings['slider']['general'], $settings['slider']['pager'], $settings['slider']['controls'], $settings['slider']['auto'], $settings['slider']['carousel']);
    $bxslider_settings['image_style'] = $settings['slider']['image_style'];
    $bxslider_settings['slider_id'] = 'bxslider-ths-' . str_replace('_', '-', $items
      ->getName());
    $bxslider_settings['colorbox'] = $settings['colorbox'];
    $bxslider_settings['thumbnail_slider'] = array_merge($settings['thumbnail_slider']['general'], $settings['thumbnail_slider']['pager'], $settings['thumbnail_slider']['controls'], $settings['thumbnail_slider']['auto'], $settings['thumbnail_slider']['carousel']);
    $bxslider_settings['thumbnail_slider']['thumbnail_style'] = $settings['thumbnail_slider']['thumbnail_style'];

    // Get thumbnail's width.
    $image_style_ths = $this->imageStyleStorage
      ->load($settings['thumbnail_slider']['thumbnail_style']);
    foreach ($image_style_ths
      ->getEffects() as $effect) {
      $thumbnail_width = $effect->configuration['width'];
    }
    $bxslider_settings['thumbnail_slider']['slideWidth'] = $thumbnail_width;
    $element = [
      '#theme' => 'bxslider_ths',
      '#items' => $parent_elements['#items'],
      '#thumbnail_items' => $rendering_ths_items,
      '#settings' => $bxslider_settings,
      '#cache' => [
        'tags' => $cache_tags,
        'contexts' => $cache_contexts,
      ],
    ];

    // Attach library.
    $element['#attached']['library'][] = 'bxslider/jquery.bxslider_ths';

    // Attach settings.
    $this
      ->sliderSettingsFixIntegerValues($bxslider_settings);
    $element['#attached']['drupalSettings']['bxslider_ths'][$bxslider_settings['slider_id']] = $bxslider_settings;
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Bxslider::$imageStyleStorage protected property The image style entity storage.
Bxslider::create public static function Creates an instance of the plugin. Overrides FormatterBase::create
Bxslider::sliderSettingsFixIntegerValues protected function Replace strings with digits to int.
Bxslider::__construct public function Constructs an ImageFormatter object. Overrides FormatterBase::__construct
BxsliderThs::defaultSettings public static function Defines the default settings for this plugin. Overrides Bxslider::defaultSettings
BxsliderThs::settingsForm public function Returns a form to configure settings for the formatter. Overrides Bxslider::settingsForm
BxsliderThs::settingsSummary public function Returns a short summary for the current formatter settings. Overrides Bxslider::settingsSummary
BxsliderThs::viewElements public function Builds a renderable array for a field value. Overrides Bxslider::viewElements
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
EntityReferenceFormatterBase::prepareView public function Loads the entities referenced in that field across all the entities being viewed. Overrides FormatterBase::prepareView
EntityReferenceFormatterBase::view public function Overrides FormatterBase::view
FileFormatterBase::checkAccess protected function Checks access to the given entity. Overrides EntityReferenceFormatterBase::checkAccess
FileFormatterBase::needsEntityLoad protected function Returns whether the entity referenced by an item needs to be loaded. Overrides EntityReferenceFormatterBase::needsEntityLoad 1
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::isApplicable public static function Returns if the formatter can be used for the provided field. Overrides FormatterInterface::isApplicable 14
ImageFormatterBase::getEntitiesToView protected function Returns the referenced entities for display. Overrides EntityReferenceFormatterBase::getEntitiesToView
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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 3
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.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
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.