You are here

WishlistItemListBuilder.php in Commerce Wishlist 8.3

File

src/WishlistItemListBuilder.php
View source
<?php

namespace Drupal\commerce_wishlist;

use Drupal\commerce_price\Calculator;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines the list builder for wishlist items.
 */
class WishlistItemListBuilder extends EntityListBuilder {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new WishlistItemListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity storage.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
    $this->entityTypeManager = $entity_type_manager;
    $this->routeMatch = $route_match;
    parent::__construct($entity_type, $entity_type_manager
      ->getStorage('commerce_wishlist_item'));
  }

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

  /**
   * {@inheritdoc}
   */
  protected function getEntityIds() {
    $wishlist = $this->routeMatch
      ->getParameter('commerce_wishlist');
    $query = $this
      ->getStorage()
      ->getQuery()
      ->condition('wishlist_id', $wishlist
      ->id())
      ->sort('purchasable_entity')
      ->sort('quantity');

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

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['id'] = $this
      ->t('Id');
    $header['item'] = $this
      ->t('Item');
    $header['quantity'] = $this
      ->t('Quantity');
    return $header + parent::buildHeader();
  }

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

    /* @var \Drupal\commerce_wishlist\Entity\WishlistItemInterface $entity */
    $row['id'] = $entity
      ->id();
    $row['item'] = $entity
      ->getTitle();
    $row['quantity'] = Calculator::trim($entity
      ->getQuantity());
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();
    $build['table']['#empty'] = $this
      ->t('There are no wishlist items yet.');
    return $build;
  }

}

Classes

Namesort descending Description
WishlistItemListBuilder Defines the list builder for wishlist items.