class FFTFormatter in Field Formatter Template 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/FFTFormatter.php \Drupal\fft\Plugin\Field\FieldFormatter\FFTFormatter
Plugin implementation of the 'fft_formatter' formatter.
Plugin annotation
@FieldFormatter(
id = "fft_formatter",
label = @Translation("Formatter Template"),
field_types = {
"text",
"text_long",
"text_with_summary",
},
)
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\fft\Plugin\Field\FieldFormatter\FFTFormatter
- 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 FFTFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ FFTFormatter.php, line 22
Namespace
Drupal\fft\Plugin\Field\FieldFormatterView source
class FFTFormatter extends EntityReferenceFormatterBase {
const FFT_PREPROCESS = 'fft.preprocess';
/**
* Check is reference type.
*
* @param string $type
* The type of field.
*
* @return bool
* Return value.
*/
private static function isReference($type) {
return substr($type, 0, 16) === 'entity_reference';
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'template' => '',
'image_style_1' => '',
'image_style_2' => '',
'settings' => '',
'reset' => 1,
'isNew' => 1,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$settings = $this
->getSettings();
$field['type'] = $this->fieldDefinition
->getType();
$fft_templates = fft_get_templates();
$options_set = $fft_templates['templates'];
$form['#attached']['library'][] = 'fft/backend';
if ($settings['isNew'] == 0) {
$fft_templates['settings'][$settings['template']] = $settings['settings'];
}
$form['#attached']['drupalSettings']['fft'] = !empty($fft_templates['settings']) ? $fft_templates['settings'] : [];
$form['isNew'] = [
'#type' => 'hidden',
'#value' => 0,
];
$form['template'] = [
'#title' => $this
->t('Template'),
'#type' => 'select',
'#options' => $options_set,
'#default_value' => $settings['template'],
'#attributes' => [
'class' => [
'fft-template',
],
],
];
$form['reset'] = [
'#type' => 'select',
'#title' => $this
->t('Reset'),
'#options' => [
0 => 'No',
1 => 'Yes',
],
'#default_value' => $settings['reset'],
'#description' => $this
->t('Reset default Drupal field wrapper markup.'),
];
switch ($field['type']) {
case 'image':
$image_style_options = image_style_options();
$form['image_style_1'] = [
'#type' => 'select',
'#title' => $this
->t('Image Styles 1'),
'#options' => $image_style_options,
'#default_value' => $settings['image_style_1'],
];
$form['image_style_2'] = [
'#type' => 'select',
'#title' => $this
->t('Image Styles 2'),
'#options' => $image_style_options,
'#default_value' => $settings['image_style_2'],
];
break;
}
$settings_des[] = $this
->t('Add settings extras for template, one setting per line with syntax key = value.');
$settings_des[] = $this
->t('Support array like key[] = value or key[name] = value.');
$form['settings'] = [
'#type' => 'textarea',
'#title' => $this
->t('Settings Extras'),
'#default_value' => '',
'#attributes' => [
'class' => [
'fft-settings',
],
],
];
$form['settings_des'] = [
'#type' => 'details',
'#title' => $this
->t('More Information'),
'#open' => FALSE,
];
$form['settings_des']['info'] = [
'#type' => 'markup',
'#markup' => nl2br(implode("\r\n", $settings_des)),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Formatter Template');
if ($this
->getSetting('template') != '') {
$fft_template = fft_get_templates('fft');
if (!empty($fft_template['templates'])) {
foreach ($fft_template['templates'] as $name => $title) {
if ($this
->getSetting('template') == $name) {
$summary = [];
$summary[] = $this
->t('Formatter Template: @template', [
'@template' => $title,
]);
break;
}
}
}
}
return $summary;
}
/**
* {@inheritdoc}
*
* Loads the entities referenced in that field across all the entities being
* viewed.
*/
public function prepareView(array $entities_items) {
$field_type = $this->fieldDefinition
->getType();
if (!$this
->isReference($field_type)) {
return;
}
parent::prepareView($entities_items);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$entity = $items
->getEntity();
$field_type = $this->fieldDefinition
->getType();
$settings = $this
->getSettings();
$data_items = $items;
if ($this
->isReference($field_type)) {
$data_items = $this
->getEntitiesToView($items, $langcode);
}
$elements = fft_field_formatter_render($entity, $field_type, $data_items, $settings, $langcode);
return $elements;
}
/**
* {@inheritdoc}
*/
public function view(FieldItemListInterface $items, $langcode = NULL) {
$view = parent::view($items, $langcode);
if ($this->settings['reset'] === '1') {
unset($view['#theme']);
}
return $view;
}
}
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:: |
protected | function | Checks access to the given entity. | 3 |
EntityReferenceFormatterBase:: |
protected | function | Returns the referenced entities for display. | 1 |
EntityReferenceFormatterBase:: |
protected | function | Returns whether the entity referenced by an item needs to be loaded. | 1 |
FFTFormatter:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsBase:: |
|
FFTFormatter:: |
constant | |||
FFTFormatter:: |
private static | function | Check is reference type. | |
FFTFormatter:: |
public | function |
Loads the entities referenced in that field across all the entities being
viewed. Overrides EntityReferenceFormatterBase:: |
|
FFTFormatter:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FormatterBase:: |
|
FFTFormatter:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FormatterBase:: |
|
FFTFormatter:: |
public | function |
Builds a renderable array for a fully themed field. Overrides EntityReferenceFormatterBase:: |
|
FFTFormatter:: |
public | function |
Builds a renderable array for a field value. Overrides FormatterInterface:: |
|
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 |
Constructs a FormatterBase object. Overrides PluginBase:: |
11 |
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. |