Thursday, October 29, 2015

Get node title from nid without using node_load($nid) in Drupal

Get node title without using node_load($nid).

function get_node_title($nid) {
  $node_title = db_select('node', 'n')
      ->fields('n',array('title'))
      ->condition('nid',$nid,'=')
      ->execute()
      ->fetchField();
  return $node_title;
}

How to render menu programatically in Drupal 7?

In template.php file you can add the below code:

$menu = menu_tree_output(menu_tree_all_data('main-menu', null, 1));
$variables['menu'] = drupal_render($menu);

Page.tpl.php (render the main menu tree in the page.tpl.php like this.)
<?php print $menu; ?>

menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL)
[https://api.drupal.org/api/drupal/includes!menu.inc/function/menu_tree_all_data/7]

Monday, October 19, 2015

Add Body field programmatically in Drupal

<?php

$types = node_type_get_types();

node_add_body_field($types['your_content_type_machine_name']);

?>