From 26f55878cb5348161b4cf42acee6a0944b9628e2 Mon Sep 17 00:00:00 2001 From: Pedro Cambra Date: Wed, 9 Jun 2021 17:04:50 +0200 Subject: [PATCH] Map popup custom controller --- modules/ui/map/farm_ui_map.module | 11 ----- modules/ui/map/farm_ui_map.routing.yml | 9 ++++- .../farmOS.map.behaviors.asset_type_layers.js | 2 +- modules/ui/map/src/Controller/MapPopup.php | 40 +++++++++++++++++++ .../html--asset--map-popup.html.twig | 29 -------------- 5 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 modules/ui/map/src/Controller/MapPopup.php delete mode 100644 modules/ui/map/templates/html--asset--map-popup.html.twig diff --git a/modules/ui/map/farm_ui_map.module b/modules/ui/map/farm_ui_map.module index cfe9a3498..9a4b7a358 100644 --- a/modules/ui/map/farm_ui_map.module +++ b/modules/ui/map/farm_ui_map.module @@ -9,17 +9,6 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Link; -/** - * Implements hook_theme(). - */ -function farm_ui_map_theme($existing, $type, $theme, $path) { - return [ - 'html__asset__map_popup' => [ - 'base hook' => 'html', - ], - ]; -} - /** * Implements hook_farm_dashboard_panes(). */ diff --git a/modules/ui/map/farm_ui_map.routing.yml b/modules/ui/map/farm_ui_map.routing.yml index 9ce3badf9..b9ff10739 100644 --- a/modules/ui/map/farm_ui_map.routing.yml +++ b/modules/ui/map/farm_ui_map.routing.yml @@ -1,6 +1,11 @@ farm_ui_map.asset.map_popup: - path: '/asset/{asset}/map_popup' + path: '/asset/{asset}/map-popup' defaults: - _entity_view: 'asset.map_popup' + _controller: '\Drupal\farm_ui_map\Controller\MapPopup::display' requirements: _entity_access: asset.view + asset: \d+ + options: + parameters: + asset: + type: entity:asset diff --git a/modules/ui/map/js/farmOS.map.behaviors.asset_type_layers.js b/modules/ui/map/js/farmOS.map.behaviors.asset_type_layers.js index dd71093e3..af8ec6ce9 100644 --- a/modules/ui/map/js/farmOS.map.behaviors.asset_type_layers.js +++ b/modules/ui/map/js/farmOS.map.behaviors.asset_type_layers.js @@ -66,7 +66,7 @@ var assetLink = link.getAttribute('href') var description = event.target.element.querySelector('.ol-popup-description'); description.innerHTML = 'Loading asset details...'; - fetch(assetLink + '/map_popup') + fetch(assetLink + '/map-popup') .then((response) => { return response.text(); }) diff --git a/modules/ui/map/src/Controller/MapPopup.php b/modules/ui/map/src/Controller/MapPopup.php new file mode 100644 index 000000000..ab64aff3b --- /dev/null +++ b/modules/ui/map/src/Controller/MapPopup.php @@ -0,0 +1,40 @@ +entityTypeManager()->getViewBuilder($asset->getEntityTypeId()); + $build = $view_builder->view($asset, $this->displayMode); + // Render already exposes cache metadata, so no need to do it twice. + $response->setContent(render($build)); + return $response; + } + +} diff --git a/modules/ui/map/templates/html--asset--map-popup.html.twig b/modules/ui/map/templates/html--asset--map-popup.html.twig deleted file mode 100644 index c46528b2f..000000000 --- a/modules/ui/map/templates/html--asset--map-popup.html.twig +++ /dev/null @@ -1,29 +0,0 @@ -{# -/** - * @file - * Theme override for rendering the asset map_popup view mode. - * - * Only renders the page content. This includes the asset, local actions - * and any other blocks added to this route. - * - * Variables: - * - logged_in: A flag indicating if user is logged in. - * - root_path: The root path of the current page (e.g., node, admin, user). - * - node_type: The content type for the current node, if the page is a node. - * - head_title: List of text elements that make up the head_title variable. - * May contain one or more of the following: - * - title: The title of the page. - * - name: The name of the site. - * - slogan: The slogan of the site. - * - page_top: Initial rendered markup. This should be printed before 'page'. - * - page: The rendered page markup. - * - page_bottom: Closing rendered markup. This variable should be printed after - * 'page'. - * - db_offline: A flag indicating if the database is offline. - * - placeholder_token: The token for generating head, css, js and js-bottom - * placeholders. - * - * @see template_preprocess_html() - */ -#} -{{ page.content|without('farm_powered') }}