abstract class FieldWrapperBase in (Entity Reference) Field Formatters 8
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/FieldWrapperBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldWrapperBase
- 3.x src/Plugin/Field/FieldFormatter/FieldWrapperBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldWrapperBase
Wraps an existing field.
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\field_formatter\Plugin\Field\FieldFormatter\FieldWrapperBase implements ContainerFactoryPluginInterface
- 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 FieldWrapperBase
File
- src/
Plugin/ Field/ FieldFormatter/ FieldWrapperBase.php, line 21
Namespace
Drupal\field_formatter\Plugin\Field\FieldFormatterView source
abstract class FieldWrapperBase extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* Entity view display.
*
* @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
*/
protected $viewDisplay;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The formatter plugin manager.
*
* @var \Drupal\Core\Field\FormatterPluginManager
*/
protected $formatterPluginManager;
/**
* Constructs a FieldFormatterWithInlineSettings 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.
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
* The formatter plugin manager.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FormatterPluginManager $formatter_plugin_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->formatterPluginManager = $formatter_plugin_manager;
}
/**
* {@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('plugin.manager.field.formatter'));
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'type' => '',
'settings' => [],
];
}
/**
* Get field definition for given field storage definition.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition
* The field storage definition.
*
* @return \Drupal\Core\Field\BaseFieldDefinition
* The field definition.
*/
protected function getFieldDefinition(FieldStorageDefinitionInterface $field_storage_definition) {
return BaseFieldDefinition::createFromFieldStorageDefinition($field_storage_definition);
}
/**
* Get all available formatters by loading available ones and filtering out.
*
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition
* The field storage definition.
*
* @return string[]
* The field formatter labels keys by plugin ID.
*/
protected function getAvailableFormatterOptions(FieldStorageDefinitionInterface $field_storage_definition) {
$formatters = $this->formatterPluginManager
->getOptions($field_storage_definition
->getType());
$formatter_instances = array_map(function ($formatter_id) {
// TODO: Ensure it is right to empty all values here, see:
// https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21FormatterPluginManager.php/class/FormatterPluginManager/8.2.x
$configuration = [
'field_definition' => $this->fieldDefinition,
'settings' => [],
'label' => '',
'view_mode' => '',
'third_party_settings' => [],
];
return $this->formatterPluginManager
->createInstance($formatter_id, $configuration);
}, array_combine(array_keys($formatters), array_keys($formatters)));
$filtered_formatter_instances = array_filter($formatter_instances, function (FormatterInterface $formatter) {
return $formatter
->isApplicable($this->fieldDefinition);
});
$options = array_map(function (FormatterInterface $formatter) {
return $formatter
->getPluginDefinition()['label'];
}, $filtered_formatter_instances);
// Remove field_link itself.
unset($options['field_link']);
return $options;
}
/**
* Ajax callback for fields with AJAX callback to update form substructure.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* The replaced form substructure.
*/
public static function onFormatterTypeChange(array $form, FormStateInterface $form_state) {
$triggeringElement = $form_state
->getTriggeringElement();
// Dynamically return the dependent ajax for elements based on the
// triggering element. This shouldn't be done statically because
// settings forms may be different, e.g. for layout builder, core, ...
if (!empty($triggeringElement['#array_parents'])) {
$subformKeys = $triggeringElement['#array_parents'];
// Remove the triggering element itself and add the 'settings' below key.
array_pop($subformKeys);
$subformKeys[] = 'settings';
// Return the subform:
return NestedArray::getValue($form, $subformKeys);
}
}
/**
* Rebuilds the form on select submit.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public static function rebuildSubmit(array $form, FormStateInterface $form_state) {
$form_state
->setRebuild(TRUE);
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$field_storage = $this->fieldDefinition
->getFieldStorageDefinition();
$formatter_options = $this
->getAvailableFormatterOptions($field_storage);
if (!empty($formatter_options)) {
$formatter_type = $this
->getSettingFromFormState($form_state, 'type');
$settings = $this
->getSettingFromFormState($form_state, 'settings');
if (!isset($formatter_options[$formatter_type])) {
$formatter_type = key($formatter_options);
$settings = [];
}
$form['type'] = [
'#type' => 'select',
'#title' => $this
->t('Formatter'),
'#options' => $formatter_options,
'#default_value' => $formatter_type,
// Note: We cannot use ::foo syntax, because the form is the entity form
// display.
'#ajax' => [
'callback' => [
get_class(),
'onFormatterTypeChange',
],
'wrapper' => 'field-formatter-settings-ajax',
'method' => 'replace',
],
'#submit' => [
[
get_class(),
'rebuildSubmit',
],
],
'#executes_submit_callback' => TRUE,
];
$options = [
'field_definition' => $this
->getFieldDefinition($field_storage),
'configuration' => [
'type' => $formatter_type,
'settings' => $settings,
'label' => '',
'weight' => 0,
],
'view_mode' => '_custom',
];
// Get the formatter settings form.
$settings_form = [
'#value' => [],
];
if ($formatter = $this->formatterPluginManager
->getInstance($options)) {
$settings_form = $formatter
->settingsForm([], $form_state);
}
$form['settings'] = $settings_form;
$form['settings']['#prefix'] = '<div id="field-formatter-settings-ajax">';
$form['settings']['#suffix'] = '</div>';
}
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($type = $this
->getSetting('type')) {
$summary[] = $this
->t('Formatter %type used.', [
'%type' => $type,
]);
}
else {
$summary[] = $this
->t('Formatter not configured.');
}
return $summary;
}
/**
* Returns a view display object used to render the content of the field.
*
* @param string $bundle_id
* The bundle ID.
*
* @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
* Entity view display.
*/
protected function getViewDisplay($bundle_id) {
if (!isset($this->viewDisplay[$bundle_id])) {
$display = EntityViewDisplay::create([
'targetEntityType' => $this->fieldDefinition
->getTargetEntityTypeId(),
'bundle' => $bundle_id,
'status' => TRUE,
]);
$display
->setComponent($this->fieldDefinition
->getName(), [
'type' => $this
->getSetting('type'),
'settings' => $this
->getSetting('settings'),
]);
$this->viewDisplay[$bundle_id] = $display;
}
return $this->viewDisplay[$bundle_id];
}
/**
* Returns the wrapped field output.
*/
protected function getFieldOutput(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
$entity = $items
->getEntity();
$build = $this
->getViewDisplay($entity
->bundle())
->build($entity);
return isset($build[$this->fieldDefinition
->getName()]) ? $build[$this->fieldDefinition
->getName()] : [];
}
/**
* Helper function to retrieve the $setting from the $form_state.
*
* @param Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
* @param string $setting
* The setting key to retrieve.
*/
protected function getSettingFromFormState(FormStateInterface $form_state, $setting) {
$field_name = $this->fieldDefinition
->getName();
if ($form_state
->hasValue([
'fields',
$field_name,
'settings_edit_form',
'settings',
$setting,
])) {
return $form_state
->getValue([
'fields',
$field_name,
'settings_edit_form',
'settings',
$setting,
]);
}
return $this
->getSetting($setting);
}
}
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 | |
FieldWrapperBase:: |
protected | property | The entity field manager. | |
FieldWrapperBase:: |
protected | property | The formatter plugin manager. | |
FieldWrapperBase:: |
protected | property | Entity view display. | |
FieldWrapperBase:: |
public static | function |
Creates an instance of the plugin. Overrides FormatterBase:: |
|
FieldWrapperBase:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsBase:: |
|
FieldWrapperBase:: |
protected | function | Get all available formatters by loading available ones and filtering out. | |
FieldWrapperBase:: |
protected | function | Get field definition for given field storage definition. | |
FieldWrapperBase:: |
protected | function | Returns the wrapped field output. | |
FieldWrapperBase:: |
protected | function | Helper function to retrieve the $setting from the $form_state. | |
FieldWrapperBase:: |
protected | function | Returns a view display object used to render the content of the field. | |
FieldWrapperBase:: |
public static | function | Ajax callback for fields with AJAX callback to update form substructure. | |
FieldWrapperBase:: |
public static | function | Rebuilds the form on select submit. | |
FieldWrapperBase:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FormatterBase:: |
|
FieldWrapperBase:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FormatterBase:: |
|
FieldWrapperBase:: |
public | function |
Constructs a FieldFormatterWithInlineSettings object. Overrides FormatterBase:: |
|
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 |
FormatterBase:: |
public | function |
Allows formatters to load information for field values being displayed. Overrides FormatterInterface:: |
2 |
FormatterBase:: |
public | function |
Builds a renderable array for a fully themed field. Overrides FormatterInterface:: |
1 |
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. |