0

Is there a way to identify a paragraph in Drupal 8. Like is there a function to get the type as paragraph in code level?

I need to get it from a form alter function. My requirement is to get all the paragraphs in a form. Or identify the paragraph fields.

  • 1
    try $paragraphs->bundle(); or $paragraphs->getType(); – No Sssweat Nov 20 '17 at 8:21
  • @NoSssweat Thanks ! but i need to get it from a form alter function. My requirement is to get all the paragraphs in a form. Or identify the paragraph fields. – i am batman Nov 20 '17 at 8:53
  • Can you not do this within TEMPLATE_preprocess_paragraph ? – John Cogan Nov 20 '17 at 11:49
  • Can you add more details about the form? Is it an entity add/edit form or a custom one? – AjitS Jan 20 at 7:44
1

You probably should look at the paragraphs module code, it does a lot of this.

That said, the basic process would probably be:

  • Identify the form fields that are entity_reference_revision fields.
  • Check that the field is limited to paragraph types (can just check 1st one).
  • If it is an update form, get the referenced entities from the field.
  • You will then need some recursive code to walk the paragraph tree to get sub paragraphs.

This will require using a lot of the field definition classes to get the information needed.

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