You are here

public static function SmartDateWidgetBase::createWidget in Smart Date 3.1.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
  2. 3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
  3. 3.0.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
  4. 3.2.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
  5. 3.3.x src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::createWidget()
  6. 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 155

Class

SmartDateWidgetBase
Base class for the 'smartdate_*' widgets.

Namespace

Drupal\smart_date\Plugin\Field\FieldWidget

Code

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';
  if (isset($values['start'])) {

    // Ensure values always display relative to the site.
    $element['value']['#default_value'] = self::remapDatetime($values['start']);
  }
  $element['end_value'] = [
    '#title' => t('End'),
  ] + $element['value'];
  if (isset($values['end'])) {

    // Ensure values always display relative to the site.
    $element['end_value']['#default_value'] = self::remapDatetime($values['end']);
  }
  $element['value']['#attributes']['class'] = [
    'time-start',
  ];
  $element['end_value']['#attributes']['class'] = [
    'time-end',
  ];
  if (isset($values['storage'])) {
    $element['storage'] = [
      '#type' => 'value',
      '#value' => $values['storage'],
    ];
  }

  // 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'] ?? $defaults['default_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',
      ],
    ],
    '#wrapper_attributes' => [
      'class' => [
        'duration-wrapper',
      ],
    ],
  ];

  // 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,
  ];
}