我有一个名为Application的自定义实体,它有一个Entity引用字段,名为Product。
当我在一个应用程序页面上单击一个产品来查看它时,我想知道我来自应用程序页面,并且知道它的<>ID。
这将允许我在Product页面上显示Application页中的信息。
但是怎么做呢?
发布于 2018-03-12 17:13:13
这种方法适用于我:
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
// Getting the referer.
$request = \Drupal::request();
$referer = $request->headers->get('referer');
// Getting the base url.
$base_url = Request::createFromGlobals()->getSchemeAndHttpHost();
// Getting the alias or the relative path.
$alias = substr($referer, strlen($base_url));
// Getting the node.
$params = Url::fromUri("internal:" . $alias)->getRouteParameters();
$entity_type = key($params);
$node = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]);
//to use dpm you need the devel module
dpm($node->id());
dpm($node->title->value);
发布于 2022-12-20 12:43:51
$previousUrl = \Drupal::request()->server->get('HTTP_REFERER');
$fake_request = Request::create($previousUrl);
$url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri());
$route_name = $url_object->getRouteName();
$url = Url::fromRoute($route_name, $url_object->getRouteParameters());
$previousPath = $url->getInternalPath();
https://drupal.stackexchange.com/questions/257450
复制相似问题