class TimeFieldStandardWidget in Timefield 1.0.x
Plugin implementation of the 'timefield_standard_widget' widget.
Plugin annotation
@FieldWidget(
id = "timefield_standard_widget",
label = @Translation("Timefield"),
field_types = {"timefield"}
)
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\timefield\Plugin\Field\FieldWidget\TimeFieldStandardWidget implements ContainerFactoryPluginInterface
- 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 TimeFieldStandardWidget
File
- src/
Plugin/ Field/ FieldWidget/ TimeFieldStandardWidget.php, line 30
Namespace
Drupal\timefield\Plugin\Field\FieldWidgetView source
class TimeFieldStandardWidget extends WidgetBase implements ContainerFactoryPluginInterface {
/**
* Entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Field widget display plugin manager.
*
* @var \Drupal\entity_browser\FieldWidgetDisplayManager
*/
protected $fieldDisplayManager;
/**
* The depth of the delete button.
*
* This property exists so it can be changed if subclasses.
*
* @var int
*/
protected static $deleteDepth = 4;
/**
* The module handler interface.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* If triggering element was hidden target_id element.
*
* @var bool
*/
protected $entityBrowserValueUpdated;
/**
* If triggering element was hidden target_id element.
*
* @var \Drupal\Core\Asset\LibraryDiscovery
*/
protected $libraryDiscovery;
/**
* Constructs widget plugin.
*
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the widget is associated.
* @param array $settings
* The widget settings.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Asset\LibraryDiscovery $libraryDiscovery
* The library discovery.
* @param \Drupal\Core\Extension\ModuleHandlerInterface|null $module_handler
* The module handler.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, LibraryDiscovery $libraryDiscovery, ModuleHandlerInterface $module_handler) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->libraryDiscovery = $libraryDiscovery;
$this->moduleHandler = $module_handler;
}
/**
* {@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['third_party_settings'], $container
->get('library.discovery'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$library = \Drupal::service('library.discovery')
->getLibraryByName('timefield', 'timepicker');
return [
'disable_plugin' => empty($library) ? TRUE : FALSE,
'input_format' => [
'separator' => ':',
'showLeadingZero' => FALSE,
'showMinutesLeadingZero' => TRUE,
'showPeriod' => TRUE,
'periodSeparator' => '',
'showHours' => TRUE,
'showMinutes' => TRUE,
'am_text' => 'AM',
'pm_text' => 'PM',
'minute_interval' => 5,
'showCloseButton' => FALSE,
'closeButtonText' => 'Done',
'showNowButton' => FALSE,
'nowButtonText' => 'Now',
'showDeselectButton' => FALSE,
'deselectButtonText' => 'Deselect',
'myPosition' => 'left top',
'atPosition' => 'left bottom',
],
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$library = $this->libraryDiscovery
->getLibraryByName('timefield', 'timepicker');
if (empty($library)) {
$this
->messenger()
->addWarning($this
->t("You will not have enhanced time input widget without downloading the plugin. %link", [
'%link' => Link::fromTextAndUrl("Read installation instructions here.", Url::fromUserInput('http://drupalcode.org/project/timefield.git/blob_plain/HEAD:/README.txt')),
]));
}
$elements['disable_plugin'] = [
'#title' => $this
->t('Disable jQuery Timepicker plugin.'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('disable_plugin'),
'#description' => $this
->t('Do not use jQuery Timepicker plugin for input.'),
'#disabled' => empty($library),
'#attributes' => [
'name' => 'disable_plugin',
],
];
$elements['separator'] = [
'#title' => $this
->t('Hour and Minute Separator'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('separator'),
'#size' => 10,
'#description' => $this
->t('The character to use to separate hours and minutes.'),
];
$elements['showLeadingZero'] = [
'#title' => $this
->t('Show Leading Zero for Hour'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('showLeadingZero'),
'#description' => $this
->t('Whether or not to show a leading zero for hours < 10.'),
];
$elements['showPeriod'] = [
'#title' => $this
->t('Show AM/PM Label'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('showPeriod'),
'#description' => $this
->t('Whether or not to show AM/PM on the input textfield both on the widget and in the text field after selecting the time with the widget.'),
];
$elements['periodSeparator'] = [
'#title' => $this
->t('What character should appear between the time and the Period (AM/PM)'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('periodSeparator'),
'#size' => 10,
'#description' => $this
->t('The character to use to separate the time from the time period (AM/PM).'),
];
$elements['am_text'] = [
'#title' => $this
->t('AM text'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('am_text'),
'#size' => 10,
];
$elements['pm_text'] = [
'#title' => $this
->t('PM text'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('pm_text'),
'#size' => 10,
];
$elements['showCloseButton'] = [
'#title' => $this
->t('Show a Button to Close the Picker Widget'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('showCloseButton'),
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
$elements['closeButtonText'] = [
'#title' => $this
->t('Close Button text'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('closeButtonText'),
'#size' => 10,
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
$elements['showNowButton'] = [
'#title' => $this
->t('Show a Button to Select the Current Time'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('showNowButton'),
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
$elements['nowButtonText'] = [
'#title' => $this
->t('Now Button text'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('nowButtonText'),
'#size' => 10,
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
$elements['showDeselectButton'] = [
'#title' => $this
->t('Show a Button to Deselect the time in the Picker Widget'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('showDeselectButton'),
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
$elements['deselectButtonText'] = [
'#title' => $this
->t('Deselect Button text'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('deselectButtonText'),
'#size' => 10,
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
$elements['myPosition'] = [
'#title' => $this
->t('my Position'),
'#type' => 'select',
'#default_value' => $this
->getSetting('myPosition'),
'#options' => array_combine([
'left top',
'left center',
'left bottom',
'center top',
'center center',
'center bottom',
'right top',
'right center',
'right bottom',
], [
'left top',
'left center',
'left bottom',
'center top',
'center center',
'center bottom',
'right top',
'right center',
'right bottom',
]),
'#description' => $this
->t('Corner of the timpicker widget dialog to position. See !jquery_info for more info.', [
'!jquery_info' => Link::fromTextAndUrl($this
->t("jQuery UI Position documentation"), Url::fromUri('http://jqueryui.com/demos/position')),
]),
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
$elements['atPosition'] = [
'#title' => $this
->t('at Position'),
'#type' => 'select',
'#options' => array_combine([
'left top',
'left center',
'left bottom',
'center top',
'center center',
'center bottom',
'right top',
'right center',
'right bottom',
], [
'left top',
'left center',
'left bottom',
'center top',
'center center',
'center bottom',
'right top',
'right center',
'right bottom',
]),
'#default_value' => $this
->getSetting('atPosition'),
'#description' => $this
->t('Where to position "my Position" relative to input widget textfield See !jquery_info for more info.', [
'!jquery_info' => Link::fromTextAndUrl($this
->t("jQuery UI Position documentation"), Url::fromUri('http://jqueryui.com/demos/position')),
]),
'#states' => [
'invisible' => [
':input[name=disable_plugin]' => [
'checked' => TRUE,
],
],
],
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this
->t('Number of summary rows: @rows', [
'@rows' => $this
->getSetting('summary_rows'),
]);
if ($this
->getSetting('show_summary')) {
$summary[] = $this
->t('Summary field will always be visible');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$wrapper = FALSE;
$instance_class = str_replace('_', '-', $items
->getName()) . "-" . $delta;
$instance_settings = $this
->getSettings();
$field_settings = $this
->getFieldSettings();
if (!$instance_settings['disable_plugin']) {
$js_settings = _timefield_js_settings($instance_class, $instance_settings);
$context = [
'type' => 'field',
'items' => $items,
];
$this->moduleHandler
->alter('timefield_js_settings', $js_settings, $context);
$element['#attached']['library'][] = 'timefield/timepicker';
$element['#attached']['library'][] = 'timefield/timefield';
$element['#attached']['drupalSettings']['timefield'][$instance_class] = $js_settings;
}
$element += [
'#delta' => $delta,
];
if ($field_settings['weekly_summary_with_label']) {
$wrapper = TRUE;
$element['label'] = [
'#title' => $this
->t('Label'),
'#description' => $this
->t('Enter a label for the summary'),
'#type' => 'textfield',
'#default_value' => isset($items[$delta]->label) ? $items[$delta]->label : '',
'#size' => 40,
'#maxlength' => 60,
];
}
$value = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value) : '';
$element['value'] = [
'#type' => 'textfield',
'#title' => Xss::filter($element['#title']),
'#description' => Xss::filter($element['#description']),
'#default_value' => $value,
'#required' => $element['#required'],
'#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
'#delta' => $delta,
'#element_validate' => [
[
static::class,
'validateTimeField',
],
],
'#attributes' => [
'class' => [
'edit-timefield-timepicker',
$instance_class,
],
],
];
if ($field_settings['totime'] == 'required' || $field_settings['totime'] == 'optional') {
$wrapper = TRUE;
$value2 = isset($items[$delta]) ? timefield_integer_to_time($instance_settings, $items[$delta]->value2) : '';
$element['value2'] = [
'#type' => 'textfield',
'#title' => $this
->t('End Time'),
'#description' => $this
->t('Enter a time value, in any format'),
'#default_value' => $value2,
'#required' => $element['#required'],
'#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
'#delta' => $delta,
'#element_validate' => [
[
static::class,
'validateTimeField',
],
],
'#attributes' => [
'class' => [
'edit-timefield-timepicker',
$instance_class,
],
],
];
}
if ($field_settings['weekly_summary'] || $field_settings['weekly_summary_with_label']) {
$wrapper = TRUE;
$days = isset($items[$delta]->mon) ? _timefield_weekly_summary_days_map($items[$delta]) : [];
$weekDays = _timefield_weekly_summary_days();
$element['days'] = [
'#type' => 'container',
'#description' => $this
->t('Select the days this schedule applies to'),
'#attributes' => [
'class' => [
'edit-field-timefield-days',
],
],
];
foreach ($weekDays as $key => $day) {
$element[$key] = [
'#title' => $day,
'#type' => 'checkbox',
'#default_value' => !empty($days) && $days[$key] != '0' ? 1 : 0,
'#attributes' => [
'class' => [
'edit-field-timefield-days',
],
],
];
if ($key == 'mon') {
$element[$key]['#prefix'] = new FormattableMarkup('<b>@label</b>', [
'@label' => 'Days',
]);
}
elseif ($key == 'sun') {
$element[$key]['#suffix'] = $this
->t('Select the days this schedule applies to');
}
}
}
if ($wrapper) {
$element['#theme_wrappers'][] = 'fieldset';
}
return $element;
}
/**
* {@inheritdoc}
*
* Validation Callback; Timefield process field.
*/
public static function validateTimeField($element, FormStateInterface $form_state) {
// If empty, set to null.
if (strlen($element['#value']) == 0) {
if (!empty($element['#required'])) {
$form_state
->setError($element, new TranslatableMarkup('@name field is required.', [
'@name' => Html::escape($element['#title']),
]));
}
$form_state
->setValueForElement($element, NULL);
return;
}
$date_value = date_parse($element['#value']);
if ($date_value['error_count']) {
$form_state
->setError($element, new TranslatableMarkup('Please enter the time in a valid format'));
}
else {
$parsed_value = timefield_time_to_integer($element['#value']);
$form_state
->setValueForElement($element, $parsed_value);
}
}
/**
* {@inheritdoc}
*/
public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
$element = parent::errorElement($element, $violation, $form, $form_state);
return $element === FALSE ? FALSE : $element[$violation->arrayPropertyPath[0]];
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | 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. | |
TimeFieldStandardWidget:: |
protected | property | The current user. | |
TimeFieldStandardWidget:: |
protected static | property | The depth of the delete button. | |
TimeFieldStandardWidget:: |
protected | property | If triggering element was hidden target_id element. | |
TimeFieldStandardWidget:: |
protected | property | Entity type manager service. | |
TimeFieldStandardWidget:: |
protected | property | Field widget display plugin manager. | |
TimeFieldStandardWidget:: |
protected | property | If triggering element was hidden target_id element. | |
TimeFieldStandardWidget:: |
protected | property | The module handler interface. | |
TimeFieldStandardWidget:: |
public static | function |
Creates an instance of the plugin. Overrides WidgetBase:: |
|
TimeFieldStandardWidget:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsBase:: |
|
TimeFieldStandardWidget:: |
public | function |
Assigns a field-level validation error to the right widget sub-element. Overrides WidgetBase:: |
|
TimeFieldStandardWidget:: |
public | function |
Returns the form for a single field widget. Overrides WidgetInterface:: |
|
TimeFieldStandardWidget:: |
public | function |
Returns a form to configure settings for the widget. Overrides WidgetBase:: |
|
TimeFieldStandardWidget:: |
public | function |
Returns a short summary for the current widget settings. Overrides WidgetBase:: |
|
TimeFieldStandardWidget:: |
public static | function | Validation Callback; Timefield process field. | |
TimeFieldStandardWidget:: |
public | function |
Constructs widget plugin. Overrides WidgetBase:: |
|
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 |
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 |
Massages the form values into the format expected for field values. Overrides WidgetInterface:: |
7 |
WidgetBase:: |
public static | function |
Stores processing information about the widget in $form_state. Overrides WidgetBaseInterface:: |