|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2019 Google Inc. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU General Public License |
| 8 | + * version 2 as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | + * MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Drupal\apigee_api_catalog\Entity\Routing; |
| 22 | + |
| 23 | +use Drupal\apigee_api_catalog\Controller\ApiDocController; |
| 24 | +use Drupal\Core\Entity\EntityTypeInterface; |
| 25 | +use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; |
| 26 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 27 | +use Symfony\Component\Routing\Route; |
| 28 | + |
| 29 | +/** |
| 30 | + * Provides routes for API Doc entities. |
| 31 | + * |
| 32 | + * @deprecated in 2.x and is removed from 3.x. Use the node "apidoc" bundle instead. |
| 33 | + * @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider |
| 34 | + * @see \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider |
| 35 | + */ |
| 36 | +class ApiDocHtmlRouteProvider extends AdminHtmlRouteProvider { |
| 37 | + |
| 38 | + use StringTranslationTrait; |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + public function getRoutes(EntityTypeInterface $entity_type) { |
| 44 | + $collection = parent::getRoutes($entity_type); |
| 45 | + |
| 46 | + if ($settings_form_route = $this->getSettingsFormRoute($entity_type)) { |
| 47 | + $collection->add('entity.apidoc.settings', $settings_form_route); |
| 48 | + } |
| 49 | + |
| 50 | + if ($apidoc_collection_route = $collection->get('entity.apidoc.collection')) { |
| 51 | + $apidoc_collection_route->setDefault('_title', $this->t('@entity_type catalog', ['@entity_type' => $entity_type->getLabel()])->render()); |
| 52 | + } |
| 53 | + |
| 54 | + if ($reimport_spec_route = $this->getReimportSpecFormRoute($entity_type)) { |
| 55 | + $collection->add("entity.apidoc.reimport_spec_form", $reimport_spec_route); |
| 56 | + } |
| 57 | + |
| 58 | + return $collection; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the settings form route. |
| 63 | + * |
| 64 | + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
| 65 | + * The entity type. |
| 66 | + * |
| 67 | + * @return \Symfony\Component\Routing\Route|null |
| 68 | + * The generated route, if available. |
| 69 | + */ |
| 70 | + protected function getSettingsFormRoute(EntityTypeInterface $entity_type) { |
| 71 | + if (!$entity_type->getBundleEntityType()) { |
| 72 | + $route = new Route('admin/structure/apidoc'); |
| 73 | + $route |
| 74 | + ->setDefaults([ |
| 75 | + '_form' => 'Drupal\apigee_api_catalog\Entity\Form\ApiDocSettingsForm', |
| 76 | + '_title_callback' => ApiDocController::class . '::title', |
| 77 | + ]) |
| 78 | + ->setRequirement('_permission', $entity_type->getAdminPermission()) |
| 79 | + ->setOption('_admin_route', TRUE); |
| 80 | + |
| 81 | + return $route; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets the reimport-spec-form route. |
| 87 | + * |
| 88 | + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
| 89 | + * The entity type. |
| 90 | + * |
| 91 | + * @return \Symfony\Component\Routing\Route|null |
| 92 | + * The generated route, if available. |
| 93 | + */ |
| 94 | + protected function getReimportSpecFormRoute(EntityTypeInterface $entity_type) { |
| 95 | + $route = new Route($entity_type->getLinkTemplate('reimport-spec-form')); |
| 96 | + |
| 97 | + $route |
| 98 | + ->setDefaults([ |
| 99 | + '_entity_form' => "apidoc.reimport_spec", |
| 100 | + '_title' => 'Re-import API Doc OpenAPI specification', |
| 101 | + ]) |
| 102 | + ->setRequirement('_entity_access', "apidoc.reimport") |
| 103 | + ->setOption('parameters', [ |
| 104 | + 'apidoc' => ['type' => 'entity:apidoc'], |
| 105 | + ]); |
| 106 | + |
| 107 | + // Entity types with serial IDs can specify this in their route |
| 108 | + // requirements, improving the matching process. |
| 109 | + if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') { |
| 110 | + $route->setRequirement('apidoc', '\d+'); |
| 111 | + } |
| 112 | + |
| 113 | + return $route; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments