【发布时间】:2015-07-08 23:29:20
【问题描述】:
首先让我说这是我在 Drupal 的第一个项目,我仍然很困惑,如果我的问题很愚蠢,我深表歉意。
我使用实体 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) 的文档如下:
返回值
可渲染数组,以实体类型和实体为键 标识符,如果存在则使用实体名称 - 请参阅 实体 ID()。如果没有关于如何查看实体的信息, 返回 FALSE。
那么如何避免数组以实体名称为键呢?如果它是由 id 键入的,那就没问题了,因为我在范围内有 $id 变量。
【问题讨论】: