public static function SmartDateWidgetBase::createWidget in Smart Date 8.2
Same name and namespace in other branches
- 3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
- 3.0.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
- 3.1.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
- 3.2.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
- 3.3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
- 3.4.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
Helper method to create SmartDate element.
2 calls to SmartDateWidgetBase::createWidget()
- SmartDateOverrideForm::buildForm in modules/
smart_date_recur/ src/ Form/ SmartDateOverrideForm.php - Form constructor.
- SmartDateWidgetBase::formElement in src/
Plugin/ Field/ FieldWidget/ SmartDateWidgetBase.php - Returns the form for a single field widget.
File
- src/
Plugin/ Field/ FieldWidget/ SmartDateWidgetBase.php, line 87
Class
- SmartDateWidgetBase
- Base class for the 'smartdate_*' widgets.
Namespace
Drupal\smart_date\Plugin\Field\FieldWidgetCode
public static function createWidget(&$element, $values, ?array $defaults) {
// If an empty set of defaults provided, create our own.
if (empty($defaults)) {
$defaults = [
'default_duration_increments' => "30\n60|1 hour\n90\n120|2 hours\ncustom",
'default_duration' => '60',
];
}
// Wrap all of the select elements with a fieldset.
$element['#theme_wrappers'][] = 'fieldset';
$element['#element_validate'][] = [
static::class,
'validateStartEnd',
];
$element['value']['#title'] = t('Start');
$element['value']['#date_year_range'] = '1902:2037';
// Ensure values always display relative to the site.
$element['value']['#default_value'] = self::remapDatetime($values['start']);
$element['end_value'] = [
'#title' => t('End'),
// Ensure values always display relative to the site.
'#default_value' => self::remapDatetime($values['end']),
] + $element['value'];
$element['value']['#attributes']['class'] = [
'time-start',
];
$element['end_value']['#attributes']['class'] = [
'time-end',
];
// Parse the allowed duration increments and create labels if not provided.
$increments = SmartDateListItemBase::parseValues($defaults['default_duration_increments']);
foreach ($increments as $key => $label) {
if (strcmp($key, $label) !== 0) {
// Label provided, so no extra logic required.
continue;
}
if (is_numeric($key)) {
// Anything but whole minutes will create errors with the time field.
$num = (int) $key;
$increments[$key] = t('@count minutes', [
'@count' => $num,
]);
}
elseif ($key == 'custom') {
$increments[$key] = t('Custom');
}
else {
// Note sure what else we would encounter, so escape it.
$increments[$key] = t('@key (unrecognized format)', [
'@key' => $key,
]);
}
}
$default_duration = $values['duration'];
if (!array_key_exists($default_duration, $increments)) {
if (array_key_exists('custom', $increments)) {
$default_duration = 'custom';
}
else {
// TODO: throw some kind of error/warning if invalid duration?
$default_duration = '';
}
}
$element['duration'] = [
'#title' => t('Duration'),
'#type' => 'select',
'#options' => $increments,
'#default_value' => $default_duration,
'#attributes' => [
'data-default' => $defaults['default_duration'],
'class' => [
'field-duration',
],
],
];
// No true input, so preserve an existing value otherwise use site default.
$default_tz = isset($values['timezone']) ? $values['timezone'] : NULL;
$element['timezone'] = [
'#type' => 'hidden',
'#title' => t('Time zone'),
'#default_value' => $default_tz,
];
}