Hi,

I'm using an "Entity Select" element in my webform, but only the title of the view results are shown in the list - not the output that comes out of "Rewrite Result".

  • I have integrated the core #2174633: View output is not used for entityreference options patch. With this patch, all other entity reference selections work fine.
  • I have created an "Entity Select" element in my webform and connected it with the correct reference view.
  • The result: only the title of the view results (= last name) is in the selection list - the "Rewrite Result" should change this to "last name, first name"

My webform with the "Entity Select" - only showing the titles in the list:
Webform EntitySelect

YAML definition of the element:
Webform YAML

Reference View:
Reference View

Other usage of the reference view (works correct):
Other EntitySelect

Any idea?

Regards,
Oliver

Comments

oschuetze created an issue. See original summary.

jrockowitz’s picture

Status: Active » Postponed
simohell’s picture

Patch #143 from https://www.drupal.org/node/2174633 works (tested with Drupal 8.3.7) with Webform element Entity autocomplete. It does not appear to work with Entity radios, Entity select or Entity checkboxes.

The same patch does work with Content type's Entity reference fields using any of the widgets: Autocomplete, Select list, Autocomplete (Tags style), Check boxes/radio buttons

zenimagine’s picture

I applied patch number 174, it works fine with Drupal 8.4 but it does not work with Webform.

ericmaster’s picture

I tracked down to call to $options = self::translateOptions($options, $element); in src/Element/WebformEntityTrait.php to be the one that overrides the Entity select values. If someone is willing to put some time create a patch we'll appreciate (I'll see if I can do myself later).

clemens.tolboom’s picture

Issue summary: View changes
clemens.tolboom’s picture

clemens.tolboom’s picture

Status: Postponed » Needs review
StatusFileSize
new999 bytes

I'm not sure why unwinding the options and translate those. And why the trait states

Translate the select options.

Attached patch is as below (commenting out loading translated nodes).

Please provide some manually test suggestions!

 /**
   * Translate the select options.
   *
   * @param array $options
   *   Untranslated options.
   * @param array $element
   *   An element.
   *
   * @return array
   *   Translated options.
   */
  protected static function translateOptions(array $options, array $element) {
    /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
    $entity_repository = \Drupal::service('entity.repository');

    foreach ($options as $key => $value) {
      if (is_array($value)) {
        $options[$key] = self::translateOptions($value, $element);
      }
      else {
//        // Set the entity in the correct language for display.
//        $option = \Drupal::entityTypeManager()
//          ->getStorage($element['#target_type'])
//          ->load($key);
//        $option = $entity_repository->getTranslationFromContext($option);
//        $options[$key] = $option->label();
      }
    }
    return $options;
  }

clemens.tolboom’s picture

Status: Needs review » Needs work
StatusFileSize
new18.29 KB

Hmmm ... above was not a wise call. I was testing checkboxes.

Removing the lines in #8 results into a select containing HTML


Fixing the view result is IMHO too late as $options contains rendered view result which are then escaped.

clemens.tolboom’s picture

After some debugging I did not found a direct solution. The options when coming from views should be typed as safe while user input must be cleaned somehow.

[edit] option is not allowed any other content but text https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option [/edit]

ericmaster’s picture

StatusFileSize
new870 bytes

The override which wouldn't allow to display custom output comes from this line specifically

//        $options[$key] = $option->label();

We should probably assume that field output coming from view is already translated. Added a condition for checking wether the selection handler is coming from a view and generated a patch that just strip tags, which should take care of wrapping tags issue, although I think this can also be fixed through views configuration.

ericmaster’s picture

Status: Needs work » Needs review
jrockowitz’s picture

@Ericmaster The patch makes sense, but we should not call ::translateOptions when the entity select is 'views'.

The Views 'Entity Reference' display should be responsible for stripping HTML tags for the option text.

The below statement should be added to \Drupal\webform\Element\WebformEntityTrait::setOptions

//  If the selection handle is not views the translate entity reference options.
if ($element['#selection_handler'] != 'views')  {
   $options = self::translateOptions($options, $element);
}
jrockowitz’s picture

StatusFileSize
new678 bytes

The attached patch, hopefully, is the simplest solution. If someone can review and mark this ticket RTBC, I will commit it.

  • jrockowitz committed 3901ae0 on 8.x-5.x
    Issue #2890618 by clemens.tolboom, jrockowitz, Ericmaster, oschuetze:...
jrockowitz’s picture

Status: Needs review » Fixed

I committed the patch. Please download the latest dev release to review.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

akalata’s picture

Just a note that even with the committed Webform patch, the Core patch referenced in #7 must still be applied.