我有一个模板的页面内容类型,工作完美。但是,当我尝试编辑或删除该内容类型的节点时,它会阻止标准模板。
所以node/130/edit?或node/130/delete?不能工作,因为我没有得到正确的页面。
我尝试了strpos($_SERVER['REQUEST_URI'])版本,但它不起作用,或者像下面这样的版本,当路径为node/130/edit?时,只给出“原始”的Drupal模板,这样第二个变量就可以通过了。
function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
// Add content type suggestions.
if (($node = \Drupal::request()->attributes->get('node')) &&
(strpos($_SERVER['REQUEST_URI'], "delete") || strpos($_SERVER['REQUEST_URI'], "edit") === false)
) {
array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
}
}array_splice提示来自https://www.drupal.org/forum/support/theme-development/2015-07-02/how-to-add-in-drupal-8-a-custom-suggestion-page-template#comment-10684366
有什么帮助吗?
发布于 2019-02-16 04:16:09
一种稍微不同的方法:为节点类型的规范路由添加一个自定义主题建议(S)。
这样,您就不必干预其位置可能发生变化的路径参数,例如,在站点开发周期的稍后阶段使用子路径别名时,也不必扩展对第三方模块添加的其他路由的检查,例如node/{nid}/translate、node/{nid}/revisions、node/{nid}/devel等。
虽然我没有测试,但是你可以这样做:
/**
* Implements hook_theme_suggestions_HOOK_alter() for page.html.twig.
*/
function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if (
$route_name = \Drupal::routeMatch()->getRouteName()
&& $route_name == 'entity.node.canonical'
&& $node = \Drupal::request()->attributes->get('node')
) {
$suggestions[] = 'page__node__' . $node->getType() . '__canonical';
}
}然后,您可以将页面模板重命名为page--node--my-type--canonical.html.twig,并且应该只为节点类型my_type在其规范(视图)路由上获取该模板。
发布于 2019-02-15 16:22:40
您可以使用path_args而不是$_SERVER['REQUEST_URI']。类似于:
function mythem_theme_suggestions_page_alter(array &$suggestions, array $variables) {
// Add content type suggestions.
// Get current path.
$current_path = \Drupal::service('path.current')->getPath();
// explode args.
$path_args = explode('/', $current_path);
if (($node = \Drupal::request()->attributes->get('node')) && isset($path_args[3]) && ($path_args[3] == 'edit' || $path_args[3] == 'delete')) {
array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
}
}有关如何获取url args的更多信息,请查看https://www.drupal.org/node/2274705。
https://drupal.stackexchange.com/questions/276578
复制相似问题