0

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 template file, I am doing it like this way:

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

by looping $compfields variable. Now my PROBLEM is : here I know that my content type has field named body which follow print '<p>'.$value->body['und']['0']['value'].'</p>'; pattern to get rendered. But If a user add some additional field, how can I render that field to static html structure present in my template without knowing which type of field he is going to add.

  • Sounds bizarre, why aren't you using the Manage Display tab of the content type? – Colin Shipton Jul 24 '15 at 13:30
  • 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 Jul 24 '15 at 13:36
  • @clive can you give an example of good module so that I can learn coding standards against XSS attack? – Sugandh Khanna Jul 24 '15 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 Jul 24 '15 at 14:00
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)

  • 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?? – Sugandh Khanna Jul 24 '15 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 Jul 24 '15 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? – Sugandh Khanna Jul 24 '15 at 15:10

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.