首先,我要说这是我在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)表示以下内容:
返回值
用实体类型和实体标识符作为关键字的可渲染数组,如果存在实体名称,则使用该实体名称-请参见entity_id()。如果没有有关如何查看实体的信息,则返回FALSE。
因此,如何避免使用实体名称作为数组的键?如果它是由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']
部分)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句