1

I try to make a custom view using templates. I need to print one certain field at the last row, after all other fields have been printed like so:

--1st row
field1
field2
--2nd row
field1a
field2a
--last row
myfield

I can manage to display a custom field inside field template like so:

<?php print $fields['title_1']->content; ?> 

However there are few problems:
1/to display the field as needed, I should print the field inside row template - but above code doe's not work inside row template, only inside field template.
2/the field get's printed twice - as normal field and as a field called directly from template as well.

The questios are 1/how can I print a field called from other than field template? 2/To prevent printing the field twice, I assume I need to exclude it from display, but after that it is not available inside template.
I found this article https://drupal.org/node/687046 where people say it is possible. I used following code but it is not working

print_r($view->result[$id]node_data_field_title_1_field_title_1_value)

can anyone point me to right direction?

Thank you

ANSWER: the correct code to put inside row template is

print $view->result[$id]->node_title

this displays excluded field

8
  • You don't have to exclude a field from display to get it printed on the template file , You must update the row style output template file
    – sel_space
    Dec 26, 2013 at 22:09
  • @sel_space if I don't exclude it, it would be printed twice - once as views field and second as a field manually printed from template using code, or am I missing something?
    – loparr
    Dec 26, 2013 at 22:15
  • @loparr, I never seen that field printed twice in template file. Which template your are using to render field? Dec 27, 2013 at 5:06
  • @JayendraKainthola I use field tamplate - it is the only one where the above code for printing field works. Tha field is printed twice - because it get's printed as normal field and then second called from template with code above.
    – loparr
    Dec 28, 2013 at 18:30
  • @loparr, I hope you are overriding views-view-fields.tpl.php for your views. Dec 29, 2013 at 3:30

2 Answers 2

0

I can't comment yet, or I would to inquire further about your use case.

Instead, I'll just provide an answer in hopes it helps.

Do you absolutely need to use a template file? In Drupal 7 Views 3, there are options for each field to customize the html for the field label and value if desired. See attached view field style settings to see how this is configurable.

view field style settings

Optionally, you can set the html to none and rewrite the output to include your specific markup. See second attachment.

rewrite field output

Hope that helps, Jason.

1
  • hi, I am using drupal 6. Yes I need to use template file.
    – loparr
    Dec 26, 2013 at 23:16
0

Whoops, I did not see the drupal 6 tag. I left my previous answer anyway.

You can also rewrite the output in drupal 6 to include your special markup, but being you have to use a template file, you do not want to exclude the field in the ui settings. If you exclude it, it will not make it to the theme layer.

You can use the a field template file and you can wrap <?php print $output; ?> with your additional markup. However, this will include the default markup as well, which is provided by the row style output template.

Based on your question including <?php print $fields['title_1']->content; ?>, I'd guess you're using a row style template file. In this file, I think you would need to add an if statement to check that you have the correct field. If true, adjust your markup accordingly, else, keep the default markup.

Something like

<?php if($field->handler->field_alias == 'node_title'): ?>
  <<?php print $field->element_type; ?> class="field-content my-class"><?php print $field->content; ?></<?php print $field->element_type; ?>>
<?php else: ?>
  <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
<?php endif; ?>
2
  • thank you for your patience, I think I did not explained it clearly. Please take look at my updated question. I want to display the field as last, after all loops have been made.
    – loparr
    Dec 27, 2013 at 0:47
  • I don't understand what you're trying to accomplish, sorry.
    – Jason
    Dec 27, 2013 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.