class ConsentAgreementController in General Data Protection Regulation 3.0.x
Same name and namespace in other branches
- 8.2 modules/gdpr_consent/src/Controller/ConsentAgreementController.php \Drupal\gdpr_consent\Controller\ConsentAgreementController
- 8 modules/gdpr_consent/src/Controller/ConsentAgreementController.php \Drupal\gdpr_consent\Controller\ConsentAgreementController
Class ConsentAgreementController.
Returns responses for Consent Agreement routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\gdpr_consent\Controller\ConsentAgreementController
Expanded class hierarchy of ConsentAgreementController
1 file declares its use of ConsentAgreementController
- GdprMyAgreementsBlock.php in modules/
gdpr_consent/ src/ Plugin/ Block/ GdprMyAgreementsBlock.php
File
- modules/
gdpr_consent/ src/ Controller/ ConsentAgreementController.php, line 24
Namespace
Drupal\gdpr_consent\ControllerView source
class ConsentAgreementController extends ControllerBase {
/**
* The entity field manager for metadata.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
private $entityFieldManager;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
private $dateFormatter;
/**
* The renderer service.
*
* @var \Drupal\Core\Render\Renderer
*/
private $renderer;
/**
* Constructs a ConsentAgreementController controller object.
*
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager for metadata.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
* @param \Drupal\Core\Render\Renderer $renderer
* The renderer service.
*/
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter, Renderer $renderer) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'), $container
->get('entity_type.manager'), $container
->get('date.formatter'), $container
->get('renderer'));
}
/**
* Displays a Consent Agreement revision.
*
* @param int $gdpr_consent_agreement_revision
* The Consent Agreement revision ID.
*
* @return array
* An array suitable for drupal_render().
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function revisionShow($gdpr_consent_agreement_revision) {
$gdprConsentAgreement = $this->entityTypeManager
->getStorage('gdpr_consent_agreement')
->loadRevision($gdpr_consent_agreement_revision);
$viewBuilder = $this->entityTypeManager
->getViewBuilder('gdpr_consent_agreement');
return $viewBuilder
->view($gdprConsentAgreement);
}
/**
* Page title callback for a Consent Agreement revision.
*
* @param int $gdpr_consent_agreement_revision
* The Consent Agreement revision ID.
*
* @return string
* The page title.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function revisionPageTitle($gdpr_consent_agreement_revision) {
$gdprConsentAgreement = $this->entityTypeManager
->getStorage('gdpr_consent_agreement')
->loadRevision($gdpr_consent_agreement_revision);
return $this
->t('Revision of %title from %date', [
'%title' => $gdprConsentAgreement
->label(),
'%date' => $this->dateFormatter
->format($gdprConsentAgreement
->getRevisionCreationTime()),
]);
}
/**
* Generates an overview table of older revisions of a Consent Agreement .
*
* @param \Drupal\gdpr_consent\Entity\ConsentAgreement $gdpr_consent_agreement
* A Consent Agreement object.
*
* @return array
* An array as expected by drupal_render().
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function revisionOverview(ConsentAgreement $gdpr_consent_agreement) {
$account = $this
->currentUser();
/** @var \Drupal\gdpr_consent\ConsentAgreementStorageInterface $storage */
$storage = $this->entityTypeManager
->getStorage('gdpr_consent_agreement');
$build['#title'] = $this
->t('Revisions for %title', [
'%title' => $gdpr_consent_agreement->title->value,
]);
$header = [
$this
->t('Revision'),
$this
->t('Operations'),
];
$revert_permission = $account
->hasPermission('manage gdpr agreements');
$delete_permission = $account
->hasPermission('manage gdpr agreements');
$rows = [];
$vids = $storage
->revisionIds($gdpr_consent_agreement);
$latest_revision = TRUE;
foreach (array_reverse($vids) as $vid) {
/** @var \Drupal\gdpr_consent\Entity\ConsentAgreement $revision */
$revision = $storage
->loadRevision($vid);
$username = [
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
];
// Use revision link to link to revisions that are not active.
$date = $this->dateFormatter
->format($revision
->getRevisionCreationTime(), 'short');
if ($vid !== $gdpr_consent_agreement
->getRevisionId()) {
$link = Link::fromTextAndUrl($date, new Url('entity.gdpr_consent_agreement.revision', [
'gdpr_consent_agreement' => $gdpr_consent_agreement
->id(),
'gdpr_consent_agreement_revision' => $vid,
]))
->toRenderable();
$link = $this->renderer
->renderPlain($link);
}
else {
$link = $gdpr_consent_agreement
->toLink($date)
->toString();
}
$row = [];
$column = [
'data' => [
'#type' => 'inline_template',
'#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
'#context' => [
'date' => $link,
'username' => $this->renderer
->renderPlain($username),
'message' => [
'#markup' => $revision
->getRevisionLogMessage(),
'#allowed_tags' => Xss::getHtmlTagList(),
],
],
],
];
$row[] = $column;
if ($latest_revision) {
$row[] = [
'data' => [
'#prefix' => '<em>',
'#markup' => $this
->t('Current revision'),
'#suffix' => '</em>',
],
];
foreach ($row as &$current) {
$current['class'] = [
'revision-current',
];
}
unset($current);
$latest_revision = FALSE;
}
else {
$links = [];
if ($revert_permission) {
$links['revert'] = [
'title' => $this
->t('Revert'),
'url' => Url::fromRoute('entity.gdpr_consent_agreement.revision_revert', [
'gdpr_consent_agreement' => $gdpr_consent_agreement
->id(),
'gdpr_consent_agreement_revision' => $vid,
]),
];
}
if ($delete_permission) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.gdpr_consent_agreement.revision_delete', [
'gdpr_consent_agreement' => $gdpr_consent_agreement
->id(),
'gdpr_consent_agreement_revision' => $vid,
]),
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$rows[] = $row;
}
$build['gdpr_consent_agreement_revisions_table'] = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $build;
}
/**
* Render My Agreements content.
*
* @param \Drupal\Core\Session\AccountInterface $user
* The user to show agreements for.
*
* @return array
* Renderable table of user agreements.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function myAgreements(AccountInterface $user) {
$map = $this->entityFieldManager
->getFieldMapByFieldType('gdpr_user_consent');
$agreement_storage = $this->entityTypeManager
->getStorage('gdpr_consent_agreement');
$rows = [];
foreach ($map as $entity_type => $fields) {
$field_names = array_keys($fields);
foreach ($field_names as $field_name) {
$ids = $this->entityTypeManager
->getStorage($entity_type)
->getQuery()
->condition($field_name . '.user_id', $user
->id())
->execute();
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadMultiple($ids);
foreach ($entities as $entity) {
/** @var \Drupal\gdpr_consent\Entity\ConsentAgreementInterface $agreement */
$agreement = $agreement_storage
->loadRevision($entity->{$field_name}->target_revision_id);
$link = $agreement->title->value;
if ($agreement
->access('view', $this->currentUser)) {
$link = $agreement
->toLink($agreement->title->value, 'revision')
->toString();
}
$row = [];
$row[] = [
'data' => [
'#markup' => $link,
],
];
$row[] = [
'data' => [
'#markup' => $entity->{$field_name}->date,
],
];
$rows[] = $row;
}
}
}
$header = [
'Agreement',
'Date Agreed',
];
return [
'#title' => 'Consent Agreements',
'table' => [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
'#empty' => $this
->t('You have not yet given any consent.'),
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConsentAgreementController:: |
private | property | The date formatter service. | |
ConsentAgreementController:: |
private | property | The entity field manager for metadata. | |
ConsentAgreementController:: |
private | property | The renderer service. | |
ConsentAgreementController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
ConsentAgreementController:: |
public | function | Render My Agreements content. | |
ConsentAgreementController:: |
public | function | Generates an overview table of older revisions of a Consent Agreement . | |
ConsentAgreementController:: |
public | function | Page title callback for a Consent Agreement revision. | |
ConsentAgreementController:: |
public | function | Displays a Consent Agreement revision. | |
ConsentAgreementController:: |
public | function | Constructs a ConsentAgreementController controller object. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |