Map popup custom controller

This commit is contained in:
Pedro Cambra 2021-06-09 17:04:50 +02:00 committed by Michael Stenta
parent 1da9527d3d
commit 26f55878cb
5 changed files with 48 additions and 43 deletions

View File

@ -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().
*/

View File

@ -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

View File

@ -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();
})

View File

@ -0,0 +1,40 @@
<?php
namespace Drupal\farm_ui_map\Controller;
use Drupal\asset\Entity\AssetInterface;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
/**
* Returns response for the map popup.
*/
class MapPopup extends ControllerBase {
/**
* Set the display mode used.
*
* @var string
*/
protected string $displayMode = 'map_popup';
/**
* Display an asset entity standalone in the map popup.
*
* @param \Drupal\asset\Entity\AssetInterface $asset
* The asset entity.
*
* @return \Symfony\Component\HttpFoundation\Response
* The response to display in the popup.
*/
public function display(AssetInterface $asset) : Response {
$response = new Response();
$view_builder = $this->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;
}
}

View File

@ -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') }}