-1

I am making a module, each time user make content type and add fields to it, all its field values get rendered into static template dynamically.

For this, I am using noad_load_multiple() to get all node values of selected content type and using hook_theme(), displaying it in a static template page.

$compfields = [node_load_multiple][2](array(), array('type' => $node_type));

return theme('component_fields',array('compfields' => $compfields));

In the template file, I am using the following code, looping over $compfields.

print '<p>' . $value->body['und']['0']['value'] . '</p>';

I know my content type has a body field which is rendered with print '<p>' . $value->body['und']['0']['value'] . '</p>';.

How can I render the additional fields added to a content type without knowing their field type?

4
  • Sounds bizarre, why aren't you using the Manage Display tab of the content type? Commented Jul 24, 2015 at 13:30
  • 1
    Your code is susceptible to XSS attacks. Unless you understand what that means, and why it's bad, you shouldn't be doing what you're doing. Get a more experienced developer to help you, at least
    – Clive
    Commented Jul 24, 2015 at 13:36
  • @clive can you give an example of good module so that I can learn coding standards against XSS attack? Commented Jul 24, 2015 at 13:59
  • Read all of the links here: drupal.org/developing/best-practices, and everything those inner pages link out to as well. There's a lot of ground to cover
    – Clive
    Commented Jul 24, 2015 at 14:00

1 Answer 1

0

You could run a 'foreach' on the items in the list. Added fields will be named 'field_ ...' which is easy to check. The fields come with their own render functions, also available in the field object code. Although i think drupal_render() will handle it correctly anyway.

A first step could be to use dsm($compfields) to identify the field structure and then use it. (dsm is part of the devel module and displays structures in nicely readable format)

3
  • you mean to say check condition on every type of field, that if my field is of type text, render $value->text. If field is of type summary or text area, follow $value->body['und']['0']['value'] pattern, if it's an image field follow xyz pattern?? Commented Jul 24, 2015 at 13:58
  • yes, that seems like a straightforward way, since you are in control of 'when a field is added' so caching will be no issue, nor the multiple reruns. btw is it drupal 7 or 6? and check out drupal_render of a field, it will handle the different theme functions itself.
    – Joel Box
    Commented Jul 24, 2015 at 14:31
  • its drupal 7. Ok, I will go with this approach. But I think, it will gonna be too lengthy. Any short cut or function that can directly recognize every dynamic field and behave accordingly? Commented Jul 24, 2015 at 15:10

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.