0

I'm here again to ask for some help, I'm running a query in a custom module to get contents of a node type, with this:

$node_type = "mynodetype";
  $nids = db_query("SELECT * FROM {node} WHERE type = :type", array(':type' => $node_type))->fetchCol();
  $nodes = node_load_multiple($nids);

And returning $nodes to the template, I'm using a foreach for $nodes array and I get something like this when I print_r() an element of $nids array.

stdClass Object ( [vid] => 12658 [uid] => 1 [title] => Node title [log]
=> [status] => 1 [comment] => 0 [promote] => 0 [sticky] => 0 [nid] =>
12658 [type] => mynodetype [language] => es [created] => 2017
[changed] => 1502159375 [tnid] => 0 [translate] => 0
[revision_timestamp] => 1502159375 [revision_uid] => 1 [mp_idmod]
=> Array ( [und] => Array ( [0] => Array ( [value] => 1 ) ) )
[mp_nmod] => Array ( [und] => Array ( [0] => Array ( [value]
=> Myvalue [format] => [safe_value] => Myvalue ) ) ) [rdf_mapping]
=> Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document )
[title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created]
=> Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created )
[datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] =>
Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] =>
xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates]
=> Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] =>
Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array (
[predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array (
[predicates] => Array ( [0] => sioc:num_replies ) [datatype] =>
xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] =>
sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] =>
date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 2017
[last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0
[name] => admin [picture] => 0 [data] => b:0; )

And inside the foreach, to render the fields I do it this way:

<?php print $nid->mp_idmod[LANGUAGE_NONE][0]['value'];  ?>

My question is, this is the right way to render the fields value of each node content type loaded? Or I should do it other way?

0

Personally, I would create an Entity View Mode for the content type with the fields arranged and formatted how I want them, then you can simply do:

$node = node_load($nid);
$node_view = node_view($node);
print drupal_render($node_view);

In your case, you need multiple nodes, so:

$views = node_view_multiple($nids, "name of entity view mode");
print drupal_render($views);

Now that you are using an entity view mode, you can give the markup you want in the node--node_type--view_mode.tpl.php file, and it will use that.

As for the query and dynamic code, it should go in a preprocess hook, and not the tpl itself. You can return it as a $variable to the template being rendered, usually this is a page callback.

Again, I don't know much about your module or where this code is. You could enable Entity View Modes and just shift this code and get rid of it, and leverage Views instead for the same exact result.

| improve this answer | |
  • Thanks for your comment, I have my query on a callback that I defined on a hook_menu item, I need to list all the contents(each field) of a custom node type in a tpl defined in the hook_theme, and the only way I found to do that was with <?php print $nid->mp_idmod[LANGUAGE_NONE][0]['value']; ?> but I feel that it's kind of messy, and I tought that there should be another way. – Elros Romeo Aug 11 '17 at 2:14
  • Also I read that it's convenient to use EntityFieldQuery instead of db_query, but I don't understand why and if with this the render of the fields in the tpl will be more clean or not. – Elros Romeo Aug 11 '17 at 2:15
  • I think you'll find it easier to relay this to Views and use a view mode. You can use views_embed_view in the template preprocess, or just let it be a Views page display. – Kevin Aug 11 '17 at 2:16
  • I've done it both ways, but depending on your need you may find the flexibility of the View beneficial. Look into Entity View Modes, it will change your life. – Kevin Aug 11 '17 at 2:18

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.