如何通过其URL别名获取节点?


37

我知道我可以通过该node_load()函数的标题加载节点。是否可以通过其URL别名加载节点?

Answers:


56

您要调查menu_get_objectdrupal_lookup_path

这样的事情会做你想要的:

$alias = "some/path/to/a/page";
$path = drupal_lookup_path("source", $alias);
$node = menu_get_object("node", 1, $path);

另外,要动态获取URL别名,请执行以下操作:$ alias = $ _SERVER ['REQUEST_URI']; $ alias = substr($ alias,1); //从字符串开头删除“ /”
Eugene Pavlov

@EugenePavlov- $alias = drupal_get_path_alias();应该是。
leymannx

0

对于希望在Drupal 8中找到实现方法的人,这里是相应的问答

如何从页面别名获取节点的图片?

use \Drupal\Core\Url;

$alias = \Drupal::service('path.alias_manager')->getPathByAlias('/etapes-de-la-vie');
$params = Url::fromUri("internal:" . $alias)->getRouteParameters();
$entity_type = key($params);
$node = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.