You are here

class JCarouselFormatter in jCarousel 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/Field/FieldFormatter/JCarouselFormatter.php \Drupal\jcarousel\Plugin\Field\FieldFormatter\JCarouselFormatter

Plugin implementation of the 'jCarousel' formatter.

Plugin annotation


@FieldFormatter(
  id = "jcarousel",
  label = @Translation("jCarousel"),
  field_types = {
    "image"
  }
)

Hierarchy

Expanded class hierarchy of JCarouselFormatter

File

src/Plugin/Field/FieldFormatter/JCarouselFormatter.php, line 26
Contains \Drupal\jcarousel\Plugin\Field\FieldFormatter\JCarouselFormatter.

Namespace

Drupal\jcarousel\Plugin\Field\FieldFormatter
View source
class JCarouselFormatter extends ImageFormatter implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
      'wrap' => NULL,
      'skin' => 'default',
      'visible' => NULL,
      'responsive' => 0,
      'scroll' => '',
      'auto' => 0,
      'autoPause' => '1',
      'animation' => '',
      'start' => '1',
      'easing' => NULL,
      'vertical' => FALSE,
      'navigation' => '',
    ) + parent::defaultSettings();
  }

  /**
   * Wraps the skins manager.
   *
   * @return \Drupal\jcarousel\jCarouselSkinsManager
   */
  protected function skinsManager() {
    return \Drupal::service('jcarousel.skins.manager');
  }

  /**
   * Returns keyed array of jCarousel skins.
   *
   * @return array
   *   Keys array of skins
   */
  public function getSkins() {
    return $this
      ->skinsManager()
      ->getDefinitions();
  }

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

    // Build the list of skins as options.
    $skins = $this
      ->getSkins();
    foreach ($skins as $key => $skin) {
      $skins[$key] = $skin['label'];
    }
    $skins[''] = $this
      ->t('None');

    // Number of options to provide in count-based options.
    $start_range = range(-10, 10);
    $range = array_combine($start_range, $start_range);

    // Remove '0'.
    unset($range[0]);
    $auto_range = [
      '' => t('Auto'),
    ] + array_combine(range(1, 10), range(1, 10));
    $element['description'] = array(
      '#type' => 'markup',
      '#value' => '<div class="messages">' . t('The jCarousel style is affected by several other settings within the display. Enable the "Use AJAX" option on your display to have items loaded dynamically. The "Items to display" option will determine how many items are preloaded into the carousel on each AJAX request. Non-AJAX carousels will contain the total number of items set in the "Items to display" option. Carousels may not be used with the "Use pager" option.') . '</div>',
    );
    $element['wrap'] = array(
      '#type' => 'select',
      '#title' => t('Wrap content'),
      '#default_value' => $this
        ->getSetting('wrap'),
      '#description' => t('Specifies whether to wrap at the first/last item (or both) and jump back to the start/end.'),
      '#options' => array(
        0 => t('Disabled'),
        'circular' => t('Circular'),
        'both' => t('Both'),
        'last' => t('Last'),
        'first' => t('First'),
      ),
    );
    $element['skin'] = array(
      '#type' => 'select',
      '#title' => t('Skin'),
      '#default_value' => $this
        ->getSetting('skin'),
      '#options' => $skins,
      '#description' => t('Skins may be provided by other modules. Set to "None" if your theme includes carousel theming directly in style.css or another stylesheet. "None" does not include any built-in navigation, arrows, or positioning at all.'),
    );
    $element['responsive'] = array(
      '#type' => 'checkbox',
      '#title' => t('Responsive (number of items)'),
      '#default_value' => $this
        ->getSetting('responsive'),
      '#description' => t('Select this option to have the carousel automatically adjust the number of visible items and the number of items to scroll at a time based on the available width.') . ' <strong>' . t('Changing this option will override the "Visible" and "Scroll" options and set carousel orientation to "horizontal".') . '</strong>',
    );
    $element['visible'] = array(
      '#type' => 'select',
      '#title' => t('Number of visible items'),
      '#options' => $auto_range,
      '#default_value' => $this
        ->getSetting('visible'),
      '#description' => t('Set an exact number of items to show at a time. It is recommended to leave set this to "auto", in which the number of items will be determined automatically by the space available to the carousel.') . ' <strong>' . t('Changing this option will override "width" properties set in your CSS.') . '</strong>',
    );
    $element['scroll'] = array(
      '#type' => 'select',
      '#title' => t('Scroll'),
      '#description' => t('The number of items to scroll at a time. The "auto" setting scrolls all the visible items.'),
      '#options' => $auto_range,
      '#default_value' => $this
        ->getSetting('scroll'),
    );
    $element['auto'] = array(
      '#type' => 'textfield',
      '#title' => t('Auto-scroll after'),
      '#size' => 4,
      '#maxlength' => 4,
      '#default_value' => $this
        ->getSetting('auto'),
      '#field_suffix' => ' ' . t('seconds'),
      '#description' => t('Specifies how many seconds to periodically auto-scroll the content. If set to 0 (default) then autoscrolling is turned off.'),
    );
    $element['navigation'] = array(
      '#type' => 'select',
      '#title' => t('Enable navigation'),
      '#options' => array(
        '' => t('None'),
        'before' => t('Before'),
        'after' => t('After'),
      ),
      '#default_value' => $this
        ->getSetting('navigation'),
      '#description' => t('Enable a clickable navigation list to jump straight to a given page.'),
    );
    $element['advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('Advanced'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#parents' => array(
        'style_options',
      ),
    );
    $element['advanced']['animation'] = array(
      '#type' => 'textfield',
      '#title' => t('Animation speed'),
      '#size' => 10,
      '#maxlength' => 10,
      '#default_value' => $this
        ->getSetting('animation'),
      '#description' => t('The speed of the scroll animation as string in jQuery terms ("slow"  or "fast") or milliseconds as integer (See <a href="http://api.jquery.com/animate/">jQuery Documentation</a>).'),
    );
    $element['advanced']['easing'] = array(
      '#type' => 'textfield',
      '#title' => t('Easing effect'),
      '#size' => 10,
      '#maxlength' => 128,
      '#default_value' => $this
        ->getSetting('easing'),
      '#description' => t('The name of the easing effect that you want to use such as "swing" (the default) or "linear". See list of options in the <a href="http://api.jquery.com/animate/">jQuery Documentation</a>.'),
    );
    $element['advanced']['start'] = array(
      '#type' => 'select',
      '#title' => t('Start position'),
      '#description' => t('The item that will be shown as the first item in the list upon loading. Useful for starting a list in the middle of a set. A negative value allows choosing an item in the end, e.g. -1 is the last item.'),
      '#options' => $range,
      '#default_value' => $this
        ->getSetting('start'),
    );
    $element['advanced']['autoPause'] = array(
      '#type' => 'checkbox',
      '#title' => t('Pause auto-scroll on hover'),
      '#description' => t('If auto-scrolling, pause the carousel when the user hovers the mouse over an item.'),
      '#default_value' => $this
        ->getSetting('autoPause'),
    );
    $element['advanced']['vertical'] = array(
      '#type' => 'checkbox',
      '#title' => t('Vertical'),
      '#description' => t('Specifies wether the carousel appears in horizontal or vertical orientation. Changes the carousel from a left/right style to a up/down style carousel. Defaults to horizontal.'),
      '#default_value' => $this
        ->getSetting('vertical'),
    );
    $link_types = array(
      'content' => t('Content'),
      'file' => t('File'),
    );
    $element['image_link'] = array(
      '#title' => t('Link image to'),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('image_link'),
      '#empty_option' => t('Nothing'),
      '#options' => $link_types,
    );
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    $wrap = $this
      ->getSetting('wrap');
    if ($wrap != 0) {
      $summary[] = t('Wrap content: @wrap', [
        '@wrap' => Unicode::ucfirst($wrap),
      ]);
    }
    $skin = $this
      ->getSetting('skin');
    $skin_name = t('None');
    if (!empty($skin)) {
      $skins = $this
        ->getSkins();
      $skin_name = isset($skins[$skin]) ? $skins[$skin]['label'] : t('Broken skin !skin', [
        '!skin' => $skin,
      ]);
    }
    $summary[] = t('Skin: @skin', [
      '@skin' => $skin_name,
    ]);
    $responsive = $this
      ->getSetting('responsive');
    if ($responsive != 0) {
      $summary[] = t('Responsive (number of items): @responsive', [
        '@responsive' => $responsive,
      ]);
    }
    $visible = $this
      ->getSetting('visible');
    $visible_name = empty($visible) ? t('Auto') : $visible;
    $summary[] = t('Number of visible items: @visible', [
      '@visible' => $visible_name,
    ]);
    $scroll = $this
      ->getSetting('scroll');
    $scroll_name = empty($visible) ? t('All visible') : $scroll;
    $summary[] = t('Number of items to scroll at a time: @scroll', [
      '@scroll' => $scroll_name,
    ]);
    $auto = $this
      ->getSetting('auto');
    if (!empty($auto)) {
      $summary[] = t('Auto-scroll after @auto seconds', [
        '@auto' => $auto,
      ]);
      $auto_pause = $this
        ->getSetting('autoPause');
      $auto_pause_name = $auto_pause == TRUE ? t('Yes') : t('No');
      $summary[] = t('Pause auto-scroll on hover: @auto_pause', [
        '@auto_pause' => $auto_pause_name,
      ]);
    }
    $navigation = $this
      ->getSetting('navigation');
    $navigation_name = empty($navigation) ? t('Disabled') : Unicode::ucfirst($navigation);
    $summary[] = t('Navigation: @navigation', [
      '@navigation' => $navigation_name,
    ]);
    $animation = $this
      ->getSetting('animation');
    if (!empty($animation)) {
      if (is_int($animation)) {
        $summary[] = t('Navigation: @animation seconds', [
          '@animation' => $animation,
        ]);
      }
      else {
        $summary[] = t('Navigation: @animation', [
          '@animation' => $animation,
        ]);
      }
    }
    $easing = $this
      ->getSetting('easing');
    if (!empty($easing)) {
      $summary[] = t('Easing effect: @easing', [
        '@easing' => $easing,
      ]);
    }
    $start = $this
      ->getSetting('start');
    $summary[] = t('Start position: @start', [
      '@start' => $start,
    ]);
    $vertical = $this
      ->getSetting('vertical');
    if ($vertical == TRUE) {
      $summary[] = t('Vertical: Yes');
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $jcarousel_items = parent::viewElements($items, $langcode);
    $jcarousel_options = $this
      ->getSettings();
    unset($jcarousel_items['image_link']);
    unset($jcarousel_items['image_style']);
    $elements[] = [
      '#theme' => 'jcarousel',
      '#items' => $jcarousel_items,
      '#options' => $jcarousel_options,
    ];
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
ImageFormatter::$currentUser protected property The current user.
ImageFormatter::$imageStyleStorage protected property The image style entity storage.
ImageFormatter::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides PluginSettingsBase::calculateDependencies
ImageFormatter::create public static function Creates an instance of the plugin. Overrides FormatterBase::create 1
ImageFormatter::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsBase::onDependencyRemoval
ImageFormatter::__construct public function Constructs an ImageFormatter object. Overrides FormatterBase::__construct 1
ImageFormatterBase::getEntitiesToView protected function Returns the referenced entities for display. Overrides EntityReferenceFormatterBase::getEntitiesToView
JCarouselFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides ImageFormatter::defaultSettings
JCarouselFormatter::getSkins public function Returns keyed array of jCarousel skins.
JCarouselFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides ImageFormatter::settingsForm
JCarouselFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides ImageFormatter::settingsSummary
JCarouselFormatter::skinsManager protected function Wraps the skins manager.
JCarouselFormatter::viewElements public function Builds a renderable array for a field value. Overrides ImageFormatter::viewElements
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::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::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.