You are here

class ConsentAgreementController in General Data Protection Regulation 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/gdpr_consent/src/Controller/ConsentAgreementController.php \Drupal\gdpr_consent\Controller\ConsentAgreementController
  2. 8 modules/gdpr_consent/src/Controller/ConsentAgreementController.php \Drupal\gdpr_consent\Controller\ConsentAgreementController

Class ConsentAgreementController.

Returns responses for Consent Agreement routes.

Hierarchy

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\Controller
View 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

Namesort descending Modifiers Type Description Overrides
ConsentAgreementController::$dateFormatter private property The date formatter service.
ConsentAgreementController::$entityFieldManager private property The entity field manager for metadata.
ConsentAgreementController::$renderer private property The renderer service.
ConsentAgreementController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
ConsentAgreementController::myAgreements public function Render My Agreements content.
ConsentAgreementController::revisionOverview public function Generates an overview table of older revisions of a Consent Agreement .
ConsentAgreementController::revisionPageTitle public function Page title callback for a Consent Agreement revision.
ConsentAgreementController::revisionShow public function Displays a Consent Agreement revision.
ConsentAgreementController::__construct public function Constructs a ConsentAgreementController controller object.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.