View source
<?php
namespace Drupal\transaction;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
class TransactionTypeListBuilder extends ConfigEntityListBuilder {
protected $entityTypeManager;
protected $transactorManager;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityTypeManagerInterface $entity_type_manager, TransactorPluginManager $transactor_manager) {
parent::__construct($entity_type, $storage);
$this->entityTypeManager = $entity_type_manager;
$this->transactorManager = $transactor_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type, $entity_type_manager
->getStorage($entity_type
->id()), $entity_type_manager, $container
->get('plugin.manager.transaction.transactor'));
}
public function buildHeader() {
$header['label'] = $this
->t('Name');
$header['transactor'] = $this
->t('Transactor');
$header['target_entity_type'] = $this
->t('Target entity type');
$header['target_entity_bundles'] = $this
->t('Applicable bundles');
return $header + parent::buildHeader();
}
protected function getTransactorPlugin(TransactionTypeInterface $transaction_type) {
$transactor = $this->transactorManager
->getDefinition($transaction_type
->getPluginId());
return [
'data' => [
'#markup' => $transactor['title'],
],
];
}
protected function getTargetType(TransactionTypeInterface $transaction_type) {
$target_entity_type = $this->entityTypeManager
->getDefinition($transaction_type
->getTargetEntityTypeId());
return [
'data' => [
'#markup' => $target_entity_type
->getLabel(),
],
];
}
protected function getTargetBundles(TransactionTypeInterface $transaction_type) {
$bundles = $transaction_type
->getBundles();
if (empty($bundles)) {
return [
'data' => [
'#markup' => '<em>' . $this
->t('All') . '</em>',
'#allowed_tags' => [
'em',
],
],
];
}
if ($target_bundle_id = $this->entityTypeManager
->getDefinition($transaction_type
->getTargetEntityTypeId())
->getBundleEntityType()) {
$target_bundle_storage = $this->entityTypeManager
->getStorage($target_bundle_id);
foreach ($bundles as $key => $bundle) {
$bundles[$key] = $target_bundle_storage
->load($bundle)
->label();
}
}
return [
'data' => [
'#markup' => implode(', ', $bundles),
],
];
}
public function buildRow(EntityInterface $entity) {
$row = [];
$row['label'] = [
'data' => $entity
->label(),
'class' => [
'menu-label',
],
];
$row['transactor'] = $this
->getTransactorPlugin($entity);
$row['target_entity_type'] = $this
->getTargetType($entity);
$row['target_entity_bundles'] = $this
->getTargetBundles($entity);
return $row + parent::buildRow($entity);
}
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this
->t('No transaction types available. <a href=":link">Add a transaction type</a>.', [
':link' => Url::fromRoute('transaction.transaction_type_creation')
->toString(),
]);
return $build;
}
public function getDefaultOperations(EntityInterface $entity) {
return [
'operations' => [
'title' => $this
->t('Transaction operations'),
'weight' => 50,
'url' => Url::fromRoute('entity.transaction_operation.collection', [
'transaction_type' => $entity
->id(),
]),
],
] + parent::getDefaultOperations($entity);
}
}