访问自定义模板中的 Drupal 实体字段
Accessing Drupal Entity fields in custom templates
首先让我说一下,这是我在 Drupal 中的第一个项目,我仍然很困惑,如果我的问题很愚蠢,我深表歉意。
我使用 Entity API 在 Drupal 7 中创建了一个自定义实体。自定义实体表示一个高尔夫球场。
我使用了这个教程:http://www.sitepoint.com/series/build-your-own-custom-entities-in-drupal/
然后我尝试添加一个自定义主题,为此我遵循了这个:https://www.drupal.org/node/1238606
我的回调函数如下所示:
function view_golf_course($id) {
$courses = entity_load('golf_course', array($id));
$course = $courses[$id];
drupal_set_title($course->name);
$output = entity_view('golf_course', array($course));
$output += array(
'#theme' => 'golf_course',
'#element' => $output,
'#view_mode' => 'full',
'#language' => LANGUAGE_NONE,
);
return $output;
}
这是我的 hook_theme()
:
function golf_course_theme($existing, $type, $theme, $path) {
return array(
'golf_course' => array(
'variables' => array('element' => null),
'template' => 'golf_course',
),
);
}
问题是在golf_course.tpl.php
中我只能通过这种方式访问高尔夫球场变量(在这个例子中我将访问地址):
render($element['golf_course']['The Lakes Golf Club']['address']['#markup'])
如您所见,为了访问地址,我必须使用 'The Lakes Golf Club'(这是当前显示的高尔夫球场的名称)作为键,但显然该名称将每次显示不同的高尔夫球场时都会更改,所以我的问题是:
如何在不使用高尔夫球场名称作为关键字的情况下访问高尔夫球场属性?
编辑
entity_view() (http://www.drupalcontrib.org/api/drupal/contributions!entity!entity.module/function/entity_view/7) 的文档说明如下:
Return value
The renderable array, keyed by the entity type and by entity
identifiers, for which the entity name is used if existing - see
entity_id(). If there is no information on how to view an entity,
FALSE is returned.
那么如何避免数组以实体名称为键控呢?如果它是由 id 键入的,那就没问题,因为我在范围内有 $id
变量。
对于正在寻找此问题答案的任何人:
如果查询的结果集包含多行,则 entity_view 将创建一个具有空索引的数组,如下所示:
$element['golf_course']['']
现在您可以通过访问 ['#entity']
数组来访问结果集中所有行的所有实体字段,如下所示:
$element['golf_course']['']['#entity'] // all golf courses
$element['golf_course']['']['#entity'][0] // first golf course in the result set
$element['golf_course']['']['#entity'][0]['label'] // label of first golf course
$element['golf_course']['']['#entity'][0]['address'] // address of first golf course
附带说明一下,如果您的模板是纯 PHP,您可以避免使用 entity_view()
,您将获得一个更清晰的数组(您没有 ['golf_course']['']['#entity']
部分)。
首先让我说一下,这是我在 Drupal 中的第一个项目,我仍然很困惑,如果我的问题很愚蠢,我深表歉意。
我使用 Entity API 在 Drupal 7 中创建了一个自定义实体。自定义实体表示一个高尔夫球场。
我使用了这个教程:http://www.sitepoint.com/series/build-your-own-custom-entities-in-drupal/ 然后我尝试添加一个自定义主题,为此我遵循了这个:https://www.drupal.org/node/1238606
我的回调函数如下所示:
function view_golf_course($id) {
$courses = entity_load('golf_course', array($id));
$course = $courses[$id];
drupal_set_title($course->name);
$output = entity_view('golf_course', array($course));
$output += array(
'#theme' => 'golf_course',
'#element' => $output,
'#view_mode' => 'full',
'#language' => LANGUAGE_NONE,
);
return $output;
}
这是我的 hook_theme()
:
function golf_course_theme($existing, $type, $theme, $path) {
return array(
'golf_course' => array(
'variables' => array('element' => null),
'template' => 'golf_course',
),
);
}
问题是在golf_course.tpl.php
中我只能通过这种方式访问高尔夫球场变量(在这个例子中我将访问地址):
render($element['golf_course']['The Lakes Golf Club']['address']['#markup'])
如您所见,为了访问地址,我必须使用 'The Lakes Golf Club'(这是当前显示的高尔夫球场的名称)作为键,但显然该名称将每次显示不同的高尔夫球场时都会更改,所以我的问题是:
如何在不使用高尔夫球场名称作为关键字的情况下访问高尔夫球场属性?
编辑
entity_view() (http://www.drupalcontrib.org/api/drupal/contributions!entity!entity.module/function/entity_view/7) 的文档说明如下:
Return value
The renderable array, keyed by the entity type and by entity identifiers, for which the entity name is used if existing - see entity_id(). If there is no information on how to view an entity, FALSE is returned.
那么如何避免数组以实体名称为键控呢?如果它是由 id 键入的,那就没问题,因为我在范围内有 $id
变量。
对于正在寻找此问题答案的任何人: 如果查询的结果集包含多行,则 entity_view 将创建一个具有空索引的数组,如下所示:
$element['golf_course']['']
现在您可以通过访问 ['#entity']
数组来访问结果集中所有行的所有实体字段,如下所示:
$element['golf_course']['']['#entity'] // all golf courses
$element['golf_course']['']['#entity'][0] // first golf course in the result set
$element['golf_course']['']['#entity'][0]['label'] // label of first golf course
$element['golf_course']['']['#entity'][0]['address'] // address of first golf course
附带说明一下,如果您的模板是纯 PHP,您可以避免使用 entity_view()
,您将获得一个更清晰的数组(您没有 ['golf_course']['']['#entity']
部分)。