Promotion: End Date Field (StoreDateTimeWidget) not visible if once set

Created on 20 June 2022, over 2 years ago
Updated 21 June 2023, over 1 year ago

Hi together

I am experiencing problems with the promotion end date field.
If creating a promotion i can check "Provide an end date" and then the end date field gets shown correctly.
But once i set and save it, the field is not visible anymore during editing the promotion.

I checked the code of \Drupal\commerce_store\Plugin\Field\FieldWidget\StoreDateTimeWidget::formElement and in my oppinion the state is not implemented the right way:

if ($items[$delta]->date) {
      $date = new DrupalDateTime($items[$delta]->value, DateTimeItemInterface::STORAGE_TIMEZONE);
      if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
        $date->setDefaultDateTime();
      }
      $element['value']['#default_value'] = $date;
    }

    // When the field is optional, it is considered more user friendly
    // to hide the date/time widget behind a checkbox.
    $field = $this->fieldDefinition;
    if (!$field->isRequired() && $field->getSetting('datetime_optional_label')) {
      $checkbox_parents = array_merge($form['#parents'], [$field->getName(), $delta, 'has_value']);
      $checkbox_path = array_shift($checkbox_parents);
      $checkbox_path .= '[' . implode('][', $checkbox_parents) . ']';

      $element['has_value'] = [
        '#type' => 'checkbox',
        '#title' => $field->getSetting('datetime_optional_label'),
        '#default_value' => !empty($element['value']['#default_value']),
        '#access' => empty($element['value']['#default_value']),
      ];
      $element['value']['#weight'] = 10;
      $element['value']['#description'] = '';
      // Workaround for #2419131.
      $element['container']['#type'] = 'container';
      $element['container'] = [
        '#type' => 'container',
        '#states' => [
          'visible' => [
            ':input[name="' . $checkbox_path . '"]' => ['checked' => TRUE],
          ],
        ],
      ];

So on edit the field has a default value set: $element['value']['#default_value']. That leads to $element['has_value'] having #access = FALSE.

And that leads to $element['container'] beeing not visible cause the condition for the visible state is not matched, cause the has_value element is not beeing rendered cause of its #access value.

Am i doing something wrong here, or do others also have this issue?

🐛 Bug report
Status

Needs review

Version

2.0

Component

Store

Created by

🇨🇭Switzerland megadesk3000

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024