View source
<?php
namespace Drupal\commerce_pricelist;
use Drupal\commerce_pricelist\Controller\PriceListController;
use Drupal\commerce_pricelist\Form\PriceListItemExportForm;
use Drupal\commerce_pricelist\Form\PriceListItemImportForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
class PriceListItemRouteProvider extends AdminHtmlRouteProvider {
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type
->id();
if ($export_route = $this
->getExportFormRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.export_form", $export_route);
}
if ($import_route = $this
->getImportFormRoute($entity_type)) {
$collection
->add("entity.{$entity_type_id}.import_form", $import_route);
}
return $collection;
}
protected function getAddFormRoute(EntityTypeInterface $entity_type) {
$route = parent::getAddFormRoute($entity_type);
$route
->setOption('parameters', [
'commerce_pricelist' => [
'type' => 'entity:commerce_pricelist',
],
]);
$route
->setDefault('_title_callback', '');
$route
->setDefault('_title', t('Add price')
->getUntranslatedString());
return $route;
}
protected function getEditFormRoute(EntityTypeInterface $entity_type) {
$route = parent::getEditFormRoute($entity_type);
$route
->setOption('parameters', [
'commerce_pricelist' => [
'type' => 'entity:commerce_pricelist',
],
]);
return $route;
}
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
$route = parent::getCollectionRoute($entity_type);
$route
->setOption('parameters', [
'commerce_pricelist' => [
'type' => 'entity:commerce_pricelist',
],
]);
$route
->setOption('_admin_route', TRUE);
$route
->setDefault('_title_callback', PriceListController::class . '::priceListItemsTitle');
return $route;
}
protected function getExportFormRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('export-form')) {
$route = new Route($entity_type
->getLinkTemplate('export-form'));
$route
->setDefaults([
'_form' => PriceListItemExportForm::class,
'_title' => 'Export prices',
])
->setRequirement('_permission', $entity_type
->getAdminPermission())
->setRequirement('commerce_pricelist', '\\d+')
->setOption('parameters', [
'commerce_pricelist' => [
'type' => 'entity:commerce_pricelist',
],
])
->setOption('_admin_route', TRUE);
return $route;
}
}
protected function getImportFormRoute(EntityTypeInterface $entity_type) {
if ($entity_type
->hasLinkTemplate('import-form')) {
$route = new Route($entity_type
->getLinkTemplate('import-form'));
$route
->setDefaults([
'_form' => PriceListItemImportForm::class,
'_title' => 'Import prices',
])
->setRequirement('_permission', $entity_type
->getAdminPermission())
->setRequirement('commerce_pricelist', '\\d+')
->setOption('parameters', [
'commerce_pricelist' => [
'type' => 'entity:commerce_pricelist',
],
])
->setOption('_admin_route', TRUE);
return $route;
}
}
}