Repository URL to install this package:
|
Version:
2.0.2 ▾
|
<?php
namespace Drupal\custom_forms\Routing;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Class CustomFormHtmlRouteProvider
*
* Adds additional routes for custom forms.
*
* @package Drupal\custom_forms\Routing
*/
class CustomFormHtmlRouteProvider extends AdminHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$collection = parent::getRoutes($entity_type);
$entity_type_id = $entity_type->id();
$route = (new Route('/admin/custom_forms/{custom_form}'))
->addDefaults([
'_controller' => '\Drupal\custom_forms\Controller\CustomFormViewController::view',
'_title_callback' => '\Drupal\custom_forms\Controller\CustomFormViewController::title',
])
->setRequirement('custom_form', '\d+')
->setRequirement('_entity_access', 'custom_form.admin_view')
->setOptions([
'parameters' => [
'custom_form' => ['type' => 'entity:custom_form'],
],
'_admin_route' => TRUE
]);
$collection->add('entity.custom_form.canonical', $route);
$route = (new Route('/admin/custom_forms/{custom_form}/delete'))
->addDefaults([
'_entity_form' => 'custom_form.delete',
'_title' => 'Delete',
])
->setRequirement('custom_form', '\d+')
->setRequirement('_entity_access', 'custom_form.delete')
->setOptions([
'parameters' => [
'custom_form' => ['type' => 'entity:custom_form'],
],
'_admin_route' => TRUE
]);
$collection->add('entity.custom_form.delete_form', $route);
$route = (new Route('/admin/custom_forms/{custom_form}/settings'))
->setDefaults([
'_entity_form' => 'custom_form.edit',
'_title' => 'Edit custom form',
'_title_callback' => '\Drupal\custom_forms\Controller\CustomFormController::settingsTitle'
])
->setRequirement('_entity_access', 'custom_form.update')
->setRequirement('custom_form', '\d+')
->setOptions([
'parameters' => [
'custom_form' => ['type' => 'entity:custom_form'],
],
'_admin_route' => TRUE
]);
$collection->add('entity.custom_form.edit_form', $route);
if ($fields_route = $this->getFieldsRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.fields_form", $fields_route);
}
if ($fields_route = $this->getPaymentMethodsRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.payment_methods_form", $fields_route);
}
if ($fields_route = $this->getMailRoute($entity_type)) {
$collection->add("entity.{$entity_type_id}.mail_form", $fields_route);
}
return $collection;
}
/**
* {@inheritdoc}
*/
protected function getCanonicalRoute(EntityTypeInterface $entity_type) {
if ($route = parent::getCanonicalRoute($entity_type)) {
$route->setOption('_admin_route', TRUE);
return $route;
}
}
/**
* Gets the fields route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getFieldsRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('fields-form')) {
$entity_type_id = $entity_type->id();
$route = new Route($entity_type->getLinkTemplate('fields-form'));
$route
->setDefaults([
'_entity_form' => "{$entity_type_id}.fields",
'_title_callback' => '\Drupal\custom_forms\Controller\CustomFormController::fieldsTitle',
])
->setRequirement('_entity_access', "{$entity_type_id}.update")
->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id],
]);
// Entity types with serial IDs can specify this in their route
// requirements, improving the matching process.
if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route->setRequirement($entity_type_id, '\d+');
}
return $route;
}
}
/**
* Gets the payment methods route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getPaymentMethodsRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('payment-methods-form')) {
$entity_type_id = $entity_type->id();
$route = new Route($entity_type->getLinkTemplate('payment-methods-form'));
$route
->setDefaults([
'_entity_form' => "{$entity_type_id}.payment_methods",
'_title_callback' => '\Drupal\custom_forms\Controller\CustomFormController::paymentMethodsTitle',
])
->setRequirement('_entity_access', "{$entity_type_id}.payment_methods")
->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id],
]);
// Entity types with serial IDs can specify this in their route
// requirements, improving the matching process.
if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route->setRequirement($entity_type_id, '\d+');
}
return $route;
}
}
/**
* Gets the mail route.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type.
*
* @return \Symfony\Component\Routing\Route|null
* The generated route, if available.
*/
protected function getMailRoute(EntityTypeInterface $entity_type) {
if ($entity_type->hasLinkTemplate('mail-form')) {
$entity_type_id = $entity_type->id();
$route = new Route($entity_type->getLinkTemplate('mail-form'));
$route
->setDefaults([
'_entity_form' => "{$entity_type_id}.mail",
'_title_callback' => '\Drupal\custom_forms\Controller\CustomFormController::mailTitle',
])
->setRequirement('_entity_access', "{$entity_type_id}.mail")
->setOption('parameters', [
$entity_type_id => ['type' => 'entity:' . $entity_type_id],
]);
// Entity types with serial IDs can specify this in their route
// requirements, improving the matching process.
if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
$route->setRequirement($entity_type_id, '\d+');
}
return $route;
}
}
}