Skip to main content
 Web开发网 » 站长学院 » 浏览器插件

Drupal8 在twig模板里调用field

2021年11月03日6130百度已收录

Drupal 8的config pages模块可以添加一个设置页面,放入自定义的一些数据,比如图片、文本等,只要是存在的field都可以。然后通过config pages的block可以方便地在前台模板里调用。如果需要在twig模板里直接调用单个field,可以通过添加预处理实现

1.在你的 主题.theme文件里添加预处理

/*** Implements template_preprocess_page().*/function template_preprocess_page(&$variables) {

$cp = \Drupal\config_pages\Entity\ConfigPages::config('set');

$field_value = $cp->get('field_tel')->value;

$variables['field_tel'] = $field_value;}

2. 在需要输出field的页面如page.html.twig里写上{{ field_tel }}

Drupal8 在twig模板里调用field  Drupal 第1张

评论列表暂无评论
发表评论
微信