0

I have a webform in Drupal 8 with an entity select element. The element is populated from an entity reference view. I have a book content type with information about the book such as title and author. I also have a field collection in book containing more specific details such as ISBN and format. The entity reference view displays title-isbn-format. For example:

Dune - 9780441013593 - Paperback

Dune - 9780143111580 - Hardcover

I applied the view entity reference patch posted here. The form correctly displays the output of the view. How do I get the same display in the form submission? If I set the form display to value and select Dune paperback the result is "MyBookFieldCollection 0 of Dune (9)."

I tried using tokens but I can only get the field collection info, not the book info.

Here is the source for the element:

book_title:
  '#type': entity_select
  '#title': 'Book Title'
  '#select2': true
  '#format': custom
  '#format_html': '[webform_submission:values:book_title:node:title] - [webform_submission:values:book_title:entity:field_isbn_13] - [webform_submission:values:book_title:entity:field_format]'
  '#format_text': '[webform_submission:values:book_title:node:title] - [webform_submission:values:book_title:entity:field_isbn_13] - [webform_submission:values:book_title:entity:field_format]'
  '#target_type': field_collection_item
  '#selection_handler': views
  '#selection_settings':
    view:
      view_name: entity_reference_for_form
      display_name: entity_reference_1
      arguments: {  }

This correctly displays the ISBN and book format but not the book title.

1 Answer 1

0

I was able to get the desired result for the book title by using twig and string manipulation.

I noticed {{ item.label }} returns MyBookFieldCollection 0 of Dune. MyBookFieldCollection is the name of the field collection and 0 represents the selected item. There will not be more than five items in a given collection. I can remove the start of the string using {{ item.label[27:] }} and this returns a value of Dune.

Here is the modified element source:

book_title:
  '#type': entity_select
  '#title': 'Book Title'
  '#select2': true
  '#format': custom
  '#format_html': '{{ item.label[27:] }} - [webform_submission:values:book_title:entity:field_isbn_13] - [webform_submission:values:book_title:entity:field_format]'
  '#format_text': '{{ item.label[27:] }} - [webform_submission:values:book_title:entity:field_isbn_13] - [webform_submission:values:book_title:entity:field_format]'
  '#target_type': field_collection_item
  '#selection_handler': views
  '#selection_settings':
    view:
      view_name: entity_reference_for_form
      display_name: entity_reference_1
      arguments: {  }

This outputs Dune - 9780441013593 - Paperback in the form submission.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.