You are here

class ImagefieldSlideshowFieldFormatter in Imagefield Slideshow 8

Plugin implementation of 'imagefield_slideshow_field_formatter' formatter.

Plugin annotation


@FieldFormatter(
  id = "imagefield_slideshow_field_formatter",
  label = @Translation("Imagefield Slideshow"),
  field_types = {
    "image"
  }
)

Hierarchy

Expanded class hierarchy of ImagefieldSlideshowFieldFormatter

File

src/Plugin/Field/FieldFormatter/ImagefieldSlideshowFieldFormatter.php, line 28

Namespace

Drupal\imagefield_slideshow\Plugin\Field\FieldFormatter
View source
class ImagefieldSlideshowFieldFormatter extends ImageFormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The image style entity storage.
   *
   * @var \Drupal\image\ImageStyleStorageInterface
   */
  protected $imageStyleStorage;

  /**
   * Constructs an ImageFormatter object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings settings.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   Thrown if the entity type doesn't exist.
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   *   Thrown if the storage handler couldn't be loaded.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
    $this->imageStyleStorage = $this->entityTypeManager
      ->getStorage('image_style');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
      ->get('current_user'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      // Implement default settings.
      'imagefield_slideshow_style' => 'large',
      'imagefield_slideshow_style_effects' => 'fade',
      'imagefield_slideshow_style_pause' => 'false',
      'imagefield_slideshow_prev_next' => 'true',
      'imagefield_slideshow_transition_speed' => 100,
      'imagefield_slideshow_timeout' => 100,
      'imagefield_slideshow_pager' => TRUE,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $image_styles = image_style_options(FALSE);
    $description_link = Link::fromTextAndUrl($this
      ->t("Configure Image Styles"), Url::fromRoute('entity.image_style.collection'));
    $element['imagefield_slideshow_style'] = [
      '#title' => t("Image style"),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('imagefield_slideshow_style'),
      '#empty_option' => t("None (original image)"),
      '#options' => $image_styles,
      '#description' => $description_link
        ->toRenderable() + [
        '#access' => $this->currentUser
          ->hasPermission('administer image styles'),
      ],
    ];
    $effects = [
      'none' => 'none',
      // 'blindX' => 'blindX',
      //      'blindY' => 'blindY',
      //      'blindZ' => 'blindZ',
      //      'cover' => 'cover',
      //      'curtainX' => 'curtainX',
      //      'curtainY' => 'curtainY',
      'fade' => 'fade',
      'fadeout' => 'fadeout',
      // 'fadeZoom' => 'fadeZoom',
      //      'growX' => 'growX',
      //      'growY' => 'growY',
      //      'scrollUp' => 'scrollUp',
      //      'scrollDown' => 'scrollDown',
      //      'scrollLeft' => 'scrollLeft',
      //      'scrollRight' => 'scrollRight',
      'scrollHorz' => 'scrollHorz',
      // 'scrollVert' => 'scrollVert',
      //      'shuffle' => 'shuffle',
      //      'slideX' => 'slideX',
      //      'slideY' => 'slideY',
      //      'toss' => 'toss',
      //      'turnUp' => 'turnUp',
      //      'turnDown' => 'turnDown',
      //      'turnLeft' => 'turnLeft',
      //      'turnRight' => 'turnRight',
      //      'uncover' => 'uncover',
      //      'wipe' => 'wipe',
      //      'zoom' => 'zoom',
      'flipHorz' => 'flipHorz',
      'flipVert' => 'flipVert',
      'shuffle' => 'shuffle',
    ];
    $element['imagefield_slideshow_style_effects'] = [
      '#type' => 'select',
      '#title' => t("Effect"),
      '#options' => $effects,
      '#default_value' => $this
        ->getSetting('imagefield_slideshow_style_effects'),
      '#description' => t("The transition effect that will be used to change between images. Not all options below may be relevant depending on the effect. <a href='http://jquery.malsup.com/cycle/browser.html' target='_black'>Follow this link to see examples of each effect.</a>"),
    ];
    $image_pause = [
      'true' => "Yes",
      'false' => "No",
    ];
    $element['imagefield_slideshow_style_pause'] = [
      '#title' => t("Image pause"),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting('imagefield_slideshow_style_pause'),
      '#options' => $image_pause,
      '#description' => t("Should image be paused on hover."),
    ];
    $element['imagefield_slideshow_prev_next'] = [
      '#title' => $this
        ->t("Enable Prev & Next button"),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('imagefield_slideshow_prev_next'),
      '#description' => $this
        ->t('This will show the Prev and Next button for slideshow.'),
    ];
    $range0 = array_combine(range(100, 1000, 100), range(100, 1000, 100));
    $range1 = array_combine(range(2000, 10000, 1000), range(2000, 10000, 1000));
    $transition_speed = array_replace($range0, $range1);
    $element['imagefield_slideshow_transition_speed'] = [
      '#type' => 'select',
      '#title' => t("Transition Speed"),
      '#options' => $transition_speed,
      '#default_value' => $this
        ->getSetting('imagefield_slideshow_transition_speed'),
      '#description' => t("The transition speed between images."),
    ];
    $range3 = array_combine(range(0, 1000, 100), range(0, 1000, 100));
    $timeout = array_replace($range3, $range1);
    $element['imagefield_slideshow_timeout'] = [
      '#type' => 'select',
      '#title' => t("Timeout"),
      '#options' => $timeout,
      '#default_value' => $this
        ->getSetting('imagefield_slideshow_timeout'),
      '#description' => t("The timeout for slides."),
    ];
    $element['imagefield_slideshow_pager'] = [
      '#title' => $this
        ->t("Enable Pager ?"),
      '#type' => 'checkbox',
      '#default_value' => $this
        ->getSetting('imagefield_slideshow_pager'),
      '#description' => $this
        ->t('This will show the Pager on slideshow.'),
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];

    // Implement settings summary.
    $image_styles = image_style_options(FALSE);

    // Unset possible 'No defined styles' option.
    unset($image_styles['']);

    // Styles could be lost because of enabled/disabled modules that defines
    // their styles in code.
    $image_style_setting = $this
      ->getSetting('imagefield_slideshow_style');
    if (isset($image_styles[$image_style_setting])) {
      $summary[] = t("Image style: @style", [
        '@style' => $image_styles[$image_style_setting],
      ]);
    }
    else {
      $summary[] = $this
        ->t("Original image");
    }
    $image_style_effect = $this
      ->getSetting('imagefield_slideshow_style_effects');
    if (isset($image_style_effect)) {
      $summary[] .= $this
        ->t("Effect: @image_style_effect", [
        "@image_style_effect" => $image_style_effect,
      ]);
    }
    $image_style_pause = $this
      ->getSetting('imagefield_slideshow_style_pause');
    if (!empty($image_style_pause)) {
      $summary[] .= $this
        ->t("Pause: @image_style_pause", [
        "@image_style_pause" => $image_style_pause,
      ]);
    }
    $image_prev_next = $this
      ->getSetting('imagefield_slideshow_prev_next');
    if ($image_prev_next) {
      $summary[] .= $this
        ->t("Prev & Next: @image_prev_next", [
        "@image_prev_next" => $image_prev_next,
      ]);
    }
    $image_transition_speed = $this
      ->getSetting('imagefield_slideshow_transition_speed');
    if ($image_transition_speed) {
      $summary[] .= $this
        ->t("Transition Speed: @image_transition_speed fx", [
        "@image_transition_speed" => $image_transition_speed,
      ]);
    }
    $image_slideshow_timeout = $this
      ->getSetting('imagefield_slideshow_timeout');
    if ($image_slideshow_timeout) {
      $summary[] .= $this
        ->t("Timeout: @image_slideshow_timeout", [
        "@image_slideshow_timeout" => $image_slideshow_timeout,
      ]);
    }
    $image_pager = $this
      ->getSetting('imagefield_slideshow_pager');
    if ($image_pager) {
      $summary[] .= $this
        ->t("Pager: @image_pager", [
        "@image_pager" => $image_pager,
      ]);
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];

    // Get the Entity value as array.
    $file = $items
      ->getEntity()
      ->toArray();

    // Early opt-out if the field is empty.
    $images = $this
      ->getEntitiesToView($items, $langcode);
    if (empty($images)) {
      return $elements;
    }
    $image_style_setting = $this
      ->getSetting('imagefield_slideshow_style');
    $image_style = NULL;
    if (!empty($image_style_setting)) {
      $image_style = \Drupal::entityTypeManager()
        ->getStorage('image_style')
        ->load($image_style_setting);
    }
    $image_uri_values = [];
    foreach ($images as $image) {
      $image_uri = $image
        ->getFileUri();

      // Get image style URL.
      if ($image_style) {
        $image_uri = ImageStyle::load($image_style
          ->getName())
          ->buildUrl($image_uri);
      }
      else {

        // Get absolute path for original image.
        $image_uri = $image
          ->createFileUrl(FALSE);
      }

      // Populate image uri's with fid.
      $fid = $image
        ->toArray()['fid'][0]['value'];
      $image_uri_values[$fid] = [
        'uri' => $image_uri,
      ];
    }

    // Populate the title and alt of images based on fid.
    foreach ([
      'title',
      'alt',
    ] as $element_name) {
      $field_name = $this->fieldDefinition
        ->getName();
      if (array_key_exists($field_name, $file)) {
        foreach ($file[$field_name] as $key => $value) {
          $image_uri_values[$value['target_id']]['alt'] = $value['alt'];
          $image_uri_values[$value['target_id']]['title'] = $value['title'];
        }
      }
    }

    // Enable prev next if only more than one image.
    $prev_next = $this
      ->getSetting('imagefield_slideshow_prev_next');
    if (count($image_uri_values) <= 1) {
      $prev_next = FALSE;
    }
    $elements[] = [
      '#theme' => 'imagefield_slideshow',
      '#url' => $image_uri_values,
      '#prev_next' => $prev_next,
      '#effect' => $this
        ->getSetting('imagefield_slideshow_style_effects'),
      '#pause' => $this
        ->getSetting('imagefield_slideshow_style_pause'),
      '#speed' => $this
        ->getSetting('imagefield_slideshow_transition_speed'),
      '#timeout' => $this
        ->getSetting('imagefield_slideshow_timeout'),
      '#pager' => $this
        ->getSetting('imagefield_slideshow_pager'),
    ];

    // Attach the image field slide show library.
    $elements['#attached']['library'][] = 'imagefield_slideshow/imagefield_slideshow';

    // Not to cache this field formatter.
    $elements['#cache']['max-age'] = 0;
    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
ImagefieldSlideshowFieldFormatter::$currentUser protected property The current user.
ImagefieldSlideshowFieldFormatter::$entityTypeManager protected property The entity type manager service.
ImagefieldSlideshowFieldFormatter::$imageStyleStorage protected property The image style entity storage.
ImagefieldSlideshowFieldFormatter::create public static function Creates an instance of the plugin. Overrides FormatterBase::create
ImagefieldSlideshowFieldFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsBase::defaultSettings
ImagefieldSlideshowFieldFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
ImagefieldSlideshowFieldFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
ImagefieldSlideshowFieldFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements
ImagefieldSlideshowFieldFormatter::__construct public function Constructs an ImageFormatter object. Overrides FormatterBase::__construct
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.