/**
* Find all nodes that contain a specific block in their layout config.
*
* @param string $blockName
* The ID of the block
*
* @return array
* The array of nodes.
*/
function findNodesByLayoutBlock($blockName) {
$query = \Drupal::database()
->select('node__layout_builder__layout', 'n');
$query->fields('n', ['entity_id', 'layout_builder__layout_section']);
$results = $query->execute()->fetchAll();
$nodes = [];
foreach ($results as $result) {
$layout = $result->layout_builder__layout_section;
$layout = unserialize($layout);
$components = $layout->getComponents();
foreach ($components as $component) {
if ($blockName === $component->getPluginId()) {
$nodes[] = $result->entity_id;
}
}
}
return $nodes;
}
Note: Be wary of using on sites with lots of nodes.
Add new comment