You are here

UserConsentListBuilder.php in Data Policy 8

Namespace

Drupal\data_policy

File

src/UserConsentListBuilder.php
View source
<?php

namespace Drupal\data_policy;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a class to build a listing of User consent entities.
 *
 * @ingroup data_policy
 */
class UserConsentListBuilder extends EntityListBuilder {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Constructs a new UserConsentListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
    parent::__construct($entity_type, $storage);
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('date.formatter'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    return [
      'id' => $this
        ->t('ID'),
      'user' => $this
        ->t('User'),
      'created' => $this
        ->t('Date and time of consent'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $owner = $entity
      ->getOwner();

    /* @var \Drupal\data_policy\Entity\UserConsentInterface $entity */
    return [
      'id' => $entity
        ->id(),
      'user' => $owner instanceof UserInterface ? $owner
        ->getDisplayName() : $this
        ->t('Deleted user'),
      'created' => $this->dateFormatter
        ->format($entity
        ->getChangedTime(), 'short'),
    ];
  }

}

Classes

Namesort descending Description
UserConsentListBuilder Defines a class to build a listing of User consent entities.