You are here

class OfficeHoursDatelist in Office Hours 8

Provides a one-line text field form element.

Plugin annotation

@FormElement("office_hours_datelist");

Hierarchy

Expanded class hierarchy of OfficeHoursDatelist

File

src/Element/OfficeHoursDatelist.php, line 14

Namespace

Drupal\office_hours\Element
View source
class OfficeHoursDatelist extends Datelist {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $parent_info = parent::getInfo();
    $info = [
      '#input' => TRUE,
      '#tree' => TRUE,
      '#process' => [
        [
          static::class,
          'processOfficeHoursSlot',
        ],
      ],
      '#element_validate' => [
        [
          static::class,
          'validateOfficeHoursSlot',
        ],
      ],
      // @see Drupal\Core\Datetime\Element\Datelist.
      '#date_part_order' => [
        'year',
        'month',
        'day',
        'hour',
        'minute',
      ],
      // @see Drupal\Core\Datetime\Element\Datetime.
      '#date_date_element' => 'none',
      // {'none'|'date'}
      '#date_time_element' => 'time',
      // {'none'|'time'|'text'}
      '#date_date_format' => 'none',
      '#date_time_callbacks' => [],
      // Can be used to add a jQuery time picker or an 'All day' checkbox.
      '#date_year_range' => '1900:2050',
      // @see Drupal\Core\Datetime\Element\DateElementBase.
      '#date_timezone' => '+0000',
    ];

    // #process, #validate bottom-up.
    $info['#process'] = array_merge($parent_info['#process'], $info['#process']);
    $info['#element_validate'] = array_merge($parent_info['#element_validate'], $info['#element_validate']);
    return $info + $parent_info;
  }

  /**
   * Callback for office_hours_select element.
   *
   * Takes #default_value and dissects it in hours, minutes and ampm indicator.
   * Mimics the date_parse() function.
   * - g = 12-hour format of an hour without leading zeros 1 through 12
   * - G = 24-hour format of an hour without leading zeros 0 through 23
   * - h = 12-hour format of an hour with leading zeros   01 through 12
   * - H = 24-hour format of an hour with leading zeros   00 through 23
   *
   * @param array $element
   * @param mixed $input
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *
   * @return array|mixed|null
   */
  public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input == FALSE) {

      // Prepare the numeric value: use a DateTime value.
      $time = $element['#default_value'];
      $date = NULL;
      try {
        if (is_array($time)) {
          $date = OfficeHoursDateHelper::createFromArray($time);
        }
        elseif (is_numeric($time)) {
          $timezone = $element['#date_timezone'];

          // The Date function needs a fixed format, so format $time to '0030'.
          $time = OfficeHoursDatetime::get($time, 'Hi');
          $date = OfficeHoursDateHelper::createFromFormat('Gi', $time, $timezone);
        }
      } catch (\Exception $e) {
        $date = NULL;
      }
      $element['#default_value'] = $date;
    }

    // Set empty minutes field to '00' for better UX.
    if (!empty($input['hour']) && isset($input['minute']) && empty($input['minute'])) {
      $input['minute'] = '00';
    }
    $input = parent::valueCallback($element, $input, $form_state);
    return $input;
  }

  /**
   * Process the office_hours_select element before showing it.
   *
   * @param $element
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param $complete_form
   *
   * @return array
   *   The screen element.
   */
  public static function processOfficeHoursSlot(&$element, FormStateInterface $form_state, &$complete_form) {
    $element['hour']['#options'] = $element['#hour_options'];
    return $element;
  }

  /**
   * Validate the hours selector element.
   *
   * @param $element
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param $complete_form
   */
  public static function validateOfficeHoursSlot(&$element, FormStateInterface $form_state, &$complete_form) {
    $input = $element['#value'];
    $value = '';
    if (isset($input['object']) && $input['object']) {
      $value = (string) $input['object']
        ->format('Gi');

      // Set the value for usage in OfficeHoursList::validateOfficeHoursSlot().
      $element['#value'] = $value;
    }
    $form_state
      ->setValueForElement($element, $value);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateElementBase::datetimeRangeYears protected static function Specifies the start and end year to use as a date range.
DateElementBase::getElementTitle protected static function Returns the most relevant title of a datetime element.
Datelist::checkEmptyInputs protected static function Checks the input array for empty values.
Datelist::incrementRound protected static function Rounds minutes and seconds to nearest requested value.
Datelist::processDatelist public static function Expands a date element into an array of individual elements.
Datelist::validateDatelist public static function Validation callback for a datelist element.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OfficeHoursDatelist::getInfo public function Returns the element properties for this element. Overrides Datelist::getInfo
OfficeHoursDatelist::processOfficeHoursSlot public static function Process the office_hours_select element before showing it.
OfficeHoursDatelist::validateOfficeHoursSlot public static function Validate the hours selector element.
OfficeHoursDatelist::valueCallback public static function Callback for office_hours_select element. Overrides Datelist::valueCallback
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.