3

When doing complex display logic and breaking apart a renderable array in a field collection template, what's the best string output to use from the array: #markup, or safe_value? Presumably #markup is already run through the right filters... right?

1 Answer 1

1

You actually want to use the render() function like so:

<?php print render($content[FIELD_NAME]); ?>

See http://api.drupal.org/api/drupal/includes%21common.inc/function/render/7 for reference. Also, be sure to use hide($content[FIELD_NAME]) right before you render $content or you will render the field a second time (unless you've hidden it in the view mode on the Manage Display tab of the content type).

4
  • Thanks, but I'm working with a field collection template, and it's a bit hairier than that. Tried to keep the question general and best-practicy though. That would work in the node template, yes. Attempts at solving the specific problem: drupal.org/node/1398966, drupal.stackexchange.com/questions/35355/…
    – Renee
    Commented May 31, 2013 at 1:07
  • You can accomplish the same thing in the field-collection-item.tpl.php as in the node template. Directly from the tpl comments:$content: An array of comment items. Use render($content) to print them all, or * print a subset such as render($content['field_example']). Use * hide($content['field_example']) to temporarily suppress the printing of a * given element. Commented Jun 6, 2013 at 1:06
  • Not usefully for our case, alas. Theming the field-collection-items template is nasty, and there is currently a core bug in the ordering of .tpl.php files that means the template of a field-collection-item.tpl.php recurses, being applied to the output of each field item inside it as well, and there's no way to make it idempotent. It's truly awful if you want to access specific items within a collection and change their order, for example to collate them in the output. We went a built a custom formatter instead and grabbed the field data off the entity IDs. Proved far less painful.
    – Renee
    Commented Jun 17, 2013 at 19:50
  • I'm giving this accepted answer, because even though it doesn't work for our use-case, it is the best standard way. (However, I still would really like to know, just for curiosity's sake, for people who must output raw from an array, which is better to use, safe-value or #markup. ;)
    – Renee
    Commented Jul 9, 2013 at 20:16

Your Answer

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

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