You are here

ContentModerationNotificationsListBuilder.php in Content Moderation Notifications 8

File

src/Controller/ContentModerationNotificationsListBuilder.php
View source
<?php

namespace Drupal\content_moderation_notifications\Controller;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;

/**
 * Provides a listing of content_moderation_notification entities.
 *
 * List Controllers provide a list of entities in a tabular form. The base
 * class provides most of the rendering logic for us. The key functions
 * we need to override are buildHeader() and buildRow(). These control what
 * columns are displayed in the table, and how each row is displayed
 * respectively.
 *
 * Drupal locates the list controller by looking for the "list" entry under
 * "controllers" in our entity type's annotation. We define the path on which
 * the list may be accessed in our module's *.routing.yml file. The key entry
 * to look for is "_entity_list". In *.routing.yml, "_entity_list" specifies
 * an entity type ID. When a user navigates to the URL for that router item,
 * Drupal loads the annotation for that entity type. It looks for the "list"
 * entry under "controllers" for the class to load.
 *
 * @package Drupal\content_moderation_notifications\Controller
 *
 * @ingroup content_moderation_notifications
 */
class ContentModerationNotificationsListBuilder extends ConfigEntityListBuilder {

  /**
   * Builds the header row for the entity listing.
   *
   * @return array
   *   A render array structure of header strings.
   *
   * @see Drupal\Core\Entity\EntityListController::render()
   */
  public function buildHeader() {
    $header['transition'] = $this
      ->t('Transitions');
    $header['bundles'] = $this
      ->t('Enabled Bundles');
    $header['roles'] = $this
      ->t('Email Roles');
    $header['author'] = $this
      ->t('Email Author');
    $header['emails'] = $this
      ->t('Adhoc Emails');
    return $header + parent::buildHeader();
  }

  /**
   * Builds a row for an entity in the entity listing.
   *
   * @param EntityInterface $entity
   *   The entity for which to build the row.
   *
   * @return array
   *   A render array of the table row for displaying the entity.
   *
   * @see Drupal\Core\Entity\EntityListController::render()
   */
  public function buildRow(EntityInterface $entity) {
    $row = array(
      'transition' => '',
    );

    // Array of transitions used in each row.
    $transition_strings = array();

    // Loop through the saved transitions.
    if ($entity->transitions) {
      $transitions = array_keys(array_filter($entity->transitions));
    }
    foreach ($transitions as $transition) {
      $transition = \Drupal::service('entity_type.manager')
        ->getStorage('moderation_state_transition')
        ->load($transition);
      $from = \Drupal::service('entity_type.manager')
        ->getStorage('moderation_state')
        ->load($transition
        ->getFromState())
        ->label();
      $to = \Drupal::service('entity_type.manager')
        ->getStorage('moderation_state')
        ->load($transition
        ->getToState())
        ->label();
      $transition_strings[] = $from . ' to ' . $to;
    }
    $row['transition'] = implode(', ', $transition_strings);
    $bundles = array();
    if ($entity->bundles) {
      $bundles = array_keys(array_filter($entity->bundles));
    }
    $roles = array();
    if ($entity->roles) {
      $roles = array_keys(array_filter($entity->roles));
    }
    $row['bundles'] = implode(', ', $bundles);
    $row['roles'] = implode(', ', $roles);
    $row['author'] = $entity->author ? 'Yes' : 'No';
    $row['emails'] = $entity->emails;
    return $row + parent::buildRow($entity);
  }

  /**
   * Adds some descriptive text to our entity list.
   *
   * Typically, there's no need to override render(). You may wish to do so,
   * however, if you want to add markup before or after the table.
   *
   * @return array
   *   Renderable array.
   */
  public function render() {
    $build['description'] = array(
      '#markup' => $this
        ->t(""),
    );
    $build[] = parent::render();
    return $build;
  }

}

Classes

Namesort descending Description
ContentModerationNotificationsListBuilder Provides a listing of content_moderation_notification entities.