You are here

CookieDeclarationBlock.php in Cookiebot - Cookie consent, Cookie monitoring and Cookie control 8

File

src/Plugin/Block/CookieDeclarationBlock.php
View source
<?php

namespace Drupal\cookiebot\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'CookieDeclarationBlock' block.
 *
 * @Block(
 *  id = "cookie_declaration_block",
 *  admin_label = @Translation("Cookie declaration block"),
 * )
 */
class CookieDeclarationBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new Block object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param string $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    $config = $this->configFactory
      ->get('cookiebot.settings');
    $cbid = $config
      ->get('cookiebot_cbid');
    if (empty($cbid)) {
      return $build;
    }
    $build['cookie_declaration_block'] = [
      '#theme' => 'cookiebot_declaration',
      '#cookiebot_src' => 'https://consent.cookiebot.com/' . $cbid . '/cd.js',
    ];
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return Cache::mergeTags(parent::getCacheTags(), [
      'cookiebot:cbid',
    ]);
  }

}

Classes

Namesort descending Description
CookieDeclarationBlock Provides a 'CookieDeclarationBlock' block.