Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将节点中的字段添加到page.tpl.php,drupal 7_Drupal_Drupal 7_Drupal Theming_Drupal Fields_Drupal Entities - Fatal编程技术网

将节点中的字段添加到page.tpl.php,drupal 7

将节点中的字段添加到page.tpl.php,drupal 7,drupal,drupal-7,drupal-theming,drupal-fields,drupal-entities,Drupal,Drupal 7,Drupal Theming,Drupal Fields,Drupal Entities,在Drupal 7的page.tpl.php中创建一个子主题,并需要从字段中提取值(纯文本),例如从自定义内容类型中提取值(纯文本),而其他内容则不在正常范围内 <!-- Adding $title as normal--> <?php print render($title_prefix); ?> <?php if (!empty($title)): ?> <h1><?php print $t

在Drupal 7的
page.tpl.php
中创建一个子主题,并需要从
字段中提取值(纯文本),例如从自定义内容类型中提取值(纯文本),而其他内容则不在正常范围内

<!-- Adding $title as normal-->
    <?php print render($title_prefix); ?>
        <?php if (!empty($title)): ?>
            <h1><?php print $title; ?></h1>
        <?php endif; ?>
    <?php print render($title_suffix); ?>

<!-- THE ISSUE: Adding field_EXAMPLE -->
    <h2> <?php print render($field_EXAMPLE;);?> </h2>
    ...
<!-- Where the rest of the content loads by default -->
    <div><?php print render($page['content']); ?></div>
还是这个

$node = node_load($nid);
$node->field_EXAMPLE[$node->language][0]['value'];
我是否将其放入
page.tpl.php
? 我试过了,但没有掷骰子-新手

这里是var_dump(get_defined_vars())

$node=node\u加载($nid);
$example_value=$node->field_示例[$node->language][0]['value'];
或者

$node=node\u加载($nid);
$values=字段\获取\项($node',$node,'EXAMPLE');
如果($values!=FALSE){
$val=$values[0]['value'];
}
否则{
//没有结果
}

您可以将代码直接放入页面模板中,但也可以将其放入页面预处理挂钩中,设置模板变量并使用页面模板中的该变量:

这是一种更干净的方式,但两者都应该有效

另外,如果前端没有立即看到更改,请尝试删除Drupal的缓存

要获取节点id,请尝试:

global $node;
$nid = $node->nid;
$node = node_load($nid);
...
如果这不起作用,试试这个:

    if ($node = menu_get_object()) {
      // Get the nid
      $nid = $node->nid;
      $node = node_load($nid);
      ...
    }
或者这样:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);
  $node = node_load($nid);
  ...
}

您可以使用预处理将值添加到传递给模板的$variables中

在template.php中:

MYTHEMENAME_preprocess_page(&variable){
 $values = current(field_get_items('node', $variable['node'],'field_machine_name'));
 $variable['myvar'] = $values['value'];
}
在template.tpl.php中

echo $myvar; // contains your value

假设您创建了一个名为
field\u thestring
的字段,要在
主题
之外的位置为内容类型
文章
的页面呈现该字段

第一步。复制主题的page.tpl.php。并将其重命名为
page--article.tpl.php

第二步。在
page.var.php

function THEME_preprocess_page(&$variables) {

// To activate page--article.tpl.php 
if (isset($variables['node']->type)) {
 $nodetype = $variables['node']->type;
 $variables['theme_hook_suggestions'][] = 'page__' . $nodetype;    
}

// Prevent errors on other pages
if ($node = menu_get_object()) {

if ( !empty($node) && $node->type == 'article') {
$fields = field_get_items('node', $node, 'field_thestring');
$index = 0;
$output = field_view_value('node', $node, 'field_thestring', $fields[$index]);
$variables['thestring'] = $output;
}
else{
$variables['thestring'] = 'Angry Russian: How this error?';
}
}
}
第三步。在
页面--article.tpl.php
添加

最初,我想要求所有内容类型都有另一个字段,因为所有内容类型都有一个标题。确定这不是进一步发展的好主意


只是一个提示。。。有一个函数
get_defined_vars()
,可用于查看tpl文件中有哪些变量可用。因此,如果您启用了devel模块,您可以将
dpm(get_defined_vars())
放入tpl文件并重新加载页面,以获得所有变量的漂亮列表。我怀疑是否需要加载节点,因为它可能已经在变量中。请参阅我对上面的
get_defined_vars()
函数的评论。根据BVSK的问题,需要在page.tpl中提取其他节点字段的值。所以加载节点是必要的。@VivekKumar我在
page.tpl.php
中的主题预处理页面()中得到一个错误`Undefined variable:nid',用于此行
$node=node\u加载($nid)EntityFormedException在节点类型的实体上缺少bundle属性
在实体_extract_id()
第7907行
…\ROOTFOLDER\includes\common.inc)
@BVSK中,您必须传递要呈现的节点id,或者在文章和自定义内容类型之间必须存在某种关系才能获取节点id。它不喜欢
$nid=$node->nid。注意:尝试在
THEME\u preprocess\u page()
Ok中获取非对象的属性,然后尝试其他选项。首先获取节点id,然后根据需要自行加载节点。
if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);
  $node = node_load($nid);
  ...
}
MYTHEMENAME_preprocess_page(&variable){
 $values = current(field_get_items('node', $variable['node'],'field_machine_name'));
 $variable['myvar'] = $values['value'];
}
echo $myvar; // contains your value
function THEME_preprocess_page(&$variables) {

// To activate page--article.tpl.php 
if (isset($variables['node']->type)) {
 $nodetype = $variables['node']->type;
 $variables['theme_hook_suggestions'][] = 'page__' . $nodetype;    
}

// Prevent errors on other pages
if ($node = menu_get_object()) {

if ( !empty($node) && $node->type == 'article') {
$fields = field_get_items('node', $node, 'field_thestring');
$index = 0;
$output = field_view_value('node', $node, 'field_thestring', $fields[$index]);
$variables['thestring'] = $output;
}
else{
$variables['thestring'] = 'Angry Russian: How this error?';
}
}
}