You are here

public static function OfficeHoursList::getDefaultOperations in Office Hours 8

Gets this list element's default operations.

Parameters

array $element: The entity the operations are for.

Return value

array The array structure is identical to the return value of self::getOperations().

1 call to OfficeHoursList::getDefaultOperations()
OfficeHoursList::processOfficeHoursSlot in src/Element/OfficeHoursList.php
Process an individual element.

File

src/Element/OfficeHoursList.php, line 41

Class

OfficeHoursList
Provides a one-line text field form element for the List Widget.

Namespace

Drupal\office_hours\Element

Code

public static function getDefaultOperations(array $element) {
  $operations = [];
  $max_delta = $element['#field_settings']['cardinality_per_day'] - 1;
  $day_delta = $element['#daydelta'];
  $day = isset($element['#value']['day']) ? (int) $element['#value']['day'] : 0;
  $suffix = ' ';

  // Show a 'Clear this line' js-link to each element.
  // Use text 'Remove', which has lots of translations.
  $operations['delete'] = [];
  if (isset($element['#value']['starthours']) || isset($element['#value']['endhours'])) {
    $operations['delete'] = [
      '#type' => 'link',
      '#title' => t('Remove'),
      '#weight' => 12,
      '#url' => Url::fromRoute('<front>'),
      // Dummy, will be catch-ed by js.
      '#suffix' => $suffix,
      '#attributes' => [
        'class' => [
          'office-hours-delete-link',
          'office-hours-link',
        ],
      ],
    ];
  }

  // Add 'Copy' link to first slot of each day.
  // First day copies from last day.
  $operations['copy'] = [];
  if ($day_delta == 0) {
    $operations['copy'] = [
      '#type' => 'link',
      '#title' => $day !== OfficeHoursDateHelper::getFirstDay() ? t('Copy previous day') : t('Copy last day'),
      '#weight' => 16,
      '#url' => Url::fromRoute('<front>'),
      // Dummy, will be catch-ed by js.
      '#suffix' => $suffix,
      '#attributes' => [
        'class' => [
          'office-hours-copy-link',
          'office-hours-link',
        ],
      ],
    ];
  }

  // Add 'Add time slot' link to all-but-last slots of each day.
  $operations['add'] = [];
  if ($day_delta < $max_delta) {
    $operations['add'] = [
      '#type' => 'link',
      '#title' => t('Add @node_type', [
        '@node_type' => t('time slot'),
      ]),
      '#weight' => 11,
      '#url' => Url::fromRoute('<front>'),
      // Dummy, will be catch-ed by js.
      '#suffix' => $suffix,
      '#attributes' => [
        'class' => [
          'office-hours-add-link',
          'office-hours-link',
        ],
      ],
    ];
  }
  return $operations;
}