class FotoramaGalleryFormatter in Fotorama Gallery 8
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase
- class \Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter implements ContainerFactoryPluginInterface
- class \Drupal\fotorama_gallery\Plugin\Field\FieldFormatter\FotoramaGalleryFormatter
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter implements ContainerFactoryPluginInterface
- class \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase
- class \Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase
- class \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FotoramaGalleryFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ FotoramaGalleryFormatter.php, line 28
Namespace
Drupal\fotorama_gallery\Plugin\Field\FieldFormatterView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityReferenceFormatterBase:: |
public | function |
Loads the entities referenced in that field across all the entities being
viewed. Overrides FormatterBase:: |
|
EntityReferenceFormatterBase:: |
public | function |
Overrides FormatterBase:: |
|
FileFormatterBase:: |
protected | function |
Checks access to the given entity. Overrides EntityReferenceFormatterBase:: |
|
FileFormatterBase:: |
protected | function |
Returns whether the entity referenced by an item needs to be loaded. Overrides EntityReferenceFormatterBase:: |
1 |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The label display setting. | |
FormatterBase:: |
protected | property |
The formatter settings. Overrides PluginSettingsBase:: |
|
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
protected | function | Returns the value of a field setting. | |
FormatterBase:: |
protected | function | Returns the array of field settings. | |
FormatterBase:: |
public static | function |
Returns if the formatter can be used for the provided field. Overrides FormatterInterface:: |
14 |
FotoramaGalleryFormatter:: |
protected | property | The Config. | |
FotoramaGalleryFormatter:: |
public static | function |
Creates an instance of the plugin. Overrides ImageFormatter:: |
|
FotoramaGalleryFormatter:: |
public static | function |
Defines the default settings for this plugin. Overrides ImageFormatter:: |
|
FotoramaGalleryFormatter:: |
private | function | Return a array with a field checkbox settings. | |
FotoramaGalleryFormatter:: |
private | function | Return a array with a field number settings. | |
FotoramaGalleryFormatter:: |
private | function | Get the correct format of the field value. | |
FotoramaGalleryFormatter:: |
public | function |
Returns a form to configure settings for the formatter. Overrides ImageFormatter:: |
|
FotoramaGalleryFormatter:: |
private | function | Fill $element with the Checkbox fields information from the settings. | |
FotoramaGalleryFormatter:: |
private | function | Fill $element with the Dimension fields information from the settings. | |
FotoramaGalleryFormatter:: |
private | function | Fill $element with the select fields information from the settings. | |
FotoramaGalleryFormatter:: |
public | function |
Returns a short summary for the current formatter settings. Overrides ImageFormatter:: |
|
FotoramaGalleryFormatter:: |
private | function | Construct the summary for each checkbox field. | |
FotoramaGalleryFormatter:: |
private | function | Construct the summary for each dimension field. | |
FotoramaGalleryFormatter:: |
private | function | Construct the summary for each select field. | |
FotoramaGalleryFormatter:: |
public | function |
Builds a renderable array for a field value. Overrides ImageFormatter:: |
|
FotoramaGalleryFormatter:: |
private | function | Get the value for each Checkbox field. | |
FotoramaGalleryFormatter:: |
private | function | Get the value for each dimension field. | |
FotoramaGalleryFormatter:: |
private | function | Get the value for each Select field. | |
FotoramaGalleryFormatter:: |
public | function |
FotoramaGalleryFormatter constructor. Overrides ImageFormatter:: |
|
ImageFormatter:: |
protected | property | The current user. | |
ImageFormatter:: |
protected | property | The image style entity storage. | |
ImageFormatter:: |
public | function |
Calculates dependencies for the configured plugin. Overrides PluginSettingsBase:: |
|
ImageFormatter:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsBase:: |
|
ImageFormatterBase:: |
protected | function |
Returns the referenced entities for display. Overrides EntityReferenceFormatterBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |