You are here

BadgeListBuilder.php in User Badges 8

Namespace

Drupal\user_badges

File

src/BadgeListBuilder.php
View source
<?php

/**
 * @file
 * Contains \Drupal\user_badges\BadgeListBuilder.
 */
namespace Drupal\user_badges;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a class to build a listing of Badge entities.
 *
 * @ingroup user_badges
 */
class BadgeListBuilder extends EntityListBuilder {

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new BlockListBuilder 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\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, RendererInterface $renderer) {
    parent::__construct($entity_type, $storage);
    $this->renderer = $renderer;
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['id'] = $this
      ->t('Badge ID');
    $header['name'] = $this
      ->t('Name');
    $header['badge_image'] = $this
      ->t('Badge Image');
    $header['weight'] = $this
      ->t('Weight');
    $header['roles']['data'] = $this
      ->t('Role');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /* @var $entity \Drupal\user_badges\Entity\Badge */
    $row['id'] = $entity
      ->id();
    $row['name'] = Link::fromTextAndUrl($entity
      ->label(), new Url('entity.badge.edit_form', array(
      'badge' => $entity
        ->id(),
    )));
    if ($entity->image->entity) {
      $image = array(
        '#theme' => 'image_style',
        '#style_name' => 'thumbnail',
        '#uri' => $entity->image->entity
          ->getFileUri(),
        '#title' => $entity
          ->label(),
      );
      $row['badge_image'] = $this->renderer
        ->render($image);
    }
    else {
      $row['badge_image'] = $this
        ->t('No Image');
    }
    $row['weight'] = $entity
      ->getBadgeWeight();
    $users_roles = $entity
      ->getBadgeRoleIds();
    $row['roles']['data'] = array(
      '#theme' => 'item_list',
      '#items' => $users_roles,
    );
    return $row + parent::buildRow($entity);
  }

}

Classes

Namesort descending Description
BadgeListBuilder Defines a class to build a listing of Badge entities.