Skip to main content
31 votes

How do I alter the behavior of the form fields contained within the paragraph field?

Since paragraphs^1.3 you can use hook_field_widget_WIDGET_TYPE_form_alter() (I linked the 8.2 version because there is a good comment about using the hook). You can see the following example in ...
Lars Jendrzejewski's user avatar
30 votes
Accepted

How to load paragraph entity on node object?

$paragraph = $node->field_paragraph->getValue(); // Loop through the result set. foreach ( $paragraph as $element ) { $p = \Drupal\paragraphs\Entity\Paragraph::load( $element['target_id'] ); ...
Tim Ahmedov's user avatar
21 votes

How to load paragraph entity on node object?

Just replace get() with field name directly : $node = \Drupal\node\Entity\Node::load(1); $paras = $node->field_paragraph->referencedEntities();
Ken's user avatar
  • 311
17 votes
Accepted

How to get paragraph field values?

You should be able to include the Paragraph entity class, and use Paragraph::load($entity_id) to load it. Example: use Drupal\paragraphs\Entity\Paragraph; $paragraph = Paragraph::load($target_id); $...
Kevin's user avatar
  • 27.9k
16 votes
Accepted

Check Content Type in Paragraph Preprocess

The correct way of getting the entity data referencing to your paragraph is to use Paragraph's entity API. /** * Implements template_preprocess_paragraph(). * * @param array $variables * An ...
hugronaphor's user avatar
  • 1,462
14 votes
Accepted

Programmatically append multiple paragraphs to entity reference field on node

