You are here

public static function OfficeHoursSlot::processOfficeHoursSlot in Office Hours 8

Process an individual element.

Build the form element. When creating a form using Form API #process, note that $element['#value'] is already set.

Parameters

$element:

\Drupal\Core\Form\FormStateInterface $form_state:

$complete_form:

Return value

array The enriched element, identical to first parameter.

Overrides OfficeHoursList::processOfficeHoursSlot

File

src/Element/OfficeHoursSlot.php, line 17

Class

OfficeHoursSlot
Provides a one-line text field form element for the Week Widget.

Namespace

Drupal\office_hours\Element

Code

public static function processOfficeHoursSlot(&$element, FormStateInterface $form_state, &$complete_form) {

  // Fill with default data from a List element.
  $element = parent::processOfficeHoursSlot($element, $form_state, $complete_form);
  $max_delta = $element['#field_settings']['cardinality_per_day'] - 1;
  $day_delta = $element['#daydelta'];
  $day = isset($element['#value']['day']) ? (int) $element['#value']['day'] : '';
  if ($day_delta == 0) {

    // This is the first slot of the day.
    // Display the slot with Day name (already translated) as label.
    $label = $element['#dayname'];
  }
  elseif ($day_delta > $max_delta) {

    // Never show this illegal slot.
    // In case the number of slots per day was lowered by admin, this element
    // may have a value. Better clear it (in case a value was entered before).
    // The value will be removed upon the next 'Save' action.
    $label = '';

    // The following style is only needed if js isn't working.
    // The following class is the trigger for js to hide the row.
    $element['#attributes']['class'][] = 'office-hours-hide';
    $element['#value'] = empty($element['#value'] ? [] : $element['#value']);
    $element['#value']['starthours'] = '';
    $element['#value']['endhours'] = '';
    $element['#value']['comment'] = '';
  }
  elseif (isset($element['#value']['starthours']) && (!empty($element['#value']['starthours']) || $element['#value']['starthours'] === '0')) {

    // @todo Use OfficeHoursDateTime::isEmpty()?
    // This is a following slot with contents.
    $label = t('and');

    // Display the slot and display Add-link.
    $element['#attributes']['class'][] = 'office-hours-more';
  }
  else {

    // This is an empty following slot.
    $label = t('and');

    // Hide the slot and add Add-link, in case shown by js.
    $element['#attributes']['class'][] = 'office-hours-hide';
    $element['#attributes']['class'][] = 'office-hours-more';
  }

  // Override (hide) the 'day' select-field.
  $element['day'] = [
    '#type' => 'hidden',
    '#prefix' => $day_delta ? "<div class='office-hours-more-label'>{$label}</div>" : "<div class='office-hours-label'>{$label}</div>",
    '#default_value' => $day,
    '#value' => $day,
  ];
  $element['#attributes']['class'][] = "office-hours-day-{$day}";
  return $element;
}