Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
Last active April 3, 2024 15:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanLaufer/c9ad6dbb2898946fee124a122b84fbe8 to your computer and use it in GitHub Desktop.
Save DanLaufer/c9ad6dbb2898946fee124a122b84fbe8 to your computer and use it in GitHub Desktop.
Drupal 8 - List all paragraphs referenced in a field and frequency used across all content types
<?php
use Drupal\paragraphs\Entity\Paragraph;
$field_name = 'field_my_field';
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists($field_name)
->execute();
$paragraphs = [];
foreach($node_ids as $node_id) {
$node = \Drupal\node\Entity\Node::load($node_id);
$hero_paragraphs = $node->get($field_name)->referencedEntities();
foreach($hero_paragraphs as $parag) {
if($parag->getParagraphType()->id) {
array_push($paragraphs, $parag->getParagraphType()->id);
}
}
}
//print_r(array_unique($paragraphs));
print_r(array_count_values($paragraphs));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment