You are here

class FotoramaGalleryFormatter in Fotorama Gallery 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/FotoramaGalleryFormatter.php \Drupal\fotorama_gallery\Plugin\Field\FieldFormatter\FotoramaGalleryFormatter

Plugin implementation of the 'fotorama_gallery display' formatter.

Plugin annotation


@FieldFormatter(
  id = "fotorama_gallery",
  label = @Translation("Fotorama"),
  field_types = {
    "image"
  }
)

Hierarchy

Expanded class hierarchy of FotoramaGalleryFormatter

File

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

Namespace

Drupal\fotorama_gallery\Plugin\Field\FieldFormatter
View source
class FotoramaGalleryFormatter extends ImageFormatter {

  /**
   * The Config.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * FotoramaGalleryFormatter constructor.
   *
   * @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\EntityStorageInterface $image_style_storage
   *   The image style storage.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory services.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage, ConfigFactoryInterface $config_factory) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage);
    $this->configFactory = $config_factory;
  }

  /**
   * {@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.manager')
      ->getStorage('image_style'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    $default_settings = [];
    $selects_fields = \Drupal::config('fotorama_gallery.settings')
      ->get('SelectFields');
    $check_box_fields = \Drupal::config('fotorama_gallery.settings')
      ->get('CheckBoxFields');
    $dimensions_fields = \Drupal::config('fotorama_gallery.settings')
      ->get('NumberFields');
    $all_fields = $selects_fields + $check_box_fields + $dimensions_fields;

    /* construct $default_settings array,
     * $default_settings['field-group']['key_field'] = 'defaultvalue'
     */
    foreach ($all_fields as $field) {
      $default_settings[$field['group']][$field['key']] = $field['defaultvalue'];

      // Add percent field for all dimensions fields.
      if ($field['group'] == 'dimensions') {
        $default_settings['dimensions']['percent_' . $field['key']] = FALSE;
      }
    }

    // Specials fields.
    $default_settings['dimensions']['ratio'] = '';
    return $default_settings + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);
    unset($element['image_link']);
    $url_options = [
      'attributes' => [
        'target' => '_blank',
      ],
    ];

    // Field groups.
    $element['dimensions'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Dimensions'),
      '#description' => Link::fromTextAndUrl($this
        ->t('Documentation: Dimensions'), Url::fromUri('http://fotorama.io/customize/dimensions/', $url_options)),
    ];
    $element['others'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Others'),
    ];
    $element['autoplay'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Autoplay'),
    ];
    $element['navigation'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Navigation'),
    ];
    $element['transition'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Transition'),
    ];

    // Specials fields.
    $element['dimensions']['ratio'] = [
      '#type' => 'textfield',
      '#open' => 1,
      '#title' => $this
        ->t('Ratio'),
      '#size' => 10,
      '#default_value' => $this
        ->getSetting('dimensions')['ratio'],
    ];

    // Common fields.
    $this
      ->settingsFormSelectsFields($element, $url_options);
    $this
      ->settingsFormCheckBoxFields($element, $url_options);
    $this
      ->settingsFormNumberFields($element);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary[] = "Fotorama Gallery Settings";
    $summary += parent::settingsSummary();

    // Specials fields.
    $value = $this
      ->getSetting('dimensions')['ratio'];
    if (!empty($value)) {
      $summary[] = $this
        ->t('data-ratio: @value', [
        '@value' => $value,
      ]);
    }

    // Common fields.
    $this
      ->settingsSummarySelectsFields($summary);
    $this
      ->settingsSummaryNumberFields($summary);
    $this
      ->settingsSummaryCheckBoxFields($summary);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = parent::viewElements($items, $langcode);
    $elements['#theme'] = 'fotorama_gallery_field';

    // Common fields.
    $elements['attributes'] = $this
      ->viewElementsSelectsFields() + $this
      ->viewElementsNumberFields() + $this
      ->viewElementsCheckBoxFields();

    // Specials fields.
    if (!empty($this
      ->getSetting('dimensions')['ratio'])) {
      $elements['attributes']['data-ratio'] = $this
        ->getSetting('dimensions')['ratio'];
    }
    return $elements;
  }

  /**
   * Fill $element with the select fields information from the settings.
   *
   * @param array $element
   *   Form elements.
   * @param array $url_options
   *   Url attributes.
   */
  private function settingsFormSelectsFields(array &$element, array $url_options) {
    $selects_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('SelectFields');
    foreach ($selects_fields as $field) {
      $element[$field['group']][$field['key']] = [
        '#type' => 'select',
        '#title' => $field['data'],
        '#options' => $field['options'],
        '#default_value' => $this
          ->getSetting($field['group'])[$field['key']],
        '#description' => Link::fromTextAndUrl($this
          ->t('Documentation: @field', [
          '@field' => $field['data'],
        ]), Url::fromUri($field['documentation'], $url_options)),
      ];
    }
  }

  /**
   * Fill $element with the Checkbox fields information from the settings.
   *
   * @param array $element
   *   Form elements.
   * @param array $url_options
   *   Url attributes.
   */
  private function settingsFormCheckBoxFields(array &$element, array $url_options) {
    $check_box_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('CheckBoxFields');
    foreach ($check_box_fields as $field) {
      $element[$field['group']][$field['key']] = [
        '#type' => 'checkbox',
        '#title' => $field['data'],
        '#default_value' => $this
          ->getSetting($field['group'])[$field['key']],
        '#description' => Link::fromTextAndUrl($this
          ->t('Documentation: @field', [
          '@field' => $field['data'],
        ]), Url::fromUri($field['documentation'], $url_options)),
      ];
    }
  }

  /**
   * Fill $element with the Dimension fields information from the settings.
   *
   * @param array $element
   *   Form elements.
   */
  private function settingsFormNumberFields(array &$element) {
    $dimensions_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('NumberFields');
    foreach ($dimensions_fields as $field) {
      $element['dimensions'][$field['key']] = $this
        ->fieldDimensionsNumberBuilder($field['data'], $field['key']);
      $element['dimensions'][$field['percent']] = $this
        ->fieldDimensionsCheckBoxBuilder($field['percent']);
    }
  }

  /**
   * Construct the summary for each select field.
   *
   * @param array $summary
   *   Summary array to be fill with the field information.
   */
  private function settingsSummarySelectsFields(array &$summary) {
    $selects_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('SelectFields');
    foreach ($selects_fields as $field) {
      $value = $this
        ->getSetting($field['group'])[$field['key']];
      if ($value != $field['defaultvalue']) {
        $summary[] = $this
          ->t('@label: @value', [
          '@label' => $field['data'],
          '@value' => $field['options'][$value],
        ]);
      }
    }
  }

  /**
   * Construct the summary for each dimension field.
   *
   * @param array $summary
   *   Summary array to be fill with the field information.
   */
  private function settingsSummaryNumberFields(array &$summary) {
    $dimensions_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('NumberFields');
    foreach ($dimensions_fields as $field) {
      $value = $this
        ->getSetting('dimensions')[$field['key']];
      $value_formatted = $this
        ->getNumberFieldsValuePercent($field['key'], $field['percent']);
      if (!empty($value)) {
        $summary[] = $this
          ->t('@label: @value', [
          '@label' => $field['data'],
          '@value' => $value_formatted,
        ]);
      }
    }
  }

  /**
   * Construct the summary for each checkbox field.
   *
   * @param array $summary
   *   Summary array to be fill with the field information.
   */
  private function settingsSummaryCheckBoxFields(array &$summary) {
    $check_box_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('CheckBoxFields');
    foreach ($check_box_fields as $field) {
      $value = $this
        ->getSetting($field['group'])[$field['key']];
      if ($value != $field['defaultvalue']) {
        $value = $value ? 'true' : 'false';
        $summary[] = $this
          ->t('@label: @value', [
          '@label' => $field['data'],
          '@value' => $value,
        ]);
      }
    }
  }

  /**
   * Get the value for each Select field.
   *
   * @return array
   *   Array with value for each field.
   */
  private function viewElementsSelectsFields() {
    $selects_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('SelectFields');
    $attributes = [];
    foreach ($selects_fields as $field) {
      $value = $this
        ->getSetting($field['group'])[$field['key']];
      if (array_search($field['options'][$value], $field['options']) != $field['defaultvalue']) {
        $attributes[$field['data']] = $field['options'][$value];
      }
    }
    return $attributes;
  }

  /**
   * Get the value for each Checkbox field.
   *
   * @return array
   *   Array with value for each field.
   */
  private function viewElementsCheckBoxFields() {
    $check_box_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('CheckBoxFields');
    $attributes = [];
    foreach ($check_box_fields as $field) {
      $value = $this
        ->getSetting($field['group'])[$field['key']];
      $value_formatted = $value ? 'true' : 'false';
      $value_boolean = $value ? 1 : 0;
      if ($field['defaultvalue'] != $value_boolean) {
        $attributes[$field['data']] = $value_formatted;
      }
    }
    return $attributes;
  }

  /**
   * Get the value for each dimension field.
   *
   * @return array
   *   Array with value for each field.
   */
  private function viewElementsNumberFields() {
    $dimensions_fields = $this->configFactory
      ->get('fotorama_gallery.settings')
      ->get('NumberFields');
    $attributes = [];
    foreach ($dimensions_fields as $field) {
      $value = $this
        ->getSetting('dimensions')[$field['key']];
      $value_formatted = $this
        ->getNumberFieldsValuePercent($field['key'], $field['percent']);
      if ($value) {
        $attributes[$field['data']] = $value_formatted;
      }
    }
    return $attributes;
  }

  /**
   * Return a array with a field number settings.
   *
   * @param string $label
   *   Label of the field.
   * @param string $field_key
   *   Key of the field to construct.
   *
   * @return array
   *   The array of field number settings.
   */
  private function fieldDimensionsNumberBuilder($label, $field_key) {
    return [
      '#title' => $label,
      '#type' => 'number',
      '#size' => 4,
      '#default_value' => $this
        ->getSetting('dimensions')[$field_key],
    ];
  }

  /**
   * Return a array with a field checkbox settings.
   *
   * @param string $field_key
   *   Key of the field.
   *
   * @return array
   *   Array of checkbox settings.
   */
  private function fieldDimensionsCheckBoxBuilder($field_key) {
    return [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Check if the value is a percentage'),
      '#default_value' => $this
        ->getSetting('dimensions')[$field_key],
    ];
  }

  /**
   * Get the correct format of the field value.
   *
   * @param string $field_key
   *   Key of the field.
   * @param string $field_percent
   *   Key of the field.
   *
   * @return string
   *   Value of the field with the correct format.
   */
  private function getNumberFieldsValuePercent($field_key, $field_percent) {
    $value = $this
      ->getSetting('dimensions')[$field_key];
    if (array_key_exists($field_percent, $this
      ->getSetting('dimensions')) && $this
      ->getSetting('dimensions')[$field_percent]) {
      return $value . '%';
    }
    else {
      return $value;
    }
  }

}

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
FotoramaGalleryFormatter::$configFactory protected property The Config.
FotoramaGalleryFormatter::create public static function Creates an instance of the plugin. Overrides ImageFormatter::create
FotoramaGalleryFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides ImageFormatter::defaultSettings
FotoramaGalleryFormatter::fieldDimensionsCheckBoxBuilder private function Return a array with a field checkbox settings.
FotoramaGalleryFormatter::fieldDimensionsNumberBuilder private function Return a array with a field number settings.
FotoramaGalleryFormatter::getNumberFieldsValuePercent private function Get the correct format of the field value.
FotoramaGalleryFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides ImageFormatter::settingsForm
FotoramaGalleryFormatter::settingsFormCheckBoxFields private function Fill $element with the Checkbox fields information from the settings.
FotoramaGalleryFormatter::settingsFormNumberFields private function Fill $element with the Dimension fields information from the settings.
FotoramaGalleryFormatter::settingsFormSelectsFields private function Fill $element with the select fields information from the settings.
FotoramaGalleryFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides ImageFormatter::settingsSummary
FotoramaGalleryFormatter::settingsSummaryCheckBoxFields private function Construct the summary for each checkbox field.
FotoramaGalleryFormatter::settingsSummaryNumberFields private function Construct the summary for each dimension field.
FotoramaGalleryFormatter::settingsSummarySelectsFields private function Construct the summary for each select field.
FotoramaGalleryFormatter::viewElements public function Builds a renderable array for a field value. Overrides ImageFormatter::viewElements
FotoramaGalleryFormatter::viewElementsCheckBoxFields private function Get the value for each Checkbox field.
FotoramaGalleryFormatter::viewElementsNumberFields private function Get the value for each dimension field.
FotoramaGalleryFormatter::viewElementsSelectsFields private function Get the value for each Select field.
FotoramaGalleryFormatter::__construct public function FotoramaGalleryFormatter constructor. Overrides ImageFormatter::__construct
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::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsBase::onDependencyRemoval
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::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.