Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
Last active May 14, 2019 15:48
Show Gist options
  • Save DanLaufer/6d33a138b5bce0c6a115b722dc0b424d to your computer and use it in GitHub Desktop.
Save DanLaufer/6d33a138b5bce0c6a115b722dc0b424d to your computer and use it in GitHub Desktop.
Drupal 8 - List all nids with a specific paragraph in a specific field
<?php
use Drupal\paragraphs\Entity\Paragraph;
$field_containing_paragraph = 'field_my_field';
$paragraph_type_to_find = 'my_paragraph';
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists($field_containing_paragraph)
->execute();
print_r($paragraph_type_to_find . PHP_EOL);
foreach($node_ids as $node_id) {
$node = \Drupal\node\Entity\Node::load($node_id);
$paragraphs = $node->get($field_containing_paragraph)->referencedEntities();
foreach($paragraphs as $parag) {
if($parag->getParagraphType()->id === $paragraph_type_to_find) {
print_r($node_id . PHP_EOL);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment