3 namespace Drupal\eu_cookie_compliance\Plugin;
5 use Drupal\Core\Plugin\PluginBase;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Drupal\Core\Config\ConfigFactoryInterface;
10 * Provides a base class for a consent storage.
12 * @see \Drupal\eu_cookie_compliance\Plugin\ConsentStorageInterface
13 * @see \Drupal\eu_cookie_compliance\Plugin\ConsentStorageManager
14 * @see \Drupal\eu_cookie_compliance\Plugin\ConsentStorageManagerInterface
17 abstract class ConsentStorageBase extends PluginBase implements ConsentStorageInterface {
22 * Subclasses should use the self::config() method, which may be overridden to
23 * address specific needs when loading config, rather than this property
24 * directly. See \Drupal\Core\Form\ConfigFormBase::config() for an example of
27 * @var \Drupal\Core\Config\ConfigFactoryInterface
29 protected $configFactory;
32 * Constructs a ConsentStorageBase object.
34 * @param array $configuration
35 * A configuration array containing information about the plugin instance.
36 * @param string $plugin_id
37 * The plugin_id for the plugin instance.
38 * @param mixed $plugin_definition
39 * The plugin implementation definition.
40 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
41 * The factory for configuration objects.
43 public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
44 parent::__construct($configuration, $plugin_id, $plugin_definition);
46 $this->configFactory = $config_factory;
52 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57 $container->get('config.factory')
64 public function label() {
65 return $this->pluginDefinition['label'];
71 public function description() {
72 return $this->pluginDefinition['description'];
78 public function getStatus() {
83 * Get the current revision of the privacy policy node.
86 * Returns the latest revision ID of the curreny privacy policy node, or
87 * FALSE if no priacy policy exists.
89 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
91 public function getCurrentPolicyNodeRevision() {
92 $config = $this->configFactory->get('eu_cookie_compliance.settings');
93 $cookie_policy_link = $config->get('popup_link');
94 $cookie_policy_drupal_path = \Drupal::service('path.alias_manager')->getPathByAlias($cookie_policy_link, \Drupal::languageManager()->getCurrentLanguage()->getId());
95 if (substr($cookie_policy_drupal_path, 0, 6) == '/node/') {
96 $node_id = explode('/', $cookie_policy_drupal_path)[2];
97 /* @var \Drupal\node\Entity\Node $node */
98 $node = \Drupal::entityTypeManager()->getStorage('node')->load($node_id);
99 return $node->getRevisionId();
109 * In addition to the parameters passed to the function, consider storing the
110 * uid, ip address, timestamp and the current revision of the cookie policy.
112 * @param string $consent_type
113 * "banner" if the consent is given to the cookie banner or the form ID when
114 * consent is given on a form.
117 * Returns TRUE when the consent has been stored successfully, FALSE on
120 abstract protected function registerConsent($consent_type);