View source
<?php
namespace Drupal\commerce_invoice\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InvoiceItemTypesAdminController extends ControllerBase {
use StringTranslationTrait;
protected $entityTypeBundleInfo;
protected $entityTypeManager;
public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'));
}
public function bundlePage($bundle = NULL) {
$entity_bundle_info = $this->entityTypeBundleInfo
->getBundleInfo('commerce_invoice_item');
return [
'#markup' => $this
->t('The @bundle-label bundle has no settings.', [
'@bundle-label' => $entity_bundle_info[$bundle]['label'],
]),
];
}
public function adminPage() {
$entity_type = $this->entityTypeManager
->getDefinition('commerce_invoice_item');
$entity_bundle_info = $this->entityTypeBundleInfo
->getBundleInfo('commerce_invoice_item');
$build = [];
$build['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Name'),
$this
->t('Description'),
$this
->t('Operations'),
],
'#rows' => [],
'#empty' => $this
->t('There are no @label yet.', [
'@label' => $entity_type
->getPluralLabel(),
]),
];
foreach ($entity_bundle_info as $bundle_name => $bundle_info) {
$build['table']['#rows'][$bundle_name] = [
'name' => [
'data' => $bundle_info['label'],
],
'description' => [
'data' => $bundle_info['description'] ?? '',
],
'operations' => [
'data' => $this
->buildOperations($bundle_name),
],
];
}
return $build;
}
protected function buildOperations($bundle_name) {
$operations = [
'manage-fields' => [
'title' => t('Manage fields'),
'weight' => 15,
'url' => Url::fromRoute("entity.commerce_invoice_item.field_ui_fields", [
'bundle' => $bundle_name,
]),
],
'manage-form-display' => [
'title' => t('Manage form display'),
'weight' => 20,
'url' => Url::fromRoute("entity.entity_form_display.commerce_invoice_item.default", [
'bundle' => $bundle_name,
]),
],
'manage-display' => [
'title' => t('Manage display'),
'weight' => 25,
'url' => Url::fromRoute("entity.entity_view_display.commerce_invoice_item.default", [
'bundle' => $bundle_name,
]),
],
];
return [
'#type' => 'operations',
'#links' => $operations,
];
}
}