Webform options limit - Entity Reference Option/Select not empty if all options are reached

Created on 18 October 2022, over 1 year ago
Updated 8 April 2024, 3 months ago

Scenario (Webform with OptionsLimitWebformHandler):

1. All options (entity references/ Entity radios) are reached
2. WebformEntityTrait gets called after all options are removed (by OptionsLimitWebformHandler), it loads "all options" (in my case all published nodes type "appointment")

  WebformEntityTrait
  ...
  public static function setOptions(array &$element, array $settings = []) {
    if (!empty($element['#options'])) {
      return;
    }

    // Make sure #target_type is not empty.
    if (empty($element['#target_type'])) {
      $element['#options'] = [];
      return;
    }
   ...

Workaround:

We can unset($element['#target_type']) in alterOptionsElement (OptionsLimitWebformHandler), so the options are not loaded again in WebformEntityTrait

   OptionsLimitWebformHandler
   ...
   protected function alterOptionsElement(array &$element, array $limits, array $reached) {
    // Set options element's options labels.
    $options = &$element['#options'];
    $this->alterOptionsElementLabels($options, $limits);

    // Disable or remove reached options.
    if ($reached) {
      switch ($this->configuration['option_none_action']) {
        case WebformOptionsLimitHandlerInterface::LIMIT_ACTION_DISABLE:
          $this->disableOptionsElement($element, $reached);
          break;

        case WebformOptionsLimitHandlerInterface::LIMIT_ACTION_REMOVE:
          $this->removeOptionsElement($element, $reached);
          break;
      }
    }

    // Display limit reached message.
    if (count($limits) === count($reached)) {
      unset($element['#target_type']);  // Workaround !!!
      $this->setElementLimitReachedMessage($element);
    }
  }
  ...

What would be a cleaner Solution?

πŸ› Bug report
Status

Needs work

Version

6.2

Component

Code

Created by

πŸ‡¨πŸ‡­Switzerland jlenni

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

Merge Requests

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.69.0 2024