View source
<?php
namespace Drupal\gdpr_consent\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\gdpr_consent\ConsentUserResolver\ConsentUserResolverPluginManager;
use Drupal\gdpr_consent\Entity\ConsentAgreement;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConsentWidget extends WidgetBase implements ContainerFactoryPluginInterface {
protected $gdprConsentResolverManager;
protected $currentUser;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container
->get('plugin.manager.gdpr_consent_resolver'), $container
->get('current_user'));
}
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ConsentUserResolverPluginManager $gdprConsentResolverManager, AccountInterface $currentUser) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->gdprConsentResolverManager = $gdprConsentResolverManager;
$this->currentUser = $currentUser;
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
if (array_key_exists(0, $form['#parents']) && $form['#parents'][0] == 'default_value_input') {
return [];
}
$canEditAnyonesConsent = $this->currentUser
->hasPermission('grant gdpr any consent');
$canEditOwnConsent = $this->currentUser
->hasPermission('grant gdpr own consent');
$consentingUser = $this
->getConsentingUser($items);
$agreementId = $items
->getFieldDefinition()
->getSetting('target_id');
if ($agreementId === '') {
return [];
}
if (!$this->currentUser
->isAnonymous()) {
if (!$canEditAnyonesConsent && $consentingUser
->id() !== $this->currentUser
->id()) {
return [];
}
if (!$canEditOwnConsent && $consentingUser
->id() === $this->currentUser
->id()) {
return [];
}
}
$agreement = ConsentAgreement::load($agreementId);
if (NULL === $agreement) {
return [];
}
$item = $items[$delta];
$element['target_id'] = [
'#type' => 'hidden',
'#default_value' => $agreementId,
];
$element['target_revision_id'] = [
'#type' => 'hidden',
'#default_value' => isset($item->target_revision_id) ? $item->target_revision_id : $agreement
->getRevisionId(),
];
$element['agreed'] = [
'#type' => 'checkbox',
'#title' => $agreement
->get('description')->value,
'#description' => $agreement
->get('long_description')->value,
'#required' => $items
->getFieldDefinition()
->isRequired(),
'#default_value' => isset($item->agreed) && (bool) $item->agreed === TRUE,
'#attributes' => [
'class' => [
'gdpr_consent_agreement',
],
],
'#attached' => [
'library' => [
'gdpr_consent/gdpr_consent_display',
],
],
];
if (!$agreement
->requiresExplicitAcceptance()) {
$element['agreed']['#title'] = '';
$element['agreed']['#type'] = 'item';
$element['agreed']['#markup'] = '<span class="gdpr_consent_implicit">' . $agreement
->get('description')->value . '</span>';
$element['agreed']['#default_value'] = TRUE;
}
if ($canEditAnyonesConsent) {
$element['notes'] = [
'#type' => 'textarea',
'#title' => 'GDPR Consent Notes',
'#required' => FALSE,
'#default_value' => isset($item->notes) ? $item->notes : '',
];
}
return $element;
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as &$value) {
if (!isset($value['user_id_accepted'])) {
$value['user_id_accepted'] = $this->currentUser
->id();
}
if (!isset($value['date'])) {
$value['date'] = \date('Y-m-d H:i:s');
}
}
unset($value);
return $values;
}
private function getConsentingUser(FieldItemListInterface $items) {
$definition = $items
->getFieldDefinition();
$resolver = $this->gdprConsentResolverManager
->getForEntityType($definition
->getTargetEntityTypeId(), $definition
->getTargetBundle());
return $resolver
->resolve($items
->getEntity());
}
}