When you add a paragraph you also need to pass the revision id: $node->field_qs_and_as[] = [ 'target_id' => $paragraph->id(), 'target_revision_id' => $paragraph->...
4uk4's user avatar
  • 101k
13 votes

How do we re-use Paragraph items?

Paragraphs is focused on reusing layouts not content. The most obvious implication is on revisions. In a standard reference field you reference the latest revision of the target. When you roll back ...
4uk4's user avatar
  • 101k
12 votes

How do I get the host paragraph when a paragraph is referenced in another paragraph?

If you look at the Paragraph class in the module, you will find a public method called getParentEntity that return the parent entity of a Paragraph instance. So, if you do: $parent = $paragraph->...
Kevin's user avatar
  • 27.9k
12 votes
Accepted

How to set validation error for specific subfield of the paragraph type?

In the docs page for setErrorByName() we can read: string $name: The name of the form element. If the #parents property of your form element is array('foo', 'bar', 'baz') then you may set an error ...
kenorb's user avatar
  • 14.2k
11 votes

How do I alter the behavior of the form fields contained within the paragraph field?

If you are using Paragraphs Browser Field Widgets, then the WIDGET_TYPE ids for hook hook_field_widget_WIDGET_TYPE_form_alter() are different: Field Widget Label Field Widget ID Provider Paragraphs ...
baikho's user avatar
  • 1,735
11 votes

How to get paragraph field values?

$paragraph = Paragraph::load($target_id); // Paragraph type could be also useful. $prgTypeId = $paragraph->getType(); /** @var \Drupal\Core\Field\EntityReferenceFieldItemList $prgMediaField */ $...
Martin Klíma's user avatar
10 votes

How to get paragraph field values?

I know this is old, but might be helpful for the future. To load entities referenced by a Node entity you can use the entity property. For example, consider the following code: $nodestorage = \Drupal:...
Claudiu Chirilov's user avatar
10 votes
Accepted

How do I delete all entities of a bundle?

There are some examples of removing nodes of a certain content type. In that case node is the entity type and article could be bundle (content type). This is no different. paragraph is the entity and [...
Neograph734's user avatar
  • 8,429
9 votes

How do I get a field value in another field's template on a paragraph?

For non-paragraph fields, there is a node object available in the variables of the field template, so you can put this in a field.html.twig file: {{ element['#object'].machine_name_of_other_field....
peezy's user avatar
  • 425
9 votes

How do I delete an orphaned paragraph node?

In Drupal 8, you can still use entity_delete_multiple(), but it is marked as deprecated and it is going to be removed from Drupal 9. It makes easy to remove those entities using Drush, for example, ...
apaderno's user avatar
  • 97.6k
9 votes

paragraph template suggestion based on parent content type

Add a theme name suggestion: mytheme.theme: /** * Implements hook_theme_suggestions_HOOK_alter(). */ function mytheme_theme_suggestions_paragraph_alter(&$suggestions, $variables) { $...
4uk4's user avatar
  • 101k
9 votes
Accepted

How to get token of parent entity in paragraph?

There's nothing out of the box (see this issue for the details). Implementing your own token isn't too tricky. I haven't had a chance to test this but I'm pretty sure it'll work: function ...
Clive's user avatar
  • 167k
8 votes

Removing div wrapper container of paragraph field and get only the plain text in twig

You shouldn't do {{ content.field_home_slide_snippet[0]|raw }}. You should do {{ content.field_home_slide_snippet }} and edit the twig template for field--field-home-slide-snippet.html.twig to have no ...
Kevin's user avatar
  • 27.9k
8 votes
Accepted

How limit access to a paragraph type by role, like it is for content types?

Simply enable the Paragraphs sub-module Paragraphs Type Permissions. It provides CRUD permissions per Paragraphs Type. http://cgit.drupalcode.org/paragraphs/tree/modules/paragraphs_type_permissions/...
leymannx's user avatar
  • 18.3k
8 votes
Accepted

How to hook on paragraph deletion?

You need to implement a node update hook and compare the paragraph IDs: use Drupal\node\Entity\NodeInterface; /** * Implements hook_ENTITY_TYPE_update() for node entities. */ function ...
4uk4's user avatar
  • 101k
7 votes
Accepted

Fields in Paragraphs twig template

The field variables are not right - they need to correlate to the field name. Example: {{ content.field_icon_1 }} {{ content.field_icon_1_title }} These are the render arrays you are looking for. {...
Kevin's user avatar
  • 27.9k
7 votes
Accepted

Module's optional field config not imported on install

Optional config is only installed if it has an explicit dependency on a module that is being installed right now. Otherwise that configuration would be re-installed every time you install a module, ...
Berdir's user avatar
  • 83.3k
7 votes
Accepted

Custom Theming Bootsrap Carousel with Paragraph Type

Unless you need some very specific logic, I wouldn't use preprocessing, but use Drupal's template suggestions and Twig instead. I always use Slick over CDN, adjust for your own needs if necessary. ...
Hudri's user avatar
  • 5,535
6 votes
Accepted

How do I delete an orphaned paragraph node?

If you are not afraid to do it programmatically, you could use this code to load and delete the paragraph: $entity = \Drupal::entityTypeManager()->getStorage('paragraph')->load($paragraph_id); ...
alexej_d's user avatar
  • 850
6 votes

How do I delete an orphaned paragraph node?

This is actually caused by the bug Removed paragraph entities are not deleted from database. When that gets fixed, this problem will stop getting worse (bloating our databases). To remove all of ...
colan's user avatar
  • 2,188
6 votes

Create any number of paragraph fields in a multiple value field on node create form

Instead of adding the multi value field items after the form is built you can try to do this on the entity before the form is built: /** * Implements hook_ENTITY_TYPE_create() for 'node'. */ ...
4uk4's user avatar
  • 101k
6 votes
Accepted

Export Paragraph bundles

Turns out it can be done with the config:export:entity command. drupal config:export:entity paragraphs_type [PARAGRAPH_TYPE] --module="[YOUR_MODULE]" --optional-config --remove-uuid --remove-...
Bram's user avatar
  • 249
6 votes
Accepted

Create nested paragraphs programmatically

save() returns the status of the save operation, not the entity. And you probably don't need to save the paragraphs, saving the node should save new paragraphs automatically. So you can try this ...
4uk4's user avatar
  • 101k
5 votes
Accepted

Pre fill multiple paragraphs while adding node

You can't reuse existing paragraphs, you have to create new paragraphs for each node: $paragraph1 = Paragraph::create([ 'title' => 'Paragraph 1', 'type' => 'paragraph_type1', ]); $...
4uk4's user avatar
  • 101k

Only top scored, non community-wiki answers of a minimum length are eligible