You are here

class FixedToContentMappingHandler in Fixed Block Content 8

Fixed block to block content mapping entity handler.

Hierarchy

Expanded class hierarchy of FixedToContentMappingHandler

See also

\Drupal\fixed_block_content\FixedToContentMappingHandlerInterface

File

src/FixedToContentMappingHandler.php, line 17

Namespace

Drupal\fixed_block_content
View source
class FixedToContentMappingHandler implements FixedToContentMappingHandlerInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * The memory cache.
   *
   * @var \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface
   */
  protected $memoryCache;

  /**
   * MappingHandler constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
   *   The memory cache.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, MemoryCacheInterface $memory_cache) {
    $this->entityTypeManager = $entity_type_manager;
    $this->database = $database;
    $this->memoryCache = $memory_cache;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('database'), $container
      ->get('entity.memory_cache'));
  }

  /**
   * {@inheritdoc}
   */
  public function setBlockContent(FixedBlockContentInterface $fixed_block, BlockContentInterface $block_content) {

    // Bundle validation.
    if ($fixed_block
      ->getBlockContentBundle() != $block_content
      ->bundle()) {
      throw new \InvalidArgumentException(sprintf('The type of the given block "%s" does not match the configured block type "%s".', $block_content
        ->bundle(), $fixed_block
        ->getBlockContentBundle()));
    }
    if ($block_content
      ->isNew()) {

      // Save the new block to get an ID, it is required to mapping.
      $block_content
        ->save();

      // New blocks (not read from the storage) must be added to the entity
      // memory cache to maintain the same object across the execution.
      $cid = 'values:block_content:' . $block_content
        ->id();
      $this->memoryCache
        ->set($cid, $block_content);
    }
    if ($current_block = $this
      ->getBlockContent($fixed_block
      ->id())) {

      // If linking the same block, no action needed.
      if ($current_block
        ->id() == $block_content
        ->id()) {
        return;
      }

      // Replacing existing block content with another, we need first to
      // release the old one.
      $this
        ->releaseBlockContent($fixed_block);
    }

    // Add fixed to content record in the mapping DB.
    $this->database
      ->insert('fixed_block_content')
      ->fields([
      'fbid' => $fixed_block
        ->id(),
      'bid' => $block_content
        ->id(),
    ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getBlockContent($fixed_block_id) {

    /** @var \Drupal\Core\Database\StatementInterface $bids */
    $bids = $this->database
      ->select('fixed_block_content', 'fbc')
      ->fields('fbc', [
      'bid',
    ])
      ->range(0, 1)
      ->condition('fbc.fbid', $fixed_block_id)
      ->execute();
    if ($bid = $bids
      ->fetchField()) {
      $block_content_storage = $this->entityTypeManager
        ->getStorage('block_content');
      return $block_content_storage
        ->load($bid);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function releaseBlockContent(FixedBlockContentInterface $fixed_block) {

    // In the case of protected blocks, the block content must be deleted to
    // prevent orphaned block content.
    if ($fixed_block
      ->isProtected() && ($block_content = $fixed_block
      ->getBlockContent(FALSE))) {
      $block_content
        ->delete();
    }

    // Delete the fixed block link with the block content.
    $this->database
      ->delete('fixed_block_content')
      ->condition('fbid', $fixed_block
      ->id())
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getFixedBlock(BlockContentInterface $block_content) {

    // New block content cannot be linked to any fixed block.
    if ($block_content
      ->isNew()) {
      return NULL;
    }

    /** @var \Drupal\Core\Database\StatementInterface $bids */
    $bids = $this->database
      ->select('fixed_block_content', 'fbc')
      ->fields('fbc', [
      'fbid',
    ])
      ->range(0, 1)
      ->condition('fbc.bid', $block_content
      ->id())
      ->execute();
    if ($fbid = $bids
      ->fetchField()) {
      $fixed_block_content_storage = $this->entityTypeManager
        ->getStorage('fixed_block_content');
      return $fixed_block_content_storage
        ->load($fbid);
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FixedToContentMappingHandler::$database protected property The database connection.
FixedToContentMappingHandler::$entityTypeManager protected property The entity type manager.
FixedToContentMappingHandler::$memoryCache protected property The memory cache.
FixedToContentMappingHandler::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
FixedToContentMappingHandler::getBlockContent public function Gets the block content linked with a fixed block. Overrides FixedToContentMappingHandlerInterface::getBlockContent
FixedToContentMappingHandler::getFixedBlock public function Gets the fixed block linked to the given block content. Overrides FixedToContentMappingHandlerInterface::getFixedBlock
FixedToContentMappingHandler::releaseBlockContent public function Breaks the link between a fixed block and a block content. Overrides FixedToContentMappingHandlerInterface::releaseBlockContent
FixedToContentMappingHandler::setBlockContent public function Links a fixed block to a block content. Overrides FixedToContentMappingHandlerInterface::setBlockContent
FixedToContentMappingHandler::__construct public function MappingHandler constructor.