class DateRangeDefaultWidget extends DateRangeWidgetBase (View source)

Plugin implementation of the 'daterange_default' widget.

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.

from  WidgetBase
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.

from  WidgetBase
protected EntityStorageInterface $dateStorage

The date format storage.

Methods

__construct(string $plugin_id, mixed $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityStorageInterface $date_storage)

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.

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

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.

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.

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.

bool
onDependencyRemoval(array $dependencies)

Informs the plugin that some configuration it depends on will be deleted.

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.

formMultipleElements(FieldItemListInterface $items, array $form, FormStateInterface $form_state)

Special handling to create form elements for multiple values.

static 
afterBuild(array $element, FormStateInterface $form_state)

After-build handler for field elements in a form.

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.

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.

static 
setWidgetState(array $parents, string $field_name, FormStateInterface $form_state, array $field_state)

Stores processing information about the widget in $form_state.

static 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.

array
settingsSummary()

Returns a short summary for the current widget 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.

array
massageFormValues(array $values, array $form, FormStateInterface $form_state)

Massages the form values into the format expected for field values.

array
getFieldSettings()

Returns the array of field settings.

mixed
getFieldSetting(string $setting_name)

Returns the value of a field setting.

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.

bool
isDefaultValueWidget(FormStateInterface $form_state)

Returns whether the widget used for default value form.

getFilteredDescription()

Returns the filtered field description.

array
formElement(FieldItemListInterface $items, int $delta, array $element, array $form, FormStateInterface $form_state)

Returns the form for a single field widget.

createDefaultValue(DrupalDateTime $date, string $timezone)

Creates a date object for use as a default value.

validateStartEnd(array $element, FormStateInterface $form_state, array $complete_form)

element_validate callback to ensure that the start date <= the end date.

Details

__construct(string $plugin_id, mixed $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityStorageInterface $date_storage)

Constructs a WidgetBase object.

Parameters

string $plugin_id

The plugin_id for the plugin instance.

mixed $plugin_definition

The plugin implementation definition.

FieldDefinitionInterface $field_definition

The definition of the field to which the widget is associated.

array $settings

The widget settings.

array $third_party_settings

Any third party settings.

EntityStorageInterface $date_storage

string getPluginId()

Gets the plugin_id of the plugin instance.

Return Value

string

The plugin_id of the plugin instance.

string getBaseId()

Gets the base_plugin_id of the plugin instance.

Return Value

string

The base_plugin_id of the plugin instance.

string|null getDerivativeId()

Gets the derivative_id of the plugin instance.

Return Value

string|null

The derivative_id of the plugin instance NULL otherwise.

array getPluginDefinition()

Gets the definition of the plugin implementation.

Return Value

array

The plugin definition, as returned by the discovery object used by the plugin manager.

bool isConfigurable()

Determines if the plugin is configurable.

Return Value

bool

A boolean indicating whether 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

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

setMessenger(MessengerInterface $messenger)

Sets the messenger.

Parameters

MessengerInterface $messenger

The messenger.

MessengerInterface messenger()

Gets the messenger.

Return Value

MessengerInterface

The messenger.

static array defaultSettings()

Defines the default settings for this plugin.

Return Value

array

A list of default settings, keyed by the setting name.

array getSettings()

Returns the array of settings, including defaults for missing settings.

Return Value

array

The array of settings.

mixed getSetting(string $key)

Returns the value of a setting, or its default value if absent.

Parameters

string $key

The setting name.

Return Value

mixed

The setting value.

protected mergeDefaults()

Merges default settings values into $settings.

$this setSettings(array $settings)

Sets the settings for the plugin.

Parameters

array $settings

The array of settings, keyed by setting names. Missing settings will be assigned their default values.

Return Value

$this

$this setSetting(string $key, mixed $value)

Sets the value of a setting for the plugin.

Parameters

string $key

The setting name.

mixed $value

The setting value.

Return Value

$this

array getThirdPartySettings(string $module = NULL)

Gets all third-party settings of a given module.

Parameters

string $module

The module providing the third-party settings.

Return Value

array

An array of key-value pairs.

mixed getThirdPartySetting(string $module, string $key, mixed $default = NULL)

Gets the value of a third-party setting.

Parameters

