With over a decade of web development experience, I specialize in Drupal (7, 8, 9, 10), CodeIgniter, Laravel, and WordPress. I offer extensive expertise in both module and theme development, providing customized solutions for complex projects. Whether you need to enhance an existing platform, create new features, or seek expert guidance, I'm here to assist. My dedication to delivering high-quality, efficient, and scalable solutions is unmatched. Feel free to contact me to explore how I can contribute to your project's success. Let's turn your ideas into reality!

“Discover paragraph usage patterns across Drupal 8 content types by listing and counting references in specified fields.”
<?php

use Drupal\paragraphs\Entity\Paragraph;

// Define the field name.
$field_name = 'field_my_field';
  • This line initializes a variable $field_name with the value 'field_my_field', which is assumed to be the machine name of a field on the nodes.
$query = \Drupal::entityQuery('node');
  • This line creates a new entity query for nodes using the \Drupal::entityQuery() method.
$node_ids = $query
    ->condition('status', 1)
    ->exists($field_name)
    ->execute();
  • This section of code sets conditions on the entity query. It fetches node IDs where the status is 1 (published) and the specified field (field_my_field) exists. The execute() method is then called to execute the query and retrieve the result set of node IDs.
$paragraphs = [];
  • Initializes an empty array called $paragraphs that will be used to store paragraph type IDs.
foreach($node_ids as $node_id) {
  • Initiates a loop over the retrieved node IDs.
  $node = \Drupal\node\Entity\Node::load($node_id);
  • Loads each node by its ID using the \Drupal\node\Entity\Node::load() method.
  $hero_paragraphs = $node->get($field_name)->referencedEntities();
  • Retrieves referenced entities (paragraphs) from the specified field (field_my_field) on the loaded node.
  foreach($hero_paragraphs as $parag) {
  • Iterates over the referenced paragraphs for the current node.
    if($parag->getParagraphType()->id) {
  • Checks if the paragraph type ID exists for the current paragraph.
      array_push($paragraphs, $parag->getParagraphType()->id);
  • If the paragraph type ID exists, it is pushed into the $paragraphs array.
    }
  }
}
//print_r(array_unique($paragraphs));
print_r(array_count_values($paragraphs));
  • This section either prints the unique values of the $paragraphs array or prints an associative array where keys are the unique paragraph type IDs and values are the count of each paragraph type.

In summary, this code fetches node IDs that meet certain criteria, loads each node, retrieves referenced paragraphs from a specific field on those nodes, and then counts the occurrences of each paragraph type. The results are printed for further analysis.

Full code:
 

<?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));
Posted by Sujan Shrestha
Categorized:
PREVIOUS POST
banner