diff --git a/src/Controller/BlockUsageReportController.php b/src/Controller/BlockUsageReportController.php index 8a6fb11..2f5966f 100644 --- a/src/Controller/BlockUsageReportController.php +++ b/src/Controller/BlockUsageReportController.php @@ -3,16 +3,24 @@ namespace Drupal\block_usage_report\Controller; use Drupal\Core\Controller\ControllerBase; -use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityListBuilderInterface; use Drupal\Core\Entity\EntityStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Database\Connection; +use Drupal\Core\Url; /** * Returns output for the block usage report. */ class BlockUsageReportController extends ControllerBase { + /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + /** * The block storage provider. * @@ -62,19 +70,23 @@ class BlockUsageReportController extends ControllerBase { * @param \Drupal\Core\Entity\EntityStorageInterface|null $fixed_block_content_storage * The fixed block content storage provider, or NULL if the associated * module does not exist. + * @param \Drupal\Core\Database\Connection $connection + * The Database connection. */ public function __construct( EntityStorageInterface $block_storage, EntityStorageInterface $block_content_storage, EntityListBuilderInterface $block_list_builder, EntityListBuilderInterface $block_content_list_builder, - ?EntityStorageInterface $fixed_block_content_storage + ?EntityStorageInterface $fixed_block_content_storage, + Connection $connection ) { $this->blockStorage = $block_storage; $this->blockContentStorage = $block_content_storage; $this->blockListBuilder = $block_list_builder; $this->blockContentListBuilder = $block_content_list_builder; $this->fixedBlockContentStorage = $fixed_block_content_storage; + $this->connection = $connection; } /** @@ -99,7 +111,8 @@ class BlockUsageReportController extends ControllerBase { $entity_type_mgr->getStorage('block_content'), $entity_type_mgr->getListBuilder('block'), $entity_type_mgr->getListBuilder('block_content'), - $fixed_block_storage + $fixed_block_storage, + $container->get('database') ); } @@ -108,16 +121,16 @@ class BlockUsageReportController extends ControllerBase { * * @param string $uuid * The UUID to search for. - * @param \Drupal\Core\Entity\ContentEntityInterface[] $entities + * @param \stdClass[] $objects * The collection of entities to search for. * - * @return \Drupal\Core\Entity\ContentEntityInterface|null + * @return object|null * THe matching entity, or NULL if not found. */ - protected function getEntityByUuid(string $uuid, array $entities): ?ContentEntityInterface { - foreach ($entities as $entity) { - if ($entity->uuid() == $uuid) { - return $entity; + protected function getEntityByUuid(string $uuid, array $objects): ?\stdClass { + foreach ($objects as $object) { + if ($object->uuid == $uuid) { + return $object; } } return NULL; @@ -131,7 +144,17 @@ class BlockUsageReportController extends ControllerBase { */ public function build(): array { /** @var \Drupal\Core\Entity\ContentEntityInterface[] $bc_blocks */ - $bc_blocks = $this->blockContentStorage->loadMultiple(); + // $bc_blocks = $this->blockContentStorage->loadMultiple(); + $query = $this->connection + ->select('block_content_field_data', 'bcfd'); + $query->fields('bcfd', [ + 'id', + 'type', + 'info', + ]); + $query->fields('bc', ['uuid']) + ->innerJoin('block_content', 'bc', 'bc.id = bcfd.id'); + $bc_blocks = $query->execute()->fetchAll(); $bc_uuids_found = []; /** @var \Drupal\block\Entity\Block[] $blocks */ @@ -169,7 +192,7 @@ class BlockUsageReportController extends ControllerBase { if ($provider == 'block_content') { $uuid = explode(':', $plugin_id)[1]; $block_content = $this->getEntityByUuid($uuid, $bc_blocks); - $type = $block_content ? $block_content->bundle() : $this->t('[broken/missing]'); + $type = $block_content ? $block_content->type : $this->t('[broken/missing]'); $bc_uuids_found[] = $uuid; } elseif ( @@ -225,14 +248,24 @@ class BlockUsageReportController extends ControllerBase { $unplaced_blocks = []; foreach ($bc_blocks as $bc_block) { - if (!in_array($bc_block->uuid(), $bc_uuids_found)) { + if (!in_array($bc_block->uuid, $bc_uuids_found)) { $unplaced_blocks[] = [ - $bc_block->label() . ' (' . $bc_block->id() . ')', - $bc_block->bundle(), + $bc_block->info . ' (' . $bc_block->id . ')', + $bc_block->type, [ 'data' => [ '#type' => 'operations', - '#links' => $this->blockContentListBuilder->getOperations($bc_block), + '#links' => [ + [ + 'title' => t('Edit'), + 'url' => Url::fromRoute('entity.block_content.edit_form', ['block_content' => $bc_block->id]), + 'localized_options' => [ + 'attributes' => [ + 'title' => t('Edit'), + ], + ], + ], + ], ], ], ]; @@ -252,7 +285,8 @@ class BlockUsageReportController extends ControllerBase { if (!empty($enabled_rows)) { $out['enabled_wrapper'] = [ '#type' => 'details', - '#title' => $this->t('Enabled blocks'), + '#title' => sprintf('%s (%s)', $this->t('Enabled blocks'), count($enabled_rows)), + '#description' => $this->t('Blocks enabled in regions'), '#open' => FALSE, ]; $out['enabled_wrapper']['enabled'] = [ @@ -264,7 +298,8 @@ class BlockUsageReportController extends ControllerBase { if (!empty($disabled_rows)) { $out['disabled_wrapper'] = [ '#type' => 'details', - '#title' => $this->t('Disabled blocks'), + '#title' => sprintf('%s (%s)', $this->t('Disabled blocks'), count($disabled_rows)), + '#description' => $this->t('Blocks disabled in regions'), '#open' => FALSE, ]; $out['disabled_wrapper']['disabled'] = [ @@ -276,7 +311,8 @@ class BlockUsageReportController extends ControllerBase { if (!empty($unplaced_blocks)) { $out['unplaced_wrapper'] = [ '#type' => 'details', - '#title' => $this->t('Unplaced custom blocks'), + '#title' => sprintf('%s (%s)', $this->t('Unplaced custom blocks'), count($unplaced_blocks)), + '#description' => $this->t('Custom blocks not placed in any region.'), '#open' => FALSE, ]; $out['unplaced_wrapper']['unplaced'] = [