string $module

The module providing the third-party setting.

string $key

The setting name.

mixed $default

The default value

Return Value

mixed

The value.

$this setThirdPartySetting(string $module, string $key, mixed $value)

Sets the value of a third-party setting.

Parameters

string $module

The module providing the third-party setting.

string $key

The setting name.

mixed $value

The setting value.

Return Value

$this

mixed unsetThirdPartySetting(string $module, string $key)

Unsets a third-party setting.

Parameters

string $module

The module providing the third-party setting.

string $key

The setting name.

Return Value

mixed

The value.

array getThirdPartyProviders()

Gets the list of third parties that store information.

Return Value

array

The list of third parties.

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.

Return Value

array

An array of dependencies grouped by type (config, content, module, theme). For example: @code array( 'config' => array('user.role.anonymous', 'user.role.authenticated'), 'content' => array('node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'), 'module' => array('node', 'user'), 'theme' => array('seven'), ); @endcode

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.

Parameters

array $dependencies

An array of dependencies that will be deleted keyed by dependency type. Dependency types are 'config', 'content', 'module' and 'theme'.

Return Value

bool

TRUE if the plugin configuration has changed, FALSE if not.

static ContainerFactoryPluginInterface create(ContainerInterface $container, array $configuration, string $plugin_id, mixed $plugin_definition)

Creates an instance of the plugin.

Parameters

ContainerInterface $container

The container to pull out services used in the plugin.

array $configuration

A configuration array containing information about the plugin instance.

string $plugin_id

The plugin ID for the plugin instance.

mixed $plugin_definition

The plugin implementation definition.

Return Value

ContainerFactoryPluginInterface

Returns an instance of this 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.

Parameters

FieldItemListInterface $items

An array of the field values. When creating a new entity this may be NULL or an empty array to use default values.

array $form

An array representing the form that the editing element will be attached to.

FormStateInterface $form_state

The current state of the form.

int $get_delta

Used to get only a specific delta value of a multiple value field.

Return Value

array

The form element array created for this field.

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

Parameters

FieldItemListInterface $items
array $form
FormStateInterface $form_state

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.

Parameters

array $element
FormStateInterface $form_state

static addMoreSubmit(array $form, FormStateInterface $form_state)

Submission handler for the "Add another item" button.

Parameters

array $form
FormStateInterface $form_state

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.

Parameters

array $form
FormStateInterface $form_state

protected formSingleElement(FieldItemListInterface $items, $delta, array $element, array $form, FormStateInterface $form_state)

Generates the form element for a single copy of the widget.

Parameters

FieldItemListInterface $items
$delta
array $element
array $form
FormStateInterface $form_state

extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)

Extracts field values from submitted form values.

Parameters

FieldItemListInterface $items

The field values. This parameter is altered by reference to receive the incoming form values.

array $form

The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

FormStateInterface $form_state

The form state.

flagErrors(FieldItemListInterface $items, ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state)

Reports field-level validation errors against actual form elements.

Parameters

FieldItemListInterface $items

The field values.

ConstraintViolationListInterface $violations

A list of constraint violations to flag.

array $form

The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

FormStateInterface $form_state

The form state.

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.

Parameters

array $parents

The array of #parents where the field lives in the form.

string $field_name

The field name.

FormStateInterface $form_state

The form state.

Return Value

array

An array with the following key/value pairs:

  • items_count: The number of widgets to display for the field.
  • array_parents: The location of the field's widgets within the $form structure. This entry is populated at '#after_build' time.

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.

Parameters

array $parents

The array of #parents where the widget lives in the form.

string $field_name

The field name.

FormStateInterface $form_state

The form state.

array $field_state

The array of data to store. See getWidgetState() for the structure and content of the array.

static protected array getWidgetStateParents(array $parents, string $field_name)

Returns the location of processing information within $form_state.

Parameters

array $parents

The array of #parents where the widget lives in the form.

string $field_name

The field name.

Return Value

array

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.

Parameters

array $form

The form where the settings form is being included in.

FormStateInterface $form_state

The current state of the form.

Return Value

array

The form definition for the widget settings.

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.

Return Value

array

A short summary of the widget 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.

Parameters

array $element

An array containing the form element for the widget, as generated by formElement().

