Issue #3043797 by tim.plunkett, johndevman, phenaproxima: Create an interface for InlineBlockUsage

merge-requests/1119/head
xjm 2019-03-27 18:54:56 -05:00
parent d28a92f7bb
commit f4f6026156
5 changed files with 74 additions and 40 deletions

View File

@ -8,7 +8,7 @@ use Drupal\block_content\Event\BlockContentGetDependencyEvent;
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\layout_builder\InlineBlockUsage; use Drupal\layout_builder\InlineBlockUsageInterface;
use Drupal\layout_builder\LayoutEntityHelperTrait; use Drupal\layout_builder\LayoutEntityHelperTrait;
use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface; use Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -52,7 +52,7 @@ class SetInlineBlockDependency implements EventSubscriberInterface {
/** /**
* The inline block usage service. * The inline block usage service.
* *
* @var \Drupal\layout_builder\InlineBlockUsage * @var \Drupal\layout_builder\InlineBlockUsageInterface
*/ */
protected $usage; protected $usage;
@ -63,12 +63,12 @@ class SetInlineBlockDependency implements EventSubscriberInterface {
* The entity type manager. * The entity type manager.
* @param \Drupal\Core\Database\Connection $database * @param \Drupal\Core\Database\Connection $database
* The database connection. * The database connection.
* @param \Drupal\layout_builder\InlineBlockUsage $usage * @param \Drupal\layout_builder\InlineBlockUsageInterface $usage
* The inline block usage service. * The inline block usage service.
* @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager * @param \Drupal\layout_builder\SectionStorage\SectionStorageManagerInterface $section_storage_manager
* The section storage manager. * The section storage manager.
*/ */
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, InlineBlockUsage $usage, SectionStorageManagerInterface $section_storage_manager) { public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, InlineBlockUsageInterface $usage, SectionStorageManagerInterface $section_storage_manager) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->database = $database; $this->database = $database;
$this->usage = $usage; $this->usage = $usage;
@ -128,7 +128,6 @@ class SetInlineBlockDependency implements EventSubscriberInterface {
// dependency. It may be used by another module besides layout builder. // dependency. It may be used by another module besides layout builder.
return NULL; return NULL;
} }
/** @var \Drupal\layout_builder\InlineBlockUsage $usage */
$layout_entity_storage = $this->entityTypeManager->getStorage($layout_entity_info->layout_entity_type); $layout_entity_storage = $this->entityTypeManager->getStorage($layout_entity_info->layout_entity_type);
$layout_entity = $layout_entity_storage->load($layout_entity_info->layout_entity_id); $layout_entity = $layout_entity_storage->load($layout_entity_info->layout_entity_id);
if ($this->isLayoutCompatibleEntity($layout_entity)) { if ($this->isLayoutCompatibleEntity($layout_entity)) {

View File

@ -23,7 +23,7 @@ class InlineBlockEntityOperations implements ContainerInjectionInterface {
/** /**
* Inline block usage tracking service. * Inline block usage tracking service.
* *
* @var \Drupal\layout_builder\InlineBlockUsage * @var \Drupal\layout_builder\InlineBlockUsageInterface
*/ */
protected $usage; protected $usage;
@ -52,7 +52,7 @@ class InlineBlockEntityOperations implements ContainerInjectionInterface {
* *
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service. * The entity type manager service.
* @param \Drupal\layout_builder\InlineBlockUsage $usage * @param \Drupal\layout_builder\InlineBlockUsageInterface $usage
* Inline block usage tracking service. * Inline block usage tracking service.
* @param \Drupal\Core\Database\Connection $database * @param \Drupal\Core\Database\Connection $database
* The database connection. * The database connection.
@ -65,7 +65,7 @@ class InlineBlockEntityOperations implements ContainerInjectionInterface {
* - The $database parameter is unused and should be removed. * - The $database parameter is unused and should be removed.
* Deprecate in https://www.drupal.org/node/3031492. * Deprecate in https://www.drupal.org/node/3031492.
*/ */
public function __construct(EntityTypeManagerInterface $entityTypeManager, InlineBlockUsage $usage, Connection $database, SectionStorageManagerInterface $section_storage_manager = NULL) { public function __construct(EntityTypeManagerInterface $entityTypeManager, InlineBlockUsageInterface $usage, Connection $database, SectionStorageManagerInterface $section_storage_manager = NULL) {
$this->entityTypeManager = $entityTypeManager; $this->entityTypeManager = $entityTypeManager;
$this->blockContentStorage = $entityTypeManager->getStorage('block_content'); $this->blockContentStorage = $entityTypeManager->getStorage('block_content');
$this->usage = $usage; $this->usage = $usage;

View File

@ -7,10 +7,8 @@ use Drupal\Core\Entity\EntityInterface;
/** /**
* Service class to track inline block usage. * Service class to track inline block usage.
*
* @internal
*/ */
class InlineBlockUsage { class InlineBlockUsage implements InlineBlockUsageInterface {
/** /**
* The database connection. * The database connection.
@ -30,12 +28,7 @@ class InlineBlockUsage {
} }
/** /**
* Adds a usage record. * {@inheritdoc}
*
* @param int $block_content_id
* The block content id.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The layout entity.
*/ */
public function addUsage($block_content_id, EntityInterface $entity) { public function addUsage($block_content_id, EntityInterface $entity) {
$this->database->merge('inline_block_usage') $this->database->merge('inline_block_usage')
@ -47,13 +40,7 @@ class InlineBlockUsage {
} }
/** /**
* Gets unused inline block IDs. * {@inheritdoc}
*
* @param int $limit
* The maximum number of block content entity IDs to return.
*
* @return int[]
* The entity IDs.
*/ */
public function getUnused($limit = 100) { public function getUnused($limit = 100) {
$query = $this->database->select('inline_block_usage', 't'); $query = $this->database->select('inline_block_usage', 't');
@ -64,10 +51,7 @@ class InlineBlockUsage {
} }
/** /**
* Remove usage record by layout entity. * {@inheritdoc}
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The layout entity.
*/ */
public function removeByLayoutEntity(EntityInterface $entity) { public function removeByLayoutEntity(EntityInterface $entity) {
$query = $this->database->update('inline_block_usage') $query = $this->database->update('inline_block_usage')
@ -81,10 +65,7 @@ class InlineBlockUsage {
} }
/** /**
* Delete the inline blocks' the usage records. * {@inheritdoc}
*
* @param int[] $block_content_ids
* The block content entity IDs.
*/ */
public function deleteUsage(array $block_content_ids) { public function deleteUsage(array $block_content_ids) {
if (!empty($block_content_ids)) { if (!empty($block_content_ids)) {
@ -94,13 +75,7 @@ class InlineBlockUsage {
} }
/** /**
* Gets usage record for inline block by ID. * {@inheritdoc}
*
* @param int $block_content_id
* The block content entity ID.
*
* @return object
* The usage record with properties layout_entity_id and layout_entity_type.
*/ */
public function getUsage($block_content_id) { public function getUsage($block_content_id) {
$query = $this->database->select('inline_block_usage'); $query = $this->database->select('inline_block_usage');

View File

@ -0,0 +1,60 @@
<?php
namespace Drupal\layout_builder;
use Drupal\Core\Entity\EntityInterface;
/**
* Defines an interface for tracking inline block usage.
*/
interface InlineBlockUsageInterface {
/**
* Adds a usage record.
*
* @param int $block_content_id
* The block content ID.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The layout entity.
*/
public function addUsage($block_content_id, EntityInterface $entity);
/**
* Gets unused inline block IDs.
*
* @param int $limit
* The maximum number of block content entity IDs to return.
*
* @return int[]
* The entity IDs.
*/
public function getUnused($limit = 100);
/**
* Remove usage record by layout entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The layout entity.
*/
public function removeByLayoutEntity(EntityInterface $entity);
/**
* Delete the inline blocks' the usage records.
*
* @param int[] $block_content_ids
* The block content entity IDs.
*/
public function deleteUsage(array $block_content_ids);
/**
* Gets usage record for inline block by ID.
*
* @param int $block_content_id
* The block content entity ID.
*
* @return object
* The usage record with properties layout_entity_id and layout_entity_type.
*/
public function getUsage($block_content_id);
}

View File

@ -264,7 +264,7 @@ class InlineBlockTest extends InlineBlockTestBase {
public function testDeletion() { public function testDeletion() {
/** @var \Drupal\Core\Cron $cron */ /** @var \Drupal\Core\Cron $cron */
$cron = \Drupal::service('cron'); $cron = \Drupal::service('cron');
/** @var \Drupal\layout_builder\InlineBlockUsage $usage */ /** @var \Drupal\layout_builder\InlineBlockUsageInterface $usage */
$usage = \Drupal::service('inline_block.usage'); $usage = \Drupal::service('inline_block.usage');
$this->drupalLogin($this->drupalCreateUser([ $this->drupalLogin($this->drupalCreateUser([
'administer content types', 'administer content types',