Last active
April 3, 2024 15:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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