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

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.

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

Hope that helps, Jason.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["<li class=\"-item\"><a href=\"https://www.facebook.com/officialstackoverflow/\" class=\"-link\">Facebook</a></li>\r\n                    <li class=\"-item\"><a href=\"https://twitter.com/stackoverflow\" class=\"-link\">Twitter</a></li>\r\n                    <li class=\"-item\"><a href=\"https://linkedin.com/company/stack-overflow\" class=\"-link\">LinkedIn</a></li>\r\n                </ul>\r\n\r\n                <p class=\"mt-auto mb24\">\r\nsite design / logo &#169; 2019 Stack Exchange Inc; user contributions licensed under <a href=\"https://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a>\r\n                            with <a href=\"https://stackoverflow.blog/2009/06/25/attribution-required/\" rel=\"license\">attribution required</a>.\r\n                    <span id=\"svnrev\">rev&nbsp;2019.7.23.34400</span>\r\n                </p>\r\n            </div>\r\n        </div>\r\n\r\n            </footer>\r\n            <script>StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); })</script>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Drupal Answers works b"], "copyright__copyright__copyright": ["int $field->element_type; ?> class=\"field-content my-class\"><?php print $field->content; ?></<?php print $field->element_type; ?>>\n<?php else: ?>\n  <<?php print $field->element_type; ?> class=\"field-content\"><?php print $field->content; ?></<?php print $field->element_type; ?>>\n<?php endif; ?>\n\nYour Answer\n\nBy clicking \u201cPost Your Answer\u201d, you agree to our terms of service, privacy policy and cookie policy\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}