ConstraintViolationInterface $error
array $form

The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

FormStateInterface $form_state

The current state of the form.

Return Value

array|bool

The element on which the error should be flagged, or FALSE to completely ignore the violation (use with care!).

array massageFormValues(array $values, array $form, FormStateInterface $form_state)

Massages the form values into the format expected for field values.

Parameters

array $values

The submitted form values produced by the widget.

  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.
array $form

The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.

FormStateInterface $form_state

The form state.

Return Value

array

An array of field values, keyed by delta.

protected array getFieldSettings()

Returns the array of field settings.

Return Value

array

The array of settings.

protected mixed getFieldSetting(string $setting_name)

Returns the value of a field setting.

Parameters

string $setting_name

The setting name.

Return Value

mixed

The setting value.

protected bool handlesMultipleValues()

Returns whether the widget handles multiple values.

Return Value

bool

TRUE if a single copy of formElement() can handle multiple field values, FALSE if multiple values require separate copies of formElement().

static bool isApplicable(FieldDefinitionInterface $field_definition)

Returns if the widget can be used for the provided field.

Parameters

FieldDefinitionInterface $field_definition

The field definition that should be checked.

Return Value

bool

TRUE if the widget can be used, FALSE otherwise.

protected bool isDefaultValueWidget(FormStateInterface $form_state)

Returns whether the widget used for default value form.

Parameters

FormStateInterface $form_state

The current state of the form.

Return Value

bool

TRUE if a widget used to input default value, FALSE otherwise.

protected FieldFilteredMarkup getFilteredDescription()

Returns the filtered field description.

Return Value

FieldFilteredMarkup

The filtered field description, with tokens replaced.

array formElement(FieldItemListInterface $items, int $delta, array $element, array $form, FormStateInterface $form_state)

Returns the form for a single field widget.

Field widget form elements should be based on the passed-in $element, which contains the base form element properties derived from the field configuration.

The BaseWidget methods will set the weight, field name and delta values for each form element. If there are multiple values for this field, the formElement() method will be called as many times as needed.

Other modules may alter the form element provided by this function using hook_field_widget_single_element_form_alter() or hook_field_widget_single_element_WIDGET_TYPE_form_alter().

The FAPI element callbacks (such as #process, #element_validate,

value_callback, etc.) used by the widget do not have access to the

original $field_definition passed to the widget's constructor. Therefore, if any information is needed from that definition by those callbacks, the widget implementing this method, or a hook_field_widget[_WIDGET_TYPE]_form_alter() implementation, must extract the needed properties from the field definition and set them as ad-hoc $element['#custom'] properties, for later use by its element callbacks.

Parameters

FieldItemListInterface $items

Array of default values for this field.

int $delta

The order of this item in the array of sub-elements (0, 1, 2, etc.).

array $element

A form element array containing basic properties for the widget:

  • field_parents: The 'parents' space for the field in the form. Most

    widgets can simply overlook this property. This identifies the location where the field values are placed within $form_state->getValues(), and is used to access processing information for the field through the getWidgetState() and setWidgetState() methods.

  • title: The sanitized element label for the field, ready for output.

  • description: The sanitized element description for the field, ready

    for output.

  • required: A Boolean indicating whether the element value is required;

    for required multiple value fields, only the first widget's values are required.

  • delta: The order of this item in the array of sub-elements; see $delta

    above.

array $form

The form structure where widgets are being attached to. This might be a full form structure, or a sub-element of a larger form.

FormStateInterface $form_state

The current state of the form.

Return Value

array

The form elements for a single widget for this field.

protected DrupalDateTime createDefaultValue(DrupalDateTime $date, string $timezone)

Creates a date object for use as a default value.

This will take a default value, apply the proper timezone for display in a widget, and set the default time for date-only fields.

Parameters

DrupalDateTime $date

The UTC default date.

string $timezone

The timezone to apply.

Return Value

DrupalDateTime

A date object for use as a default value in a field widget.

validateStartEnd(array $element, FormStateInterface $form_state, array $complete_form)

element_validate callback to ensure that the start date <= the end date.

Parameters

array $element

An associative array containing the properties and children of the generic form element.

FormStateInterface $form_state

The current state of the form.

array $complete_form

The complete form structure.