【问题标题】:Add a field from a node into page.tpl.php, drupal 7将节点中的字段添加到 page.tpl.php,drupal 7
【发布时间】:2016-10-24 19:49:33
【问题描述】:

在 Drupal 7 的 page.tpl.php 中创建一个子主题,并需要从自定义内容类型中提取 field_EXAMPLE 的值(纯文本),而不是其余内容的正常位置。

<!-- 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>

field_get_items 会起作用吗?

function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
  $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  return isset($entity->{$field_EXAMPLE}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}

还是这个?

$node = node_load($nid);
$node->field_EXAMPLE[$node->language][0]['value'];

我要把这个放在page.tpl.php 吗? 试过了,但没有骰子。 -新手

这里是 var_dump(get_defined_vars());

              ["field_thestring"]=>
              array(1) {
                ["und"]=>
                array(1) {
                  [0]=>
                  array(3) {
                    ["value"]=>
                    string(44) "This is a string of text please refer to me "
                    ["format"]=>
                    NULL
                    ["safe_value"]=>
                    string(44) "This is a string of text please refer to me "
                  }
                }
              }

【问题讨论】:

  • 只是一个提示...有一个函数get_defined_vars() 可以用来查看tpl 文件中有哪些变量可用。因此,如果您启用了 devel 模块,您可以将dpm(get_defined_vars()) 放入 tpl 文件并重新加载页面以获得所有变量的漂亮列表。

标签: drupal drupal-7 drupal-theming drupal-fields drupal-entities


【解决方案1】:

假设您创建了一个名为field_thestring 的字段,您希望在页面内容呈现位置之外的THEME 之外的位置为内容类型article 的页面呈现该字段。

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

第 2 步。在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?';
}
}
}

第 3 步。在page--article.tpl.php 添加&lt;?php print render($thestring); ?&gt;

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

Source

【讨论】:

    【解决方案2】:
    $node = node_load($nid);
    $example_value = $node->field_EXAMPLE[$node->language][0]['value'];
    
    <h2> <?php print $example_value;?> </h2>
    

    或者,

    $node = node_load($nid);
    $values = field_get_items('node', $node, 'EXAMPLE');
    if ($values != FALSE) {
      $val = $values[0]['value'];
    }
    else {
      // no result
    }
    <h2> <?php print $example_value;?> </h2>
    

    【讨论】:

    • 我怀疑需要加载节点,因为它可能已经在变量中。请参阅我对上面get_defined_vars() 函数的评论。
    • 根据BVSK的问题,需要拉取page.tpl中其他节点字段的值。所以加载节点是必要的。
    • @VivekKumar 我收到一个错误`Undefined variable: nid in THEME_preprocess_page()` in page.tpl.php for this line $node = node_load($nid);
    • EntityMalformedException 在类型节点的实体上缺少捆绑属性。 in entity_extract_ids()在线7907 ....\ROOTFOLDER\includes\common.inc)
    • @BVSK 您必须传递要呈现的节点 ID,否则文章和自定义内容类型之间必须存在某种关系才能获取节点 ID。
    【解决方案3】:

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

    https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x

    这是一种更清洁的方式,但两者都应该工作。

    另外,如果您没有立即在前端看到更改,请尝试删除 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);
      ...
    }
    

    【讨论】:

    • 它不喜欢 $nid = $node-&gt;nid; 。注意:试图在THEME_preprocess_page()中获取非对象的属性
    • 好的,然后尝试其他选项。首先获取节点 ID,然后根据需要自行加载节点。
    【解决方案4】:

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

    https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x

    在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多