cookies_addons-1.0.3/modules/cookies_addons_fields/cookies_addons_fields.module
modules/cookies_addons_fields/cookies_addons_fields.module
<?php
/**
* @file
* Primary module hooks for Cookies Addons Fields module.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_field_formatter_third_party_settings_form().
*/
function cookies_addons_fields_field_formatter_third_party_settings_form(FormatterInterface $plugin, FieldDefinitionInterface $field_definition, $view_mode, array $form, FormStateInterface $form_state) {
// Prepare the form element for the cookie service selection.
$element = [];
$serviceOptions = ['_none' => t('None')];
$services = \Drupal::service('entity_type.manager')->getStorage('cookies_service')->loadByProperties(['status' => 1]);
foreach ($services as $service) {
$serviceOptions[$service->id()] = $service->label();
}
// Add cookie consent settings to all field formatters.
$element['cookies_service'] = [
'#type' => 'select',
'#title' => t('Cookies service'),
'#description' => t('Enter the machine name of the cookies service that is required for this field. If no options are available, please check @link.',
[
'@link' => Link::fromTextAndUrl(t('services list'), Url::fromRoute('entity.cookies_service.collection', [], ['attributes' => ['target' => '_blank']]))->toString(),
]
),
'#default_value' => $plugin->getThirdPartySetting('cookies_addons_fields', 'cookies_service'),
'#options' => $serviceOptions,
];
return $element;
}
/**
* Implements hook_field_formatter_settings_summary_alter().
*/
function cookies_addons_fields_field_formatter_settings_summary_alter(array &$summary, array $context) {
$formatter = $context['formatter'];
// Add a summary message when a cookie service is set.
if ($cookies_service = $formatter->getThirdPartySetting('cookies_addons_fields', 'cookies_service')) {
$summary[] = t('Cookies service: @service', ['@service' => $cookies_service]);
}
}
/**
* Implements hook_preprocess_field().
*/
function cookies_addons_fields_preprocess_field(&$variables) {
$entity = $variables['element']['#object'];
$field_name = $variables['element']['#field_name'];
// Check if this field has a cookie service restriction.
$cookies_service = $variables['element']['#third_party_settings']['cookies_addons_fields']['cookies_service'] ?? NULL;
if (empty($cookies_service) || $cookies_service === '_none') {
return;
}
// Skip if this is a POST request.
if (\Drupal::request()->getMethod() === 'POST') {
return;
}
// Get the cookie service entity.
$cookiesServices = \Drupal::service('entity_type.manager')
->getStorage('cookies_service')
->loadByProperties([
'id' => $cookies_service,
'status' => 1,
]);
$cookiesService = reset($cookiesServices);
if ($cookiesService) {
$serviceLabel = $cookiesService->label();
}
else {
$serviceLabel = $cookies_service;
}
// Generate a unique ID for this field.
$field_id = $entity->getEntityTypeId() . '-' . $entity->id() . '-' . $field_name;
$viewMode = $variables['element']['#view_mode'] ?? 'default';
foreach ($variables['items'] as $key => $item) {
// Clear the content of each field item.
$variables['items'][$key]['content'] = [];
}
// Replace the field content with a placeholder.
$variables['attributes']['class'][] = 'cookies-addons-fields-placeholder';
$variables['attributes']['data-cookies-service'] = $cookies_service;
$variables['attributes']['data-service-name'] = $serviceLabel;
$variables['attributes']['data-field-id'] = $field_id;
$variables['attributes']['data-view-mode'] = $viewMode;
$variables['attributes']['id'] = $field_id . '-content';
$variables['#attached']['library'][] = 'cookies_addons_fields/cookies-addons-fields';
}
