我想使用字段中设置的分类术语值来增强页面的标题。
所以标题看起来像这样:
为了修改标题,我覆盖了
field--node--title--[content-type].html.twig
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,通过元素对象请求访问节点本身和其他字段:
element['#object'].get('field_my_field').get(0)
Run Code Online (Sandbox Code Playgroud)
获取第一个元素。
element['#object'].get('field_my_field').get(0)['target_id']
Run Code Online (Sandbox Code Playgroud)
返回分类术语的 tid。但
element['#object'].get('field_my_field').get(0)['name']
Run Code Online (Sandbox Code Playgroud)
返回一个空元素。
如何检索字段值?我无法想象这需要 php 代码。
更新: 我找到了一个方法,但看起来有点复杂:
由于#object 中未解析分类术语,因此我必须添加一个 hook_preprocess:field 函数来准备具有必要数据的变量:
function myTheme_preprocess_field(&$variables, $hook){
if ($variables['field_name'] == 'title'){
$variables['my_field'] = Term::load($variables['element']['#object']->get('my_field')->get(0)->getValue()['target_id']);
}
Run Code Online (Sandbox Code Playgroud)
现在可以在 twig 中访问该字段,如下所示:
{{ my_field.name.getValue()[0]['value'] }}
Run Code Online (Sandbox Code Playgroud)
对我来说,这似乎是一项艰巨的工作,因此非常感谢更直接的方法。
我想从 core/modules/search/src/Controller\SearchController.php 的第 119 行删除搜索页面中添加的搜索结果字符串。
if (count($results)) {
$build['search_results_title'] = array(
'#markup' => '<h2>' . $this->t('Search results') . '</h2>',
);
}
Run Code Online (Sandbox Code Playgroud)
我可以使用搜索表单上的 preprocess_form 函数和搜索结果上的 preprocess_search_result 更改上面的搜索表单和结果列表。
是否有我错过的预处理功能,或者我可以使用自定义模板文件吗?
我正在一个 Drupal 8 站点上工作,其中我有一个自定义模块/块,可以从另一台服务器获取内容并将其注入到 div 中。当用户单击注入内容中的链接(例如阅读更多内容)时,我需要提供内容(完整的 html 页面)(从服务器获取),而无需模板的页眉和页脚。
我的问题是如何在没有模板页眉和页脚的情况下提供页面?
感谢您。
我在树枝模板中有一个图像网址,例如“ public://path/to/image.png”。我想获取“ sites / default / files / path / to / image.png”以在模板中使用它。
我认为它应该与这样的事情一起工作:
{{ url ('public://path/to/image.png') }}
Run Code Online (Sandbox Code Playgroud)
要么
{{ file_uri_scheme ('public://path/to/image.png') }}
Run Code Online (Sandbox Code Playgroud)
但是我得到的只是错误。我知道第二个是PHP函数,但是有没有机会用TWIG做到这一点?
我从buildForm函数创建了一个自定义表单.在这种形式,我想添加一个图像字段.
我可以通过这段代码来做到这一点:
$form['main']['image'] = array(
'#type' => 'text_format',
'#title' => t('image'),
'#default_value' => array(10),
);
Run Code Online (Sandbox Code Playgroud)
我可以上传和删除表单中的图片.但是,当我上传图片时,我没有预览.我的意思是,当我通过Drupal UI创建内容时.我可以添加一个预配置的"图像"字段.当我通过这个"图像"字段上传图像时,我预览了图像.
在这里,当我以编程方式创建field元素时,我上传她时没有预览图像.当我通过"图像"字段上传她时,如何使用api Drupal进行图像预览?
我试图在Drupal 8的结果页面中显示搜索结果计数,我想显示如下内容:23 results for search_word.
我正在使用默认的Drupal搜索,并item-list--search-results.html.twig作为模板文件显示结果,但我找不到可用变量中的搜索结果计数,任何想法怎么能找到这个值?
我正在制作一个新主题,但在加载CSS时遇到问题。尽管我认为对Drupal的文档是正确的,但它不起作用。
我在/ themes / custom / noadin中创建了一个文件夹,并添加了信息和库文件
noadin.info.yml:
name: noadin
description: 'My new theme'
type: theme
core: 8.x
libraries:
- noadin/global-styling
- noadin/global-scripts
Run Code Online (Sandbox Code Playgroud)
并创建了noadin.libraries.yml:
global-styling:
version: VERSION
css:
theme:
css/style.css: {}
global-scripts:
version: VERSION
js:
js/script.js: {}
Run Code Online (Sandbox Code Playgroud)
样式表位于/ themes / custom / noadin / css
谁能告诉我我的误解是什么?
ps Drupal采用了正确的主题。
我正在通过开发自定义表单小部件模块迈出第一步,以了解Drupal 8的工作原理。我的目标是显示参考节点的图像字段值,而不是在单选按钮列表(在核心中可用)中显示其节点标题。这将使网站管理员在为节点选择背景图像时选择图片而不是文本。
这是使用Drupal 8内置的“复选框/单选按钮”小部件而无需自定义工作的表单的外观:
这是我希望我的自定义小部件出现(至少开始)的Photoshop样机:
到目前为止,我已经能够创建一个扩展“复选框/单选按钮”小部件的启动模块,并参考“ 开发人员示例”模块和遍历核心。这至少帮助我更好地了解了Drupal 8的模块结构。
模块结构:
modules
custom
back_image_widget
back_image_widget.info.yml
back_image_widget.module
src
Plugin
Field
Field Widget
BackImageWidget.php
Run Code Online (Sandbox Code Playgroud)
back_image_widget.info.yml:
name: Background Image Entity Widget
type: module
description: Used to list Background Image entities as images instead of text labels in the Text Message content type form.
package: Custom
core: 8.x
Run Code Online (Sandbox Code Playgroud)
back_image_widget.module:
<?php
/**
* @file
* Used to list Background Image entities as images instead of text labels in the Text Message content type form.
*/
Run Code Online (Sandbox Code Playgroud)
BackImageWidget.php: …
在Drupal 7中
if ($_SESSION['messages']['status'][0] == t('Registration successful. You are now logged in.')) {
unset($_SESSION['messages']['status']);
}
Run Code Online (Sandbox Code Playgroud)
我如何在drupal 8中实现这一点?请帮忙
我已成功使用预处理页面挂钩,例如:
function mytheme_preprocess_page__node_front(&$variables) {
...
}
Run Code Online (Sandbox Code Playgroud)
和
function mytheme_preprocess_page__node_12(&$variables) {
...
}
Run Code Online (Sandbox Code Playgroud)
它与分别名为page - front.html.twig和page - 12.html.twig的自定义模板相关联.
我正在尝试为名为Video的内容类型实现相同的挂钩和模板配对.我的理解是,我的示例是针对特定页面的自定义模板,而我的目标是整个内容类型的自定义模板,但我得到了一个名为node的自定义模板 - video.html.twig可用作所有视频页面的模板.但是,当我尝试根据此模板名称编写一个钩子时:
function mytheme_preprocess_node__video(&$variables) {
...
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.我认为我要么不能定义这样的钩子,要么我只是错误地命名它.我找到了一些与此相关的线程,例如这似乎意味着我需要为所有节点定义一个钩子,然后编写一个if语句来分别处理每个类型.所以.......
最后的问题:我可以为整个内容类型定义一个钩子,如果是这样,我做错了什么?