You are here

abstract class ConsentStorageBase in EU Cookie Compliance (GDPR Compliance) 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/ConsentStorageBase.php \Drupal\eu_cookie_compliance\Plugin\ConsentStorageBase

Provides a base class for a consent storage.

Hierarchy

Expanded class hierarchy of ConsentStorageBase

See also

\Drupal\eu_cookie_compliance\Plugin\ConsentStorageInterface

\Drupal\eu_cookie_compliance\Plugin\ConsentStorageManager

\Drupal\eu_cookie_compliance\Plugin\ConsentStorageManagerInterface

Plugin API

1 file declares its use of ConsentStorageBase
BasicConsentStorage.php in src/Plugin/ConsentStorage/BasicConsentStorage.php

File

src/Plugin/ConsentStorageBase.php, line 17

Namespace

Drupal\eu_cookie_compliance\Plugin
View source
abstract class ConsentStorageBase extends PluginBase implements ConsentStorageInterface {

  /**
   * The config factory.
   *
   * Subclasses should use the self::config() method, which may be overridden to
   * address specific needs when loading config, rather than this property
   * directly. See \Drupal\Core\Form\ConfigFormBase::config() for an example of
   * this.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a ConsentStorageBase 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 mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  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 label() {
    return $this->pluginDefinition['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function description() {
    return $this->pluginDefinition['description'];
  }

  /**
   * Get status.
   *
   * @return bool
   *   Current status.
   */
  public function getStatus() {
    return TRUE;
  }

  /**
   * Get the current revision of the privacy policy node.
   *
   * @return bool|int
   *   Returns the latest revision ID of the curreny privacy policy node, or
   *   FALSE if no priacy policy exists.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getCurrentPolicyNodeRevision() {
    $config = $this->configFactory
      ->get('eu_cookie_compliance.settings');
    $cookie_policy_link = $config
      ->get('popup_link');
    $cookie_policy_drupal_path = \Drupal::service('path_alias.manager')
      ->getPathByAlias($cookie_policy_link, \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId());
    if (substr($cookie_policy_drupal_path, 0, 6) === '/node/') {
      $node_id = explode('/', $cookie_policy_drupal_path)[2];

      /** @var \Drupal\node\Entity\Node $node */
      $node = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->load($node_id);

      // Ensure the node has been loaded before accessing any properties.
      if (!$node) {
        return FALSE;
      }
      return $node
        ->getRevisionId();
    }
    return FALSE;
  }

  /**
   * Register consent.
   *
   * In addition to the parameters passed to the function, consider storing the
   * uid, ip address, timestamp and the current revision of the cookie policy.
   *
   * @param string $consent_type
   *   "banner" if the consent is given to the cookie banner or the form ID when
   *   consent is given on a form.
   *
   * @return bool
   *   Returns TRUE when the consent has been stored successfully, FALSE on
   *   error.
   */
  public abstract function registerConsent($consent_type);

}

Members

Namesort descending Modifiers Type Description Overrides
ConsentStorageBase::$configFactory protected property The config factory.
ConsentStorageBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ConsentStorageBase::description public function Returns the consent storage description. Overrides ConsentStorageInterface::description
ConsentStorageBase::getCurrentPolicyNodeRevision public function Get the current revision of the privacy policy node.
ConsentStorageBase::getStatus public function Get status.
ConsentStorageBase::label public function Returns the consent storage label. Overrides ConsentStorageInterface::label
ConsentStorageBase::registerConsent abstract public function Register consent. 1
ConsentStorageBase::__construct public function Constructs a ConsentStorageBase object. Overrides PluginBase::__construct 1
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.