You are here

public function MicrositeLogoBranding::build in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 modules/entity_hierarchy_microsite/src/Plugin/Block/MicrositeLogoBranding.php \Drupal\entity_hierarchy_microsite\Plugin\Block\MicrositeLogoBranding::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

modules/entity_hierarchy_microsite/src/Plugin/Block/MicrositeLogoBranding.php, line 63

Class

MicrositeLogoBranding
Defines a class for a branding and logo block for a microsite.

Namespace

Drupal\entity_hierarchy_microsite\Plugin\Block

Code

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;
  }

  /** @var \Drupal\entity_hierarchy_microsite\Entity\MicrositeInterface $microsite */
  $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;
}