Commit af573443 authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #2627934: Improve coding of ListWidget UX.

parent 265372ab
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -27,11 +27,6 @@ class OfficeHoursList extends FormElement {
      '#element_validate' => [
        [$class, 'validateOfficeHoursSlot'],
      ],
      '#attached' => [
        'library' => [
          'office_hours/office_hours',
        ],
      ],
    ];
    return $info;
  }
+38 −44
Original line number Diff line number Diff line
@@ -15,6 +15,44 @@ use Drupal\office_hours\OfficeHoursDateHelper;
 */
class OfficeHoursSelect extends Datelist {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    $parent_info = parent::getInfo();

    $info = [
      '#input' => TRUE,
      '#tree' => TRUE,
      '#process' => [
        [$class, 'processOfficeHours'],
      ],
      '#element_validate' => [
        [$class, 'validateOfficeHours'],
      ],

      // @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_date_callbacks' => [],
      //'#date_time_format' => 'time', // see format_date()
      '#date_time_callbacks' => [], // Can be used to add a jQuery timepicker or an 'All day' checkbox.
      '#date_year_range' => '1900:2050',
      // @see Drupal\Core\Datetime\Element\DateElementBase.
      '#date_timezone' => NULL, // new \DateTimezone(DATETIME_STORAGE_TIMEZONE),
    ];

    // #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.
   *
@@ -122,48 +160,4 @@ class OfficeHoursSelect extends Datelist {
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);

    $info = [
      '#input' => TRUE,
      '#tree' => TRUE,
      '#process' => [
        [$class, 'processOfficeHours'],
      ],
      '#element_validate' => [
        [$class, 'validateOfficeHours'],
      ],
      //'#attached' => [
      //  'library' => [
      //    'office_hours/office_hours',
      //  ],
      //],

      // @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_date_callbacks' => [],
      //'#date_time_format' => 'time', // see format_date()
      '#date_time_callbacks' => [], // Can be used to add a jQuery timepicker or an 'All day' checkbox.
      '#date_year_range' => '1900:2050',
      // @see Drupal\Core\Datetime\Element\DateElementBase.
      '#date_timezone' => NULL, // new \DateTimezone(DATETIME_STORAGE_TIMEZONE),
    ];

    $parent_info = parent::getInfo();

    // #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;
  }

}
+3 −16
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ class OfficeHoursDefaultWidget extends OfficeHoursWidgetBase {
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);

    // In D8, we have a (deliberate) anomaly in the widget.
    // We prepare 1 widget for the whole week, but the field has unlimited cardinality.
@@ -98,16 +99,7 @@ class OfficeHoursDefaultWidget extends OfficeHoursWidgetBase {
    }

    // Build multi element widget. Copy the description, etc. into the table.
    $element['value'] = $element + [
        /*
        // Begin D7-code.
        // '#theme' => 'office_hours_week',
        '#field_name' => $items->getName(),
        '#title' => $this->fieldDefinition->getLabel(),
        // '#required' => $this->fieldDefinition->isRequired(),
        '#description' => $this->fieldDefinition->getDescription(),
        // End   D7-code.
        */
    $element['value'] = [
        '#type' => 'office_hours_table',
        '#header' => [
          'title' => $this->t($element['#title']),
@@ -118,12 +110,7 @@ class OfficeHoursDefaultWidget extends OfficeHoursWidgetBase {
        ],
        '#tableselect' => FALSE,
        '#tabledrag' => FALSE,
        '#attached' => [
          'library' => [
            'office_hours/office_hours_widget',
          ],
        ],
      ] + $elements;
      ] + $element['value'] + $elements;

    return $element;
  }
+16 −21
Original line number Diff line number Diff line
@@ -23,8 +23,7 @@ class OfficeHoursListWidget extends OfficeHoursWidgetBase {
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    // Get field settings, to make it accessible for each element in other functions.
    $settings = $this->getFieldSettings();
    $element = parent::formElement($items, $delta, $element, $form, $form_state);

    $default_value = isset($items[$delta]) ? $items[$delta]->getValue() : NULL;
    $day = isset($default_value['day']) ? $default_value['day'] : '';
@@ -35,23 +34,19 @@ class OfficeHoursListWidget extends OfficeHoursWidgetBase {
        '#default_value' => $default_value,
        '#day' => $day,
        // Make sure the value is shown in OfficeHoursSlot.
      '#daydelta' => 0, // $daydelta;
        '#daydelta' => 0,
        '#dayname' => $daynames[$day],
      '#field_settings' => $settings,
      '#date_element_type' => $this->getSetting('date_element_type'),
        // Wrap all of the select elements with a fieldset.
      '#theme_wrappers' => ['fieldset', ],
        '#theme_wrappers' => [
          'fieldset',
        ],
        '#attributes' => [
          'class' => [
            'container-inline',
          ],
        ],
      '#attached' => [
        'library' => [
          'office_hours/office_hours_widget',
        ],
      ],
    ];

      ] + $element['value'];

    return $element;
  }
+18 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\office_hours\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;

@@ -77,6 +78,23 @@ abstract class OfficeHoursWidgetBase extends WidgetBase {
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

    $element['value'] = [
      '#date_element_type' => $this->getSetting('date_element_type'),
      '#attached' => [
        'library' => [
          'office_hours/office_hours_widget',
        ],
      ],
    ];

    return $element;
  }

  /**
   * {@inheritdoc}
   */