(Note: There seems to be a problem with Entity Reference elements in Webform 8.x-5.0-beta15, so this is being demonstrated on beta13. I opened a separate issue regarding the problem: https://www.drupal.org/node/2899524)

Here is what I'm trying to do:
Create a webform that will get values of fields from paragraphs in a node and display them as radio buttons.
Here is what I did:
1. Created a Paragraph Type
2. Created a content type and added a Paragraph field of the type I just created, with unlimited number of values
3. Created a node and added three paragraph items
4. Created a view of type Paragraph that correctly displays the Paragraph items and added a Entity Reference display for the view
5. Created a Webform and :
- Added an element of type: Entity radios
- Under ENTITY REFERENCE SETTINGS selected "Paragraph"
- Under "Reference method" selected "Views: Filter by entity reference view"
- Selected the Entity Reference view I created for the paragraphs (item 4 above)
6. Added a Webform field to the same content type

The node displays the form with three items with radio buttons, however the values of the Paragraph fields is not displayed. Instead it displays: the node title and field label from the content type. In my case it's displayed as follows (See also attached image):

O Node 1 > Article Paragraph Field
O Node 1 > Article Paragraph Field
O Node 1 > Article Paragraph Field

The same thing happens if I don't use a View in the webform, and instead use the default setting to show all paragraphs.

When testing with Taxonomy terms they are displays correctly using this same "Entity radios" element.

Comments

ericyellin created an issue. See original summary.

ericyellin’s picture

Issue summary: View changes
ericyellin’s picture

jrockowitz’s picture

StatusFileSize
new2.9 KB

Attached is a webform that replicates your exact issue. The problem is that the Paragraphs module does not support using paragraphs as an entity reference. @see http://cgit.drupalcode.org/paragraphs/tree/paragraphs.module?h=8.x-1.x#n99

I am going to disable Entity reference support for paragraphs in the webform module.

jrockowitz’s picture

Priority: Major » Normal
Status: Active » Needs review
StatusFileSize
new1.37 KB

@ericyellin I know this was not the answer you were hoping for but this is definitely a Paragraphs related issue.

You might be able to use hook_webform_options_alter() or hook_webform_options_WEBFORM_OPTIONS_ID_alter() to create custom options that reference paragraphs.

  • jrockowitz authored 49029af on 8.x-5.x
    Issue #2899556 by jrockowitz, ericyellin: Paragraphs as Entity Reference...
jrockowitz’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

drdam’s picture

Just link of the explaination from paragraph team : https://www.drupal.org/node/2722379

nathan tsai’s picture

I found a workaround for referencing paragraphs, although I don't know if it's defined behaviour though.

membership_type:
  '#type': entity_select
  '#title': 'Paragraph Entity Reference'
  '#target_type': paragraph
  '#selection_handler': views
  '#selection_settings':
    view:
      view_name: my_view # Replace this with your view
      display_name: block_1 # Replace this with your Entity Reference to Paragraph
      arguments: { }

The only problem is that the output becomes "My View Name > My Display Name ([paragraph id])"

See #3249807: Paragraphs can be Referenced Multiple Times when using a View

nathan tsai’s picture

Able to solve the output as "My View Name > My Display Name ([paragraph id])" with a custom Webform Element extending the default entity_select element.

<?PHP

// custom/src/Element/CustomMembersEntitySelectFormatted.php

namespace Drupal\custom\Element;

use Drupal\webform\Element\WebformEntitySelect;
use Drupal\webform\Element\WebformEntityTrait;

/**
 * Provides a webform element for an entity select menu.
 *
 * @FormElement("custom_members_entity_select")
 */
class CustomMembersEntitySelectFormatted extends WebformEntitySelect {

  public static function setOptions(array &$element, array $settings = []) {

    // @TODO Can we strip the tags here?
    // See https://git.drupalcode.org/project/webform/-/blob/6.x/src/Element/WebformEntityTrait.php#L81

    return parent::setOptions($element, $settings);
  }

}

and

<?php
// custom/src/Plugin/WebformElement/CustomMembersEntitySelectFormatted.php

namespace Drupal\custom\Plugin\WebformElement;

use Drupal\webform\Plugin\WebformElement\WebformEntitySelect;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\Plugin\WebformElementEntityOptionsInterface;

use Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait;
use Drupal\webform\Plugin\WebformElement\WebformEntityOptionsTrait;

/**
 * Provides a 'custom_members_entity_select' element.
 *
 * @WebformElement(
 *   id = "custom_members_entity_select",
 *   label = @Translation("Custom Entity select (formatted)"),
 *   description = @Translation("Provides a form element to select a single or multiple entity references using a select menu. Styles paragraph output"),
 *   category = @Translation("Entity reference elements"),
 * )
 */
class CustomMembersEntitySelectFormatted extends WebformEntitySelect implements WebformElementEntityOptionsInterface {

  /**
   * {@inheritdoc}
   */
  protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
    $entity = $this->getTargetEntity($element, $webform_submission, $options);
    if ($entity) {
      $format = $this->getItemFormat($element);
      if ($format === 'link' && $entity->getEntityTypeId() === 'paragraph') {
        if ($entity->bundle() === 'my_paragraph_bundle') {
          return $entity->field_title->value ?? 'Error';
        }
      }
    }

    return parent::formatTextItem($element, $webform_submission, $options);
  }

}

Also needed to remove the field and the field wrapper element in the view (since Webform only strips tags for the webform_entity_select element.