You are here

class InlineBlockUsage in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/src/InlineBlockUsage.php \Drupal\layout_builder\InlineBlockUsage
  2. 10 core/modules/layout_builder/src/InlineBlockUsage.php \Drupal\layout_builder\InlineBlockUsage

Service class to track inline block usage.

Hierarchy

Expanded class hierarchy of InlineBlockUsage

1 file declares its use of InlineBlockUsage
InlineBlockUsageTest.php in core/modules/layout_builder/tests/src/Unit/InlineBlockUsageTest.php
1 string reference to 'InlineBlockUsage'
layout_builder.services.yml in core/modules/layout_builder/layout_builder.services.yml
core/modules/layout_builder/layout_builder.services.yml
1 service uses InlineBlockUsage
inline_block.usage in core/modules/layout_builder/layout_builder.services.yml
Drupal\layout_builder\InlineBlockUsage

File

core/modules/layout_builder/src/InlineBlockUsage.php, line 11

Namespace

Drupal\layout_builder
View source
class InlineBlockUsage implements InlineBlockUsageInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Creates an InlineBlockUsage object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public function addUsage($block_content_id, EntityInterface $entity) {
    $this->database
      ->merge('inline_block_usage')
      ->keys([
      'block_content_id' => $block_content_id,
      'layout_entity_id' => $entity
        ->id(),
      'layout_entity_type' => $entity
        ->getEntityTypeId(),
    ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getUnused($limit = 100) {
    $query = $this->database
      ->select('inline_block_usage', 't');
    $query
      ->fields('t', [
      'block_content_id',
    ]);
    $query
      ->isNull('layout_entity_id');
    $query
      ->isNull('layout_entity_type');
    return $query
      ->range(0, $limit)
      ->execute()
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function removeByLayoutEntity(EntityInterface $entity) {
    $query = $this->database
      ->update('inline_block_usage')
      ->fields([
      'layout_entity_type' => NULL,
      'layout_entity_id' => NULL,
    ]);
    $query
      ->condition('layout_entity_type', $entity
      ->getEntityTypeId());
    $query
      ->condition('layout_entity_id', $entity
      ->id());
    $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteUsage(array $block_content_ids) {
    if (!empty($block_content_ids)) {
      $query = $this->database
        ->delete('inline_block_usage')
        ->condition('block_content_id', $block_content_ids, 'IN');
      $query
        ->execute();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getUsage($block_content_id) {
    $query = $this->database
      ->select('inline_block_usage');
    $query
      ->condition('block_content_id', $block_content_id);
    $query
      ->fields('inline_block_usage', [
      'layout_entity_id',
      'layout_entity_type',
    ]);
    $query
      ->range(0, 1);
    return $query
      ->execute()
      ->fetchObject();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InlineBlockUsage::$database protected property The database connection.
InlineBlockUsage::addUsage public function Adds a usage record. Overrides InlineBlockUsageInterface::addUsage
InlineBlockUsage::deleteUsage public function Delete the inline blocks' the usage records. Overrides InlineBlockUsageInterface::deleteUsage
InlineBlockUsage::getUnused public function Gets unused inline block IDs. Overrides InlineBlockUsageInterface::getUnused
InlineBlockUsage::getUsage public function Gets usage record for inline block by ID. Overrides InlineBlockUsageInterface::getUsage
InlineBlockUsage::removeByLayoutEntity public function Remove usage record by layout entity. Overrides InlineBlockUsageInterface::removeByLayoutEntity
InlineBlockUsage::__construct public function Creates an InlineBlockUsage object.