Comma separated list option for multiple value fields

Created on 11 July 2019, almost 6 years ago
Updated 27 January 2023, about 2 years ago

Currently an HTML list will be generated for a composite field with multiple values. This winds up being a problem when using Views data export β†’ because the resulting entry is simply the concatenation of the values with no separator. CSS formatting can be used on views that display a list but that does not work for the export option.

The current format option is not being used. The following can be added to WebformSubmissionCompositeField.php to use the List format to generate a simple comma separated text list. I was not able to figure out a way to do this using the Rewrite feature and TWIG.

      // added to the render function

      if ($this->options['webform_element_format']=='list') {
        // List format is comma separated list
        $result = [];
        for ( $i = 0; ; $i++ ) {
          $options['delta'] = $i;
          $formatted_item = $this->webformElementManager->invokeMethod('formatHtml', $composite_element, $webform_submission, $options);
          if ($formatted_item) {
            $result[] = $formatted_item['#plain_text'];
          } else {
            break;
          }
        }

        return [ '#markup' => implode(', ',$result) ];
      }

It might be preferable to add another option field with a separator field so that something other than a comma separator could be used. It is possible to use TWIG to replace the comma but only if the text values do not include commas.

✨ Feature request
Status

Needs work

Version

5.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bwong

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.

  • I confirmed that the code provided does not work for multi-valued fields. In addition, the code didn't meet coding standards. Here's a modified version (which still doesn't solve the issue for multi-valued fields):

          // List format is a comma-separated list.
          if ($this->options['webform_element_format'] == 'list') {
            $result = [];
            $options['delta'] = 0;
            while ($formatted_item = $this->webformElementManager->invokeMethod('formatHtml',$composite_element, $webform_submission, $options)) {
              $result[] = $formatted_item['#plain_text'];
              $options['delta'] += 1;
            }
    
            return ['#markup' => implode(', ', $result)];
          }
    
  • Status changed to Needs work about 2 years ago
Production build 0.71.5 2024