Ii am theming a view and am finding that the content fields are coming out mis-named, and their names change depending on their output order in the Fields area. Here are the fields I'm working with as reported by Theme Information:

  • Field Content: Store Hours (field_store_hours) (ID: field_store_hours_value)
  • Field Content: Phone Number (field_phone_number) (ID: field_phone_number_value)
  • Field Content: Credit Cards Accepted (field_credit_cards) (ID: field_credit_cards_value)
  • Field Content: Long Description (field_long_description) (ID: field_long_description_value)
  • Field Content: Suite Number (field_unit_number) (ID: field_unit_number_value)

When I print_r($vars['row']), I get this -- notice the odd way the node_data fields are named:

stdClass Object
(
    [nid] => 18
    [node_data_field_store_hours_field_store_hours_value] => 
    [node_type] => store
    [node_vid] => 306
    [node_data_field_store_hours_field_phone_number_value] => 
    [node_data_field_store_hours_field_long_description_value] => (data)    
    [node_data_field_store_hours_field_unit_number_value] => 80
)

If I reorder the fields to put the unit number first, then those node_data fields come out like this instead:

stdClass Object
(
    [nid] => 18
    [node_data_field_unit_number_field_unit_number_value] => 80
    [node_type] => store
    [node_vid] => 306
    [node_data_field_unit_number_field_store_hours_value] => 
    [node_data_field_unit_number_field_phone_number_value] => 
    [node_data_field_unit_number_field_long_description_value] =>  (data)     
)

I've searched for a similar issue in the Views and CCK issue queues but haven't found anything. I've disabled all other contrib modules and don't see any change. What else can I try?

Comments

merlinofchaos’s picture

Status: Active » Closed (works as designed)

The names used in the actual result object are not guaranteed, and never have been, and never will be. In order to prevent collisions, this is a very important distinction.

If you want to know what the actual variable in the result object will be, you can get that information from the handler:

$view->field[$field_id]->field_alias

That is guaranteed to always be correct. $field_id is guaranteed never to change once you've added the field to a view.

markabur’s picture

thanks, i can certainly set up my view and then not touch the field order when i'm theming... though i'm a bit confused by your explanation.

what's the difference between "names used in the actual result object" (which are not guaranteed), and the "actual variable in the result object"?

is the field naming i just noticed different than it used to be? i have some sites i built a few months ago and the field names weren't smooshed together like they are now.

note that i am seeing field_alias match the field names i'm getting from print_r($vars['row']):

[field_alias] => node_data_field_unit_number_field_long_description_value
[aliases] => Array
  (
    [field_long_description_value] => node_data_field_unit_number_field_long_description_value
    [nid] => nid
    [type] => node_type
    [vid] => node_vid
  )

in a preprocessor function, is it possible to get at my long description field using 'field_long_description_value' instead of 'node_data_field_unit_number_field_long_description_value'?

thanks,

-mark

markabur’s picture

any idea why this works:

print_r($vars['row']->{$vars['view']->field['field_store_hours_value']->field_alias});

but the same thing with this field:

print_r($vars['row']->{$vars['view']->field['field_store_long_description_value']->field_alias})

results in this error?

[11-Aug-2009 13:45:15] PHP Fatal error: Cannot access empty property in [...]/template.php on line 92

'field_store_long_description_value' is the correct id, and 'node_data_field_unit_number_field_long_description_value' seems to be an alias for it, but $vars['view']->field['field_store_long_description_value']->field_alias is null...

i'd like to avoid hard-coding the alias, but it appears to be the only way to get the content if this particular field:

print_r($vars['row']->'node_data_field_unit_number_field_long_description_value')

merlinofchaos’s picture

field_long_description_value

vs

field_store_long_description_value

I think the second one is in error.

markabur’s picture

doh! thanks.

markabur’s picture

ok, sorry, one more related issue. one of my cck fields is a multiple-select.

if i have the "group multiple values" option turned off, i get the expected alias:

print_r($vars['view']->field['field_credit_cards_value']->field_alias);
result: node_data_field_credit_cards_field_credit_cards_value

but if i have the "group multiple values" option turned on -- which is what i need -- then look what happens:

print_r($vars['view']->field['field_credit_cards_value']->field_alias);
result: node_vid

obviously then when i get the value $vars['row']->node_vid i don't see the credit cards, i see the vid.

you can actually see the problem in my OP -- the credit cards field does not appear in the result object. if i turn group multiple values off, then the credit cards field *does* appear in the result object.

merlinofchaos’s picture

When using group multiple, the values are retrieved through a secondary query. They should be stored somewhere on the object itself at that point, and you'll have to reference them probably with a nid or vid.