WidgetBase
abstract class WidgetBase extends PluginSettingsBase implements WidgetInterface, ContainerFactoryPluginInterface (View source)
Base class for 'Field widget' plugin implementations.
Traits
Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
Provides dependency injection friendly methods for serialization.
Provides a trait for the messenger service.
Constants
| DERIVATIVE_SEPARATOR |
A string which is used to separate base plugin IDs from the derivative ID. |
Properties
| protected string | $pluginId | The plugin_id. |
from PluginBase |
| protected array | $pluginDefinition | The plugin implementation definition. |
from PluginBase |
| protected array | $configuration | Configuration information passed into the plugin. |
from PluginBase |
| protected TranslationInterface | $stringTranslation | The string translation service. |
from StringTranslationTrait |
| protected array | $_serviceIds | An array of service IDs keyed by property name used for serialization. |
from DependencySerializationTrait |
| protected array | $_entityStorages | An array of entity type IDs keyed by the property name of their storages. |
from DependencySerializationTrait |
| protected MessengerInterface | $messenger | The messenger. |
from MessengerTrait |
| protected array | $settings | The widget settings. |
|
| protected array | $thirdPartySettings | The plugin settings injected by third party modules. |
from PluginSettingsBase |
| protected bool | $defaultSettingsMerged | Whether default settings have been merged into the current $settings. |
from PluginSettingsBase |
| protected FieldDefinitionInterface | $fieldDefinition | The field definition. |
Methods
Constructs a WidgetBase object.
Translates a string to the current language or to a given language.
Formats a string containing a count of items.
Returns the number of plurals supported by a given language.
Gets the string translation service.
Defines the default settings for this plugin.
Returns the array of settings, including defaults for missing settings.
Returns the value of a setting, or its default value if absent.
Sets the value of a setting for the plugin.
Gets all third-party settings of a given module.
Gets the value of a third-party setting.
Sets the value of a third-party setting.
Unsets a third-party setting.
Gets the list of third parties that store information.
Calculates dependencies for the configured plugin.
Informs the plugin that some configuration it depends on will be deleted.
Creates an instance of the plugin.
Creates a form element for a field.
Special handling to create form elements for multiple values.
After-build handler for field elements in a form.
Submission handler for the "Add another item" button.
Ajax callback for the "Add another item" button.
Generates the form element for a single copy of the widget.
Extracts field values from submitted form values.
Reports field-level validation errors against actual form elements.
Retrieves processing information about the widget from $form_state.
Stores processing information about the widget in $form_state.
Returns the location of processing information within $form_state.
Returns a form to configure settings for the widget.
Returns a short summary for the current widget settings.
Assigns a field-level validation error to the right widget sub-element.
Massages the form values into the format expected for field values.
Returns the array of field settings.
Returns the value of a field setting.
Returns whether the widget handles multiple values.
Returns if the widget can be used for the provided field.
Returns whether the widget used for default value form.
Returns the filtered field description.
Details
__construct(string $plugin_id, mixed $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings)
Constructs a WidgetBase object.
string
getPluginId()
Gets the plugin_id of the plugin instance.
string
getBaseId()
Gets the base_plugin_id of the plugin instance.
string|null
getDerivativeId()
Gets the derivative_id of the plugin instance.
array
getPluginDefinition()
Gets the definition of the plugin implementation.
bool
isConfigurable()
Determines if the plugin is configurable.
protected TranslatableMarkup
t(string $string, array $args = [], array $options = [])
Translates a string to the current language or to a given language.
See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.
In order for strings to be localized, make them available in one of the ways supported by the
protected
formatPlural($count, $singular, $plural, array $args = [], array $options = [])
Formats a string containing a count of items.
protected
getNumberOfPlurals($langcode = NULL)
Returns the number of plurals supported by a given language.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.
$this
setStringTranslation(TranslationInterface $translation)
Sets the string translation service to use.
__sleep()
{@inheritdoc}
__wakeup()
{@inheritdoc}
setMessenger(MessengerInterface $messenger)
Sets the messenger.
MessengerInterface
messenger()
Gets the messenger.
static array
defaultSettings()
Defines the default settings for this plugin.
array
getSettings()
Returns the array of settings, including defaults for missing settings.
mixed
getSetting(string $key)
Returns the value of a setting, or its default value if absent.
protected
mergeDefaults()
Merges default settings values into $settings.
$this
setSettings(array $settings)
Sets the settings for the plugin.
$this
setSetting(string $key, mixed $value)
Sets the value of a setting for the plugin.
array
getThirdPartySettings(string $module = NULL)
Gets all third-party settings of a given module.
mixed
getThirdPartySetting(string $module, string $key, mixed $default = NULL)
Gets the value of a third-party setting.
$this
setThirdPartySetting(string $module, string $key, mixed $value)
Sets the value of a third-party setting.
mixed
unsetThirdPartySetting(string $module, string $key)
Unsets a third-party setting.
array
getThirdPartyProviders()
Gets the list of third parties that store information.
array
calculateDependencies()
Calculates dependencies for the configured plugin.
Dependencies are saved in the plugin's configuration entity and are used to determine configuration synchronization order. For example, if the plugin integrates with specific user roles, this method should return an array of dependencies listing the specified roles.
bool
onDependencyRemoval(array $dependencies)
Informs the plugin that some configuration it depends on will be deleted.
This method allows plugins to keep their configuration up-to-date when a dependency calculated with ::calculateDependencies() is removed. For example, an entity view display contains a formatter having a setting pointing to an arbitrary config entity. When that config entity is deleted, this method is called by the view display to react to the dependency removal by updating its configuration.
This method must return TRUE if the removal event updated the plugin configuration or FALSE otherwise.
static ContainerFactoryPluginInterface
create(ContainerInterface $container, array $configuration, string $plugin_id, mixed $plugin_definition)
Creates an instance of the plugin.
array
form(FieldItemListInterface $items, array $form, FormStateInterface $form_state, int $get_delta = NULL)
Creates a form element for a field.
If the entity associated with the form is new (i.e., $entity->isNew() is TRUE), the 'default value', if any, is pre-populated. Also allows other modules to alter the form element by implementing their own hooks.
protected
formMultipleElements(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
Special handling to create form elements for multiple values.
Handles generic features for multiple fields:
- number of widgets
- AHAH-'add more' button
- table display and drag-n-drop value reordering
static
afterBuild(array $element, FormStateInterface $form_state)
After-build handler for field elements in a form.
This stores the final location of the field within the form structure so that flagErrors() can assign validation errors to the right form element.
static
addMoreSubmit(array $form, FormStateInterface $form_state)
Submission handler for the "Add another item" button.
static
addMoreAjax(array $form, FormStateInterface $form_state)
Ajax callback for the "Add another item" button.
This returns the new page content to replace the page content made obsolete by the form submission.
protected
formSingleElement(FieldItemListInterface $items, $delta, array $element, array $form, FormStateInterface $form_state)
Generates the form element for a single copy of the widget.
extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
Extracts field values from submitted form values.
flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state)
Reports field-level validation errors against actual form elements.
static array
getWidgetState(array $parents, string $field_name, FormStateInterface $form_state)
Retrieves processing information about the widget from $form_state.
This method is static so that it can be used in static Form API callbacks.
static
setWidgetState(array $parents, string $field_name, FormStateInterface $form_state, array $field_state)
Stores processing information about the widget in $form_state.
This method is static so that it can be used in static Form API #callbacks.
static protected array
getWidgetStateParents(array $parents, string $field_name)
Returns the location of processing information within $form_state.
array
settingsForm(array $form, FormStateInterface $form_state)
Returns a form to configure settings for the widget.
Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.
array
settingsSummary()
Returns a short summary for the current widget settings.
If an empty result is returned, a UI can still be provided to display a settings form in case the widget has configurable settings.
array|bool
errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state)
Assigns a field-level validation error to the right widget sub-element.
Depending on the widget's internal structure, a field-level validation error needs to be flagged on the right sub-element.
array
massageFormValues(array $values, array $form, FormStateInterface $form_state)
Massages the form values into the format expected for field values.
protected array
getFieldSettings()
Returns the array of field settings.
protected mixed
getFieldSetting(string $setting_name)
Returns the value of a field setting.
protected bool
handlesMultipleValues()
Returns whether the widget handles multiple values.
static bool
isApplicable(FieldDefinitionInterface $field_definition)
Returns if the widget can be used for the provided field.
protected bool
isDefaultValueWidget(FormStateInterface $form_state)
Returns whether the widget used for default value form.
protected FieldFilteredMarkup
getFilteredDescription()
Returns the filtered field description.