You are here

class PaymentListBuilder in Payment 8.2

Lists payment entities.

Hierarchy

Expanded class hierarchy of PaymentListBuilder

1 file declares its use of PaymentListBuilder
PaymentListBuilderTest.php in tests/src/Unit/Entity/Payment/PaymentListBuilderTest.php

File

src/Entity/Payment/PaymentListBuilder.php, line 15

Namespace

Drupal\payment\Entity\Payment
View source
class PaymentListBuilder extends EntityListBuilder implements PaymentListBuilderInterface {

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $currencyStorage;

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

  /**
   * The ID of the owner to restrict payments by.
   *
   * @var int|null
   *   The owner ID or null to allow payments of all owners.
   */
  protected $ownerId;

  /**
   * The redirect destination.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $redirectDestination;

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {

    /** @var static $list_builder */
    $list_builder = parent::createInstance($container, $entity_type);
    $list_builder->currencyStorage = $container
      ->get('entity_type.manager')
      ->getStorage('currency');
    $list_builder->dateFormatter = $container
      ->get('date.formatter');
    $list_builder->moduleHandler = $container
      ->get('module_handler');
    $list_builder->redirectDestination = $container
      ->get('redirect.destination');
    $list_builder->stringTranslation = $container
      ->get('string_translation');
    return $list_builder;
  }

  /**
   * Sets the date formatter.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter.
   */
  public function setDateFormatter(DateFormatterInterface $date_formatter) {
    $this->dateFormatter = $date_formatter;
  }

  /**
   * Sets the currency storage.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $currency_storage
   *   The currency storage.
   */
  public function setCurrencyStorage(EntityStorageInterface $currency_storage) {
    $this->currencyStorage = $currency_storage;
  }

  /**
   * {@inheritdoc}
   */
  public function restrictByOwnerId($owner_id) {
    $this->ownerId = $owner_id;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntityIds() {
    $query = $this
      ->getStorage()
      ->getQuery();
    $header = $this
      ->buildHeader();
    $query
      ->tableSort($header);
    if ($this->ownerId) {
      $query
        ->condition('owner', $this->ownerId);
    }
    return $query
      ->pager($this->limit)
      ->execute();
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['updated'] = [
      'data' => $this
        ->t('Last updated'),
      'field' => 'changed',
      'sort' => 'DESC',
      'specifier' => 'changed',
    ];
    $header['status'] = [
      'data' => $this
        ->t('Status'),
    ];
    $header['amount'] = [
      'data' => $this
        ->t('Amount'),
    ];
    $header['payment_method'] = array(
      'data' => $this
        ->t('Payment method'),
      'class' => array(
        RESPONSIVE_PRIORITY_LOW,
      ),
    );
    $header['owner'] = array(
      'data' => $this
        ->t('Payer'),
      'class' => array(
        RESPONSIVE_PRIORITY_MEDIUM,
      ),
    );
    $header['operations'] = [
      'data' => $this
        ->t('Operations'),
    ];
    return $header;
  }

  /**
   * {@inheritdnoc}
   */
  public function buildRow(EntityInterface $payment) {

    /** @var \Drupal\payment\Entity\PaymentInterface $payment */
    $row['data']['updated'] = $this->dateFormatter
      ->format($payment
      ->getChangedTime());
    $status_definition = $payment
      ->getPaymentStatus()
      ->getPluginDefinition();
    $row['data']['status'] = $status_definition['label'];

    /** @var \Drupal\currency\Entity\CurrencyInterface $currency */
    $currency = $this->currencyStorage
      ->load($payment
      ->getCurrencyCode());
    if (!$currency) {
      $currency = $this->currencyStorage
        ->load('XXX');
    }
    $row['data']['amount'] = $currency
      ->formatAmount($payment
      ->getAmount());
    $row['data']['payment_method'] = $payment
      ->getPaymentMethod() ? $payment
      ->getPaymentMethod()
      ->getPluginDefinition()['label'] : $this
      ->t('Unavailable');
    $row['data']['owner']['data'] = array(
      '#theme' => 'username',
      '#account' => $payment
        ->getOwner(),
    );
    $operations = $this
      ->buildOperations($payment);
    $row['data']['operations']['data'] = $operations;
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultOperations(EntityInterface $entity) {
    $destination = $this->redirectDestination
      ->get();
    $operations = parent::getDefaultOperations($entity);
    foreach ($operations as &$operation) {
      $operation['query']['destination'] = $destination;
    }
    if ($entity
      ->access('view')) {
      $operations['view'] = array(
        'title' => $this
          ->t('View'),
        'weight' => -10,
        'url' => $entity
          ->toUrl(),
      );
    }
    if ($entity
      ->access('update_status')) {
      $operations['update_status'] = array(
        'title' => $this
          ->t('Update status'),
        'attributes' => array(
          'data-accepts' => 'application/vnd.drupal-modal',
        ),
        'query' => array(
          'destination' => $destination,
        ),
        'url' => $entity
          ->toUrl('update-status-form'),
      );
    }
    if ($entity
      ->access('capture')) {
      $operations['capture'] = array(
        'title' => $this
          ->t('Capture'),
        'attributes' => array(
          'data-accepts' => 'application/vnd.drupal-modal',
        ),
        'query' => array(
          'destination' => $destination,
        ),
        'url' => $entity
          ->toUrl('capture-form'),
      );
    }
    if ($entity
      ->access('refund')) {
      $operations['refund'] = array(
        'title' => $this
          ->t('Refund'),
        'attributes' => array(
          'data-accepts' => 'application/vnd.drupal-modal',
        ),
        'query' => array(
          'destination' => $destination,
        ),
        'url' => $entity
          ->toUrl('refund-form'),
      );
    }
    if ($entity
      ->access('complete')) {
      $operations['complete'] = array(
        'title' => $this
          ->t('Complete'),
        'url' => $entity
          ->toUrl('complete'),
      );
    }
    return $operations;
  }

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

    // @todo Remove this when https://drupal.org/node/2253257 is fixed.
    $build['#attached'] = array(
      'library' => array(
        'core/drupal.ajax',
      ),
    );
    return $build;
  }

}

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::ensureDestination protected function Ensures that a destination is present on the given URL.
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
EntityListBuilder::__construct public function Constructs a new EntityListBuilder object. 16
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PaymentListBuilder::$currencyStorage protected property The currency storage.
PaymentListBuilder::$dateFormatter protected property The date formatter.
PaymentListBuilder::$ownerId protected property The ID of the owner to restrict payments by.
PaymentListBuilder::$redirectDestination protected property The redirect destination. Overrides RedirectDestinationTrait::$redirectDestination
PaymentListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides EntityListBuilder::buildHeader
PaymentListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. Overrides EntityListBuilder::buildOperations
PaymentListBuilder::buildRow public function {@inheritdnoc} Overrides EntityListBuilder::buildRow
PaymentListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
PaymentListBuilder::getDefaultOperations protected function Gets this list's default operations. Overrides EntityListBuilder::getDefaultOperations
PaymentListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. Overrides EntityListBuilder::getEntityIds
PaymentListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render
PaymentListBuilder::restrictByOwnerId public function Restricts the displayed payments by owner ID. Overrides PaymentListBuilderInterface::restrictByOwnerId
PaymentListBuilder::setCurrencyStorage public function Sets the currency storage.
PaymentListBuilder::setDateFormatter public function Sets the date formatter.
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.