MicrositeLogoBranding.php in Entity Reference Hierarchy 8.2
File
modules/entity_hierarchy_microsite/src/Plugin/Block/MicrositeLogoBranding.php
View source
<?php
namespace Drupal\entity_hierarchy_microsite\Plugin\Block;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity_hierarchy_microsite\Plugin\MicrositePluginTrait;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MicrositeLogoBranding extends BlockBase implements ContainerFactoryPluginInterface {
use MicrositePluginTrait {
create as parentCreate;
}
protected function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
return $this;
}
protected $entityTypeManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = self::parentCreate($container, $configuration, $plugin_id, $plugin_definition);
return $instance
->setEntityTypeManager($container
->get('entity_type.manager'));
}
public function build() {
$cache = new CacheableMetadata();
if (!($node = $this
->getContextValue('node')) || !$node instanceof NodeInterface || !($microsites = $this->childOfMicrositeLookup
->findMicrositesForNodeAndField($node, $this->configuration['field']))) {
$build = [];
if ($node) {
$cache
->addCacheableDependency($node);
}
$cache
->applyTo($build);
return $build;
}
$microsite = reset($microsites);
$cache
->addCacheableDependency($node);
$cache
->addCacheableDependency($microsite);
if ($home = $microsite
->getHome()) {
$cache
->addCacheableDependency($home);
}
$build = [
'#theme' => 'entity_hierarchy_microsite_branding',
'#site_name' => $microsite
->label(),
'#site_home' => $home ? $home
->toUrl()
->toString() : '',
'#microsite' => $microsite,
];
if ($media = $microsite
->getLogo()) {
$cache
->addCacheableDependency($media);
$build['#site_logo'] = $this->entityTypeManager
->getViewBuilder('media')
->view($media, 'entity_hierarchy_microsite');
}
$cache
->applyTo($build);
return $build;
}
public function getCacheContexts() {
return array_merge(parent::getCacheContexts(), [
'entity_hierarchy_microsite',
]);
}
}