Accessing Drupal Entity fields in custom templates -


first of let me first project in drupal , still confused, apologize if question stupid.

i created custom entity in drupal 7 using entity api. custom entity represents golf course.

i used tutorial: http://www.sitepoint.com/series/build-your-own-custom-entities-in-drupal/ tried add custom theme, followed this: https://www.drupal.org/node/1238606

my callback function looks this:

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; } 

and hook_theme():

function golf_course_theme($existing, $type, $theme, $path) {   return array(     'golf_course' => array(       'variables' => array('element' => null),       'template' => 'golf_course',     ),   ); } 

the problem in golf_course.tpl.php can access golf course variables way (in example access address):

render($element['golf_course']['the lakes golf club']['address']['#markup']) 

as can see, in order access address have use 'the lakes golf club', (which name of displayed golf course), key, name going change every time display different golf course, question is:

how can access golf course attributes without having use golf course name key?

edit

the docs entity_view() (http://www.drupalcontrib.org/api/drupal/contributions!entity!entity.module/function/entity_view/7) following:

return value

the renderable array, keyed entity type , entity identifiers, entity name used if existing - see entity_id(). if there no information on how view entity, false returned.

so how avoid array keyed name of entity? if keyed id ok because have $id variable in scope.

for looking answer question: if result set of query contains multiple rows, entity_view create array empty index so:

$element['golf_course'][''] 

and can access entity fields of rows in result set accessing ['#entity'] array so:

$element['golf_course']['']['#entity'] // golf courses $element['golf_course']['']['#entity'][0] // first golf course in result set $element['golf_course']['']['#entity'][0]['label'] // label of first golf course $element['golf_course']['']['#entity'][0]['address'] // address of first golf course 

on side note, if templates in pure php can avoid using entity_view(), cleaner array (you don't have ['golf_course']['']['#entity'] part).


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -