abstract class Base in Double Field 8.3
Same name and namespace in other branches
- 4.x src/Plugin/Field/FieldFormatter/Base.php \Drupal\double_field\Plugin\Field\FieldFormatter\Base
Base class for Double field formatters.
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\double_field\Plugin\Field\FieldFormatter\Base
- 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 Base
3 files declare their use of Base
- DateTimezoneTest.php in tests/
src/ Functional/ DateTimezoneTest.php - TestBase.php in tests/
src/ Functional/ TestBase.php - TestBase.php in tests/
src/ FunctionalJavascript/ TestBase.php
File
- src/
Plugin/ Field/ FieldFormatter/ Base.php, line 17
Namespace
Drupal\double_field\Plugin\Field\FieldFormatterView source
abstract class Base extends FormatterBase {
/**
* Subfield types that can be rendered as a link.
*
* @var array
*/
protected static $linkTypes = [
'email',
'telephone',
'uri',
];
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = [];
foreach ([
'first',
'second',
] as $subfield) {
$settings[$subfield] = [
// Hidden option are useful to display data with Views module.
'hidden' => FALSE,
'prefix' => '',
'suffix' => '',
'link' => FALSE,
'format_type' => 'medium',
// @todo Create tests for this options.
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => 2,
];
}
return $settings + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$settings = $this
->getSettings();
$field_settings = $this
->getFieldSettings();
$types = DoubleFieldItem::subfieldTypes();
$element = [];
// General settings.
foreach ([
'first',
'second',
] as $subfield) {
$type = $field_settings['storage'][$subfield]['type'];
$title = $subfield == 'first' ? $this
->t('First subfield') : $this
->t('Second subfield');
$title .= ' - ' . $types[$type];
$element[$subfield] = [
'#title' => $title,
'#type' => 'details',
];
$element[$subfield]['link'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display as link'),
'#default_value' => $settings[$subfield]['link'],
'#weight' => -10,
'#access' => in_array($type, static::$linkTypes),
];
if ($type == 'datetime_iso8601') {
$format_types = DateFormat::loadMultiple();
$date_formatter = self::getDateFormatter();
$time = new DrupalDateTime();
$options = [];
foreach ($format_types as $type => $type_info) {
$format = $date_formatter
->format($time
->getTimestamp(), $type);
$options[$type] = $type_info
->label() . ' (' . $format . ')';
}
$element[$subfield]['format_type'] = [
'#type' => 'select',
'#title' => $this
->t('Date format'),
'#description' => $this
->t('Choose a format for displaying the date.'),
'#options' => $options,
'#default_value' => $settings[$subfield]['format_type'],
];
}
else {
$element[$subfield]['format_type'] = [
'#type' => 'value',
'#default_value' => $settings[$subfield]['format_type'],
];
}
$element[$subfield]['hidden'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hidden'),
'#default_value' => $settings[$subfield]['hidden'],
];
$element[$subfield]['prefix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Prefix (deprecated)'),
'#size' => 30,
'#default_value' => $settings[$subfield]['prefix'],
];
$element[$subfield]['suffix'] = [
'#type' => 'textfield',
'#title' => $this
->t('Suffix (deprecated)'),
'#size' => 30,
'#default_value' => $settings[$subfield]['suffix'],
];
// BC Layer. The settings below may not be set if site was updated from
// version below 3.3.
// @todo Remove this in 4.0.
$default_settings = self::defaultSettings()[$subfield];
$settings[$subfield]['scale'] = $settings[$subfield]['scale'] ?? $default_settings['scale'];
$settings[$subfield]['decimal_separator'] = $settings[$subfield]['decimal_separator'] ?? $default_settings['decimal_separator'];
$settings[$subfield]['thousand_separator'] = $settings[$subfield]['thousand_separator'] ?? $default_settings['thousand_separator'];
if ($type == 'numeric' || $type == 'float' || $type == 'integer') {
$options = [
'' => $this
->t('- None -'),
'.' => $this
->t('Decimal point'),
',' => $this
->t('Comma'),
' ' => $this
->t('Space'),
chr(8201) => $this
->t('Thin space'),
"'" => $this
->t('Apostrophe'),
];
$element[$subfield]['thousand_separator'] = [
'#type' => 'select',
'#title' => $this
->t('Thousand marker'),
'#options' => $options,
'#default_value' => $settings[$subfield]['thousand_separator'],
];
}
else {
$element[$subfield]['thousand_separator'] = [
'#type' => 'value',
'#default_value' => $settings[$subfield]['thousand_separator'],
];
}
if ($type == 'numeric' || $type == 'float') {
$element[$subfield]['decimal_separator'] = [
'#type' => 'select',
'#title' => $this
->t('Decimal marker'),
'#options' => [
'.' => $this
->t('Decimal point'),
',' => $this
->t('Comma'),
],
'#default_value' => $settings[$subfield]['decimal_separator'],
];
$element[$subfield]['scale'] = [
'#type' => 'number',
'#title' => $this
->t('Scale', [], [
'context' => 'decimal places',
]),
'#min' => 0,
'#max' => 10,
'#default_value' => $settings[$subfield]['scale'],
'#description' => $this
->t('The number of digits to the right of the decimal.'),
];
}
else {
$element[$subfield]['decimal_separator'] = [
'#type' => 'value',
'#default_value' => $settings[$subfield]['decimal_separator'],
];
$element[$subfield]['scale'] = [
'#type' => 'value',
'#default_value' => $settings[$subfield]['scale'],
];
}
}
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$settings = $this
->getSettings();
$field_settings = $this
->getFieldSettings();
$subfield_types = DoubleFieldItem::subfieldTypes();
$summary = [];
foreach ([
'first',
'second',
] as $subfield) {
$subfield_type = $field_settings['storage'][$subfield]['type'];
$summary[] = new FormattableMarkup('<b>@subfield - @subfield_type</b>', [
'@subfield' => $subfield == 'first' ? $this
->t('First subfield') : $this
->t('Second subfield'),
'@subfield_type' => strtolower($subfield_types[$subfield_type]),
]);
if ($subfield_type == 'datetime_iso8601') {
$summary[] = $this
->t('Date format: @format', [
'@format' => $settings[$subfield]['format_type'],
]);
}
if (in_array($subfield_type, static::$linkTypes)) {
$summary[] = $this
->t('Link: @value', [
'@value' => $settings[$subfield]['link'] ? $this
->t('yes') : $this
->t('no'),
]);
}
$summary[] = $this
->t('Hidden: @value', [
'@value' => $settings[$subfield]['hidden'] ? $this
->t('yes') : $this
->t('no'),
]);
if ($settings[$subfield]['prefix'] != '') {
$summary[] = $this
->t('Prefix (deprecated): @prefix', [
'@prefix' => $settings[$subfield]['prefix'],
]);
}
if ($settings[$subfield]['suffix'] != '') {
$summary[] = $this
->t('Suffix (deprecated): @suffix', [
'@suffix' => $settings[$subfield]['suffix'],
]);
}
if ($subfield_type == 'numeric' || $subfield_type == 'float' || $subfield_type == 'integer') {
$summary[] = $this
->t('Number format: @format', [
'@format' => $this
->numberFormat($subfield, 1234.123456789),
]);
}
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function view(FieldItemListInterface $items, $langcode = NULL) {
$elements = [];
if (count($items) > 0) {
// A field may appear multiple times in a single view. Since items are
// passed by reference we need to ensure they are processed only once.
$items = clone $items;
$this
->prepareItems($items);
$elements = parent::view($items, $langcode);
}
return $elements;
}
/**
* Prepare field items.
*
* @param \Drupal\Core\Field\FieldItemListInterface $items
* List of field items.
*/
protected function prepareItems(FieldItemListInterface $items) {
$field_settings = $this
->getFieldSettings();
$settings = $this
->getSettings();
// @todo Remove this in 4.0.
foreach ([
'first',
'second',
] as $subfield) {
if ($settings[$subfield]['prefix']) {
@trigger_error('Prefix formatter setting is deprecated in double_field:8.x-3.4 and will be removed in double_field:8.x-4.0.', E_USER_DEPRECATED);
}
if ($settings[$subfield]['suffix']) {
@trigger_error('Suffix formatter setting is deprecated in double_field:8.x-3.4 and will be removed in double_field:8.x-4.0.', E_USER_DEPRECATED);
}
}
foreach ($items as $delta => $item) {
foreach ([
'first',
'second',
] as $subfield) {
if ($settings[$subfield]['hidden']) {
$item->{$subfield} = FALSE;
}
else {
$type = $field_settings['storage'][$subfield]['type'];
if ($type == 'boolean') {
$item->{$subfield} = $field_settings[$subfield][$item->{$subfield} ? 'on_label' : 'off_label'];
}
if ($type == 'numeric' || $type == 'float' || $type == 'integer') {
$item->{$subfield} = $this
->numberFormat($subfield, $item->{$subfield});
}
if ($type == 'datetime_iso8601' && $item->{$subfield} && !$field_settings[$subfield]['list']) {
// We follow the same principles as Drupal Core.
// In the case of a datetime subfield, the date must be parsed using
// the storage time zone and converted to the user's time zone while
// a date-only field should have no timezone conversion performed.
$timezone = $field_settings['storage'][$subfield]['datetime_type'] === 'datetime' ? date_default_timezone_get() : DoubleFieldItem::DATETIME_STORAGE_TIMEZONE;
$timestamp = $items[$delta]
->createDate($subfield)
->getTimestamp();
$date_formatter = self::getDateFormatter();
$item->{$subfield} = [
'#theme' => 'time',
'#text' => $date_formatter
->format($timestamp, $settings[$subfield]['format_type'], '', $timezone),
'#html' => FALSE,
'#attributes' => [
'datetime' => $date_formatter
->format($timestamp, 'custom', 'Y-m-d\\TH:i:s') . 'Z',
],
'#cache' => [
'contexts' => [
'timezone',
],
],
];
}
// Replace the value with its label if possible.
$original_value[$subfield] = $item->{$subfield};
if ($field_settings[$subfield]['list']) {
if (isset($field_settings[$subfield]['allowed_values'][$item->{$subfield}])) {
$item->{$subfield} = $field_settings[$subfield]['allowed_values'][$item->{$subfield}];
}
else {
$item->{$subfield} = FALSE;
}
}
if (!empty($settings[$subfield]['link'])) {
$value = isset($original_value) ? $original_value[$subfield] : $item->{$subfield};
switch ($type) {
case 'email':
$item->{$subfield} = [
'#type' => 'link',
'#title' => $item->{$subfield},
'#url' => Url::fromUri('mailto:' . $value),
];
break;
case 'telephone':
$item->{$subfield} = [
'#type' => 'link',
'#title' => $item->{$subfield},
'#url' => Url::fromUri('tel:' . rawurlencode(preg_replace('/\\s+/', '', $value))),
'#options' => [
'external' => TRUE,
],
];
break;
case 'uri':
$item->{$subfield} = [
'#type' => 'link',
'#title' => $item->{$subfield},
'#url' => Url::fromUri($value),
'#options' => [
'external' => TRUE,
],
];
break;
}
}
}
}
$items[$delta] = $item;
}
}
/**
* {@inheritdoc}
*/
protected function numberFormat($subfield, $number) {
$settings = $this
->getSetting($subfield);
if ($this
->getFieldSetting('storage')[$subfield]['type'] == 'integer') {
$settings['scale'] = 0;
}
// BC Layer. The settings below may not be set if site was updated from
// version below 3.3.
// @todo Remove this in 4.0.
$default_settings = self::defaultSettings()[$subfield];
$settings['scale'] = $settings['scale'] ?? $default_settings['scale'];
$settings['decimal_separator'] = $settings['decimal_separator'] ?? $default_settings['decimal_separator'];
$settings['thousand_separator'] = $settings['thousand_separator'] ?? $default_settings['thousand_separator'];
return number_format($number, $settings['scale'], $settings['decimal_separator'], $settings['thousand_separator']);
}
/**
* Returns date formatter.
*/
protected static function getDateFormatter() {
return \Drupal::service('date.formatter');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Base:: |
protected static | property | Subfield types that can be rendered as a link. | |
Base:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsBase:: |
3 |
Base:: |
protected static | function | Returns date formatter. | |
Base:: |
protected | function | ||
Base:: |
protected | function | Prepare field items. | |
Base:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FormatterBase:: |
3 |
Base:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FormatterBase:: |
3 |
Base:: |
public | function |
Builds a renderable array for a fully themed field. Overrides FormatterBase:: |
|
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 | |
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:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
11 |
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 |
FormatterBase:: |
public | function |
Allows formatters to load information for field values being displayed. Overrides FormatterInterface:: |
2 |
FormatterBase:: |
public | function |
Constructs a FormatterBase object. Overrides PluginBase:: |
11 |
FormatterInterface:: |
public | function | Builds a renderable array for a field value. | 47 |
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 |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
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 |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
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. |