FixedToContentMappingHandler.php in Fixed Block Content 8
File
src/FixedToContentMappingHandler.php
View source
<?php
namespace Drupal\fixed_block_content;
use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FixedToContentMappingHandler implements FixedToContentMappingHandlerInterface {
protected $entityTypeManager;
protected $database;
protected $memoryCache;
public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $database, MemoryCacheInterface $memory_cache) {
$this->entityTypeManager = $entity_type_manager;
$this->database = $database;
$this->memoryCache = $memory_cache;
}
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'));
}
public function setBlockContent(FixedBlockContentInterface $fixed_block, BlockContentInterface $block_content) {
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()) {
$block_content
->save();
$cid = 'values:block_content:' . $block_content
->id();
$this->memoryCache
->set($cid, $block_content);
}
if ($current_block = $this
->getBlockContent($fixed_block
->id())) {
if ($current_block
->id() == $block_content
->id()) {
return;
}
$this
->releaseBlockContent($fixed_block);
}
$this->database
->insert('fixed_block_content')
->fields([
'fbid' => $fixed_block
->id(),
'bid' => $block_content
->id(),
])
->execute();
}
public function getBlockContent($fixed_block_id) {
$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;
}
public function releaseBlockContent(FixedBlockContentInterface $fixed_block) {
if ($fixed_block
->isProtected() && ($block_content = $fixed_block
->getBlockContent(FALSE))) {
$block_content
->delete();
}
$this->database
->delete('fixed_block_content')
->condition('fbid', $fixed_block
->id())
->execute();
}
public function getFixedBlock(BlockContentInterface $block_content) {
if ($block_content
->isNew()) {
return NULL;
}
$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;
}
}