class DateRecurBasicWidget in Recurring Dates Field 3.0.x
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldWidget/DateRecurBasicWidget.php \Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget
- 3.x src/Plugin/Field/FieldWidget/DateRecurBasicWidget.php \Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget
- 3.1.x src/Plugin/Field/FieldWidget/DateRecurBasicWidget.php \Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget
Basic RRULE widget.
Displays an input textarea accepting RRULE strings.
Plugin annotation
@FieldWidget(
id = "date_recur_basic_widget",
label = @Translation("Simple Recurring Date Widget"),
field_types = {
"date_recur"
}
)
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\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface
- class \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase
- class \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase
- class \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeDefaultWidget
- class \Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget
- class \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeDefaultWidget
- class \Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase
- class \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase
- class \Drupal\Core\Field\WidgetBase implements WidgetInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DateRecurBasicWidget
1 file declares its use of DateRecurBasicWidget
File
- src/
Plugin/ Field/ FieldWidget/ DateRecurBasicWidget.php, line 30
Namespace
Drupal\date_recur\Plugin\Field\FieldWidgetView source
class DateRecurBasicWidget extends DateRangeDefaultWidget {
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) : array {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['#theme'] = 'date_recur_basic_widget';
$element['#element_validate'][] = [
$this,
'validateRrule',
];
// ::createDefaultValue isnt given enough context about the field item, so
// override its functions here.
$element['value']['#default_value'] = $element['end_value']['#default_value'] = NULL;
$element['value']['#date_timezone'] = $element['end_value']['#date_timezone'] = NULL;
$this
->createDateRecurDefaultValue($element, $items[$delta]);
// Move fields into a first occurrence container as 'End date' can be
// confused with 'End date' RRULE concept.
$element['first_occurrence'] = [
'#type' => 'fieldset',
'#title' => $this
->t('First occurrence'),
// Needs a weight otherwise children do not show up within single
// cardinality widgets.
'#weight' => 0,
];
$firstOccurrenceParents = array_merge($element['#field_parents'], [
$this->fieldDefinition
->getName(),
$delta,
'first_occurrence',
]);
$element['value']['#title'] = $this
->t('Start');
$element['end_value']['#title'] = $this
->t('End');
$element['end_value']['#description'] = $this
->t('Leave end empty to copy start date; the occurrence will therefore not have any duration.');
// The end date is never required. Start date is copied over if end date is
// empty.
$element['end_value']['#required'] = FALSE;
$element['value']['#group'] = $element['end_value']['#group'] = implode('][', $firstOccurrenceParents);
// Add custom value callbacks to correctly form a date from time zone field.
$element['value']['#value_callback'] = $element['end_value']['#value_callback'] = [
$this,
'dateValueCallback',
];
// Saved values (should) always have a time zone.
$timeZone = $items[$delta]->timezone ?? NULL;
$zones = $this
->getTimeZoneOptions();
$element['timezone'] = [
'#type' => 'select',
'#title' => t('Time zone'),
'#default_value' => $timeZone,
'#options' => $zones,
];
$element['rrule'] = [
'#type' => 'textarea',
'#default_value' => $items[$delta]->rrule ?? NULL,
'#title' => $this
->t('Repeat rule'),
'#description' => $this
->t('Repeat rule in <a href=":link">iCalendar Recurrence Rule</a> (RRULE) format.', [
':link' => 'https://icalendar.org/iCalendar-RFC-5545/3-8-5-3-recurrence-rule.html',
]),
];
return $element;
}
/**
* Validator for start and end elements.
*
* Sets the time zone before datetime element processes values.
*
* @param array $element
* An associative array containing the properties and children of the
* generic form element.
* @param array|false $input
* Input, if any.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The value to assign to the element.
*/
public function dateValueCallback(array $element, $input, FormStateInterface $form_state) : array {
if ($input !== FALSE) {
$timeZonePath = array_slice($element['#parents'], 0, -1);
$timeZonePath[] = 'timezone';
// Warning: The time zone is not yet validated, make sure it is valid
// before using.
$submittedTimeZone = NestedArray::getValue($form_state
->getUserInput(), $timeZonePath);
if (!isset($submittedTimeZone)) {
// If no time zone was submitted, such as when the 'timezone' field is
// set to #access => FALSE, its necessary to fall back to the fields
// default value.
$timeZoneFieldPath = array_slice($element['#array_parents'], 0, -1);
$timeZoneFieldPath[] = 'timezone';
$timeZoneField = NestedArray::getValue($form_state
->getCompleteForm(), $timeZoneFieldPath);
$submittedTimeZone = isset($timeZoneField['#value']) ? $timeZoneField['#value'] : $timeZoneField['#default_value'] ?? NULL;
}
$allTimeZones = \DateTimeZone::listIdentifiers();
// @todo Add test for invalid submitted time zone.
if (!in_array($submittedTimeZone, $allTimeZones)) {
// A date is invalid if the time zone is invalid.
// Need to kill inputs otherwise
// \Drupal\Core\Datetime\Element\Datetime::validateDatetime thinks there
// is valid input.
return [
'date' => '',
'time' => '',
'object' => NULL,
];
}
$element['#date_timezone'] = $submittedTimeZone;
}
// Setting a callback overrides default value callback in the element,
// call original now.
return Datetime::valueCallback($element, $input, $form_state);
}
/**
* Validates RRULE and first occurrence dates.
*
* @param array $element
* An associative array containing the properties and children of the
* generic form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*/
public function validateRrule(array &$element, FormStateInterface $form_state, array &$complete_form) : void {
$input = NestedArray::getValue($form_state
->getValues(), $element['#parents']);
/** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $startDate */
$startDate = $input['value'];
/** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $startDateEnd */
$startDateEnd = $input['end_value'];
if (is_array($startDate) || is_array($startDateEnd)) {
// Dates are an array if invalid input was submitted (e.g date:
// 80616-02-01).
return;
}
/** @var string $rrule */
$rrule = $input['rrule'];
if ($startDateEnd && !isset($startDate)) {
$form_state
->setError($element['value'], (string) $this
->t('Start date must be set if end date is set.'));
}
// If end was empty, copy start date over.
if (!isset($startDateEnd) && $startDate) {
$form_state
->setValueForElement($element['end_value'], $startDate);
$startDateEnd = $startDate;
}
// Validate RRULE.
// Only ensure start date is set, as end date is optional.
if (strlen($rrule) > 0 && $startDate) {
try {
DateRecurHelper::create($rrule, $startDate
->getPhpDateTime(), $startDateEnd ? $startDateEnd
->getPhpDateTime() : NULL);
} catch (\Exception $e) {
$form_state
->setError($element['rrule'], (string) $this
->t('Repeat rule is formatted incorrectly.'));
}
}
}
/**
* Get a list of time zones suitable for a select field.
*
* @return array
* A list of time zones where keys are PHP time zone codes, and values are
* human readable and translatable labels.
*/
protected function getTimeZoneOptions() : array {
return \system_time_zones(TRUE, TRUE);
}
/**
* Get the current users time zone.
*
* @return string
* A PHP time zone string.
*/
protected function getCurrentUserTimeZone() : string {
return \date_default_timezone_get();
}
/**
* {@inheritdoc}
*/
protected function createDefaultValue($date, $timezone) : DrupalDateTime {
assert($date instanceof DrupalDateTime);
assert(is_string($timezone));
// Cannot set time zone here as field item contains time zone.
if ($this
->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
$date
->setDefaultDateTime();
}
return $date;
}
/**
* Set element default value and time zone.
*
* @param array $element
* The element.
* @param \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem $item
* The date recur field item.
*/
protected function createDateRecurDefaultValue(array &$element, DateRecurItem $item) : void {
$startDate = $item->start_date;
$startDateEnd = $item->end_date;
$timeZone = isset($item->timezone) ? new \DateTimeZone($item->timezone) : NULL;
if ($timeZone) {
$element['value']['#date_timezone'] = $element['end_value']['#date_timezone'] = $timeZone
->getName();
if ($startDate) {
$startDate
->setTimezone($timeZone);
$element['value']['#default_value'] = $this
->createDefaultValue($startDate, $timeZone
->getName());
}
if ($startDateEnd) {
$startDateEnd
->setTimezone($timeZone);
$element['end_value']['#default_value'] = $this
->createDefaultValue($startDateEnd, $timeZone
->getName());
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateRangeDefaultWidget:: |
protected | property | The date format storage. | |
DateRangeDefaultWidget:: |
public static | function |
Creates an instance of the plugin. Overrides WidgetBase:: |
|
DateRangeDefaultWidget:: |
public | function |
Constructs a WidgetBase object. Overrides WidgetBase:: |
|
DateRangeWidgetBase:: |
public | function |
Massages the form values into the format expected for field values. Overrides DateTimeWidgetBase:: |
|
DateRangeWidgetBase:: |
public | function | #element_validate callback to ensure that the start date <= the end date. | |
DateRecurBasicWidget:: |
protected | function | Set element default value and time zone. | |
DateRecurBasicWidget:: |
protected | function |
Creates a date object for use as a default value. Overrides DateTimeWidgetBase:: |
|
DateRecurBasicWidget:: |
public | function | Validator for start and end elements. | |
DateRecurBasicWidget:: |
public | function |
Returns the form for a single field widget. Overrides DateRangeDefaultWidget:: |
|
DateRecurBasicWidget:: |
protected | function | Get the current users time zone. | |
DateRecurBasicWidget:: |
protected | function | Get a list of time zones suitable for a select field. | |
DateRecurBasicWidget:: |
public | function | Validates RRULE and first occurrence dates. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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:: |
2 |
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 static | function |
Defines the default settings for this plugin. Overrides PluginSettingsInterface:: |
42 |
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. | 4 |
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. | |
WidgetBase:: |
protected | property | The field definition. | |
WidgetBase:: |
protected | property |
The widget settings. Overrides PluginSettingsBase:: |
|
WidgetBase:: |
public static | function | Ajax callback for the "Add another item" button. | |
WidgetBase:: |
public static | function | Submission handler for the "Add another item" button. | |
WidgetBase:: |
public static | function | After-build handler for field elements in a form. | |
WidgetBase:: |
public | function |
Assigns a field-level validation error to the right widget sub-element. Overrides WidgetInterface:: |
8 |
WidgetBase:: |
public | function |
Extracts field values from submitted form values. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
public | function |
Reports field-level validation errors against actual form elements. Overrides WidgetBaseInterface:: |
2 |
WidgetBase:: |
public | function |
Creates a form element for a field. Overrides WidgetBaseInterface:: |
3 |
WidgetBase:: |
protected | function | Special handling to create form elements for multiple values. | 1 |
WidgetBase:: |
protected | function | Generates the form element for a single copy of the widget. | |
WidgetBase:: |
protected | function | Returns the value of a field setting. | |
WidgetBase:: |
protected | function | Returns the array of field settings. | |
WidgetBase:: |
protected | function | Returns the filtered field description. | |
WidgetBase:: |
public static | function |
Retrieves processing information about the widget from $form_state. Overrides WidgetBaseInterface:: |
|
WidgetBase:: |
protected static | function | Returns the location of processing information within $form_state. | |
WidgetBase:: |
protected | function | Returns whether the widget handles multiple values. | |
WidgetBase:: |
public static | function |
Returns if the widget can be used for the provided field. Overrides WidgetInterface:: |
4 |
WidgetBase:: |
protected | function | Returns whether the widget used for default value form. | |
WidgetBase:: |
public | function |
Returns a form to configure settings for the widget. Overrides WidgetInterface:: |
16 |
WidgetBase:: |
public | function |
Returns a short summary for the current widget settings. Overrides WidgetInterface:: |
15 |
WidgetBase:: |
public static | function |
Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface:: |