Score:0

How to use datelist in a custom field widget?

um flag

Since an administrator of the website I am building is visually impaired, I need to create a custom date field widget rather than use the one provided by Drupal.

I am trying to implement a datelist element.

<?php

namespace Drupal\my_module\Plugin\Field\FieldWidget;

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

/**
 * Accessible date field widget.
 *
 * @FieldWidget(
 *   id = "accessible_date",
 *   label = @Translation("Accessible date widget"),
 *   field_types = {
 *     "datetime"
 *   }
 * )
 */
class AccessibleDate extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $max_year = date('Y') + 1;
    $default_date = new DrupalDateTime();
    if (isset($items[$delta]->value)) {
      $default_date = new DrupalDateTime($items[$delta]->value);
    }

    $element += [
      '#type' => 'datelist',
      '#default_value' => $default_date,
      '#date_part_order' => [
        'day',
        'month',
        'year',
        'hour',
        'minute',
      ],
      '#date_year_range' => '2000:' . $max_year,
    ];

    return ['value' => $element];
  }

}

The widget is displayed correctly but when I submit the form to add or edit a node, I have the following error on each of the datelist fields: "The datetime value must be a string."

enter image description here

Do you know how to fix this?

Score:0
um flag

Ok I figured it out, the datetime format needs a Y-m-dTH:i:s format. I added a validate callback to my element to alter the data.

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $max_year = date('Y') + 1;
    dpm($items[$delta]->value);
    $default_date = new DrupalDateTime();
    if (isset($items[$delta]->value)) {
      $default_date = new DrupalDateTime($items[$delta]->value);
    }

    $element += [
      '#type' => 'datelist',
      '#default_value' => $default_date,
      '#date_part_order' => [
        'day',
        'month',
        'year',
        'hour',
        'minute',
      ],
      '#date_year_range' => '2000:' . $max_year,
      '#element_validate' => [
        [static::class, 'validate'],
      ],
    ];

    return ['value' => $element];
  }

  /**
   * Prepare datelist value.
   */
  public static function validate($element, FormStateInterface $form_state) {
    $value = $element['#value'];
    $date = $value['year'] . '-' . $value['month'] . '-' . $value['day'] . 'T' . $value['hour'] . ':' . $value['minute'] . ':00';
    $form_state->setValueForElement($element, $date);
  }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.