You are here

class SubscriptionListBuilder in Mailing List 8

Defines a class to build a listing of subscriptions.

Hierarchy

Expanded class hierarchy of SubscriptionListBuilder

See also

\Drupal\mailing_list\Entity\Subscription

File

src/SubscriptionListBuilder.php, line 20

Namespace

Drupal\mailing_list
View source
class SubscriptionListBuilder extends EntityListBuilder {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Contruct a new SubscriptionListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The action storage.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder object.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, AccountInterface $current_user, FormBuilderInterface $form_builder, RequestStack $request_stack) {
    parent::__construct($entity_type, $storage);
    $this->currentUser = $current_user;
    $this->formBuilder = $form_builder;
    $this->requestStack = $request_stack;
  }

  /**
   * {@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('current_user'), $container
      ->get('form_builder'), $container
      ->get('request_stack'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header = [
      'title' => [
        'data' => $this
          ->t('Title'),
        'class' => [
          RESPONSIVE_PRIORITY_MEDIUM,
        ],
      ],
      'list' => $this
        ->t('Mailing list'),
      'email' => $this
        ->t('Email'),
    ];
    if ($this->currentUser
      ->hasPermission('administer mailing list subscriptions')) {
      $header += [
        'author' => [
          'data' => $this
            ->t('Author'),
          'class' => [
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
        'status' => $this
          ->t('Status'),
        'changed' => [
          'data' => $this
            ->t('Updated'),
          'class' => [
            RESPONSIVE_PRIORITY_LOW,
          ],
        ],
      ];
    }
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $row = [];

    /** @var \Drupal\mailing_list\SubscriptionInterface $entity */
    if (!$entity
      ->access('view')) {
      return $row;
    }
    $uri = $entity
      ->urlInfo();
    $options = $uri
      ->getOptions();
    $langcode = $entity
      ->language()
      ->getId();
    $languages = \Drupal::languageManager()
      ->getLanguages();
    $options += $langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? [
      'language' => $languages[$langcode],
    ] : [];
    $uri
      ->setOptions($options);
    $row['title']['data'] = [
      '#type' => 'link',
      '#title' => $entity
        ->label(),
      '#url' => $uri,
    ];
    $row['list'] = $entity
      ->getList()
      ->label();
    $row['email']['data'] = [
      '#markup' => $entity
        ->getEmail(),
    ];
    if ($this->currentUser
      ->hasPermission('administer mailing list subscriptions')) {
      $row['author']['data'] = [
        '#theme' => 'username',
        '#account' => $entity
          ->getOwner(),
      ];
      $row['status'] = $entity
        ->isActive() ? $this
        ->t('Active') : $this
        ->t('Inactive');
      $row['changed'] = \Drupal::service('date.formatter')
        ->format($entity
        ->getChangedTime(), 'short');
    }
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();

    // Anonymous users with no session started has no subscription access. We
    // return the anonymous subscription access form.
    if (!count($build['table']['#rows']) && $this->currentUser
      ->isAnonymous()) {
      return $this->formBuilder
        ->getForm('\\Drupal\\mailing_list\\Form\\AnonymousSubscriptionAccessForm');
    }
    $build['table']['#empty'] = $this
      ->t('No subscriptions found.');

    // Prevent search engines from indexing this subscriptions list and pages.
    $build['#attached']['html_head'][] = [
      [
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'robots',
          'content' => 'noindex,nofollow',
        ],
      ],
      'mailing_list',
    ];
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityIds() {
    $query = $this
      ->getStorage()
      ->getQuery()
      ->sort($this->entityType
      ->getKey('id'));

    // Filter by email address for anonymous users when come from a hashed
    // access link.
    if ($this->currentUser
      ->isAnonymous() && ($sid = $this->requestStack
      ->getMasterRequest()
      ->get('mailing_list_subscription')) && ($subscription = $this
      ->getStorage()
      ->load($sid))) {
      $query
        ->condition('email', $subscription
        ->getEmail());
    }

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $query
        ->pager($this->limit);
    }
    return $query
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities. 3
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getDefaultOperations protected function Gets this list's default operations. 2
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
EntityListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilderInterface::load 4
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
SubscriptionListBuilder::$currentUser protected property The current user.
SubscriptionListBuilder::$formBuilder protected property The form builder.
SubscriptionListBuilder::$requestStack protected property The request stack.
SubscriptionListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
SubscriptionListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides EntityListBuilder::buildRow
SubscriptionListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
SubscriptionListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. Overrides EntityListBuilder::getEntityIds
SubscriptionListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
SubscriptionListBuilder::__construct public function Contruct a new SubscriptionListBuilder object. Overrides EntityListBuilder::__construct