I set up a template file to display my view with some custom layout. Upon removing a field from views my field alias names changed and broke my template file.
The problem can be reproduced in Drupal 6.9 with CCK and Views installed on MAMP. I have also had the same issue with a LAMP stack.
To produce the problem:
1. Perform a clean install of Drupal 6.9
2. Install CCK-6.x-2.1 and Views-6.x-2.2
3. Other than the core modules, enable CCK Content, CCK Text, Views, and Views UI.
4. Create a CCK content type with 2 text fields called "test_1" and "test_2".
5. Create a few nodes with this content type.
6. Create a new View and add a Page display with the fields "test_1", "test_2" and save view.
7. Copy "views-view.tpl.php" from the views module folder and add to the Garland theme.
8. Add print_r($view); to the template file to get all data exposed by the $view object.
9. print($view->result[0]->node_data_field_test_1_field_test_2_value); displays the value of the first node and the field "test_2".
10. Remove "test_1" field from the view.
11. print($view->result[0]->node_data_field_test_2_field_test_2_value); now displays the same value as 9. above.
Every time I add or remove fields from Views my template file beaks because the fields have a new alias.
Is there a way to keep the field alias the same while editing my view?
Comments
Comment #1
merlinofchaos commentedThis is the expected behavior; you should never refer to the alias names used in the $result directly. If you do, you run the risk of exactly this happening.
This:
print($view->result[0]->node_data_field_test_2_field_test_2_value);is a bug in your code.
Comment #2
merlinofchaos commentedNot only is that a bug, that is begging for cross-site scripting attacks.
Comment #3
easp commentedI did not see anywhere in the $view object that I could grab the value from the field other than from the field alias.
Is there another way to grab the field value?
Comment #4
webchickUse the $rows variable in your template. This contains the sanitized values, ready for printing, and are named nice, sane things like $rows[0]['field_test_2'].
Comment #5
easp commentedThanks for the tip. I will look into the $rows variable instead of the $views object.
Comment #6
merlinofchaos commentedIt actually depends on the template (sorry about that, I am a bit inconsistent about this). Sometimes it's $rows and sometimes it's $fields -- but this should be documented at the top of every template. In addition, there's a more extensive document telling you how to find this information in the advanced help.
Comment #7
easp commentedHow would I access hidden fields?
They do not appear in the $rows or $fields variables.
Comment #8
merlinofchaos commentedThat's slightly trickier:
Comment #9
webchickSince that seems like scary voodoo Views dark magick, ;) I spoke with merlin on IRC and he suggested #362218: Add a consistent way to render fields from a view as a consistent way to deal with printing any type of field, hidden or no.
Comment #10
webchickJust a quick correction to #8 to save the next person some headaches. This ended up actually being:
(Where the CCK field was named 'field_host')
Thanks again, Earl!
Comment #11
LEternity commentedThere (still) seems to be a related problem with Views + Views Custom Field. See #701798: use of the "field id" instead of field aliases.
Comment #12
joetsuihk commentedRe #10
or use:
Comment #13
akobashikawa commentedI hope this help you: http://drupal.org/node/997228#comment-4731518