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']);

?>

Wednesday, September 30, 2015

Print a custom static fivestar widget with fivestar module

$star = 80;
$star_display =  theme('fivestar_static', array('rating' => $star,'stars' => 5));
print render($star_display);

Tuesday, September 15, 2015

Install PHP, Apache, MySQL, PhpMyAdmin On Ubuntu


sudo apt-get update
sudo apt-get upgrade (optional)

MySQL
=====
sudo apt-get install mysql-server mysql-client

Apache

=====
sudo apt-get install apache2

PHP
===
sudo apt-get install php5 libapache2-mod-php5 php5-mysql

Restart Apache
===========
/etc/init.d/apache2 restart

PHPMyAdmin
==========
sudo apt-get install phpmyadmin

I don’t like to use Apache’s default http folder /var/www so I need to update the apache2.conf file:
sudo gedit /etc/apache2/apache2.conf

Changing the default location will require me to make sure phpMyAdmin continues to load on http://localhost/phpMyAdmin so at the end of the same file I ll add the virtual host configuration for phpMyAdmin
Include /etc/phpmyadmin/apache.conf

Restart Apache   
===========
/etc/init.d/apache2 restart

------------------------------------------------------------------------------------------------------------------------------------

Increase Upload Max File size and other configurations

sudo gedit /etc/php5/apache2/php.ini
upload_max_filesize 2M
sudo /etc/init.d/apache2 restart

------------------------------------------------------------------------------------------------------------------------------------

Clean URLs with Apache 2 on Ubuntu

sudo a2enmod rewrite
sudo apache2ctl -M

If you are running Ubuntu 14.04+:
sudo gedit /etc/apache2/apache2.conf
Otherwise:
sudo gedit /etc/apache2/sites-available/default

AllowOverride None to AllowOverride All
sudo /etc/init.d/apache2 reload

------------------------------------------------------------------------------------------------------------------------------------

Enable Curl

sudo apt-get install php5-curl & sudo /etc/init.d/apache2 restart

------------------------------------------------------------------------------------------------------------------------------------

Thursday, October 16, 2014

Clean URLs with Apache 2 on Ubuntu

Open terminal: Ctrl+Alt+T

1)  sudo a2enmod rewrite
2) sudo apache2ctl -M   (and seeing if rewrite_module is on the list.)
3) sudo gedit /etc/apache2/sites-available/default
4) change text "AllowOverride None" to "AllowOverride All"
5) sudo /etc/init.d/apache2 reload


Refer: https://www.drupal.org/node/134439

Thursday, October 9, 2014

Building Multistep forms using the Drupal 7 CTools object cache

http://drupalcontrib.org/api/drupal/contributions!ctools!includes!object-cache.inc/7
The non-volatile object cache is used to store an object while it is being edited, so that we don't have to save until we're completely done. The cache should be 'cleaned' on a regular basis, meaning to remove old objects from the cache, but otherwise the data in this cache must remain stable, as it includes unsaved changes.

Building Multistep forms using the Drupal 7 CTools object cache

multi_step.info
 name = MultiStep  
 description = Custom Multistep form module.  
 core = 7.x  
 dependencies[] = ctools  

multi_step.install
 <?php  
 /**  
  * Implements hook_schema().  
  */            
 function multi_step_schema() {  
  $schema['score'] = array(  
   'description' => 'Stores all-specific information for Score.',  
   'fields' => array(  
     'id' => array(  
     'type' => 'serial',  
     'not null' => TRUE,  
    ),  
    'score' => array(  
     'type' => 'varchar',  
     'default' => '',  
     'length' => 15,  
    ),     
    'smoke' => array(  
     'type' => 'varchar',  
     'default' => '',  
     'length' => 15,  
    ),  
   ),  
   'primary key' => array('id'),  
  );  
  return $schema;  
 }  

multi_step.module
 function multi_step_menu() {  
  $items = array();  
  $items['step1'] = array(  
   'title' => 'Score',  
   'page callback' => 'drupal_get_form',  
   'page arguments' => array('multi_step_one'),  
   'access callback' => TRUE,  
   'type' => MENU_NORMAL_ITEM,  
  );       
  $items['step2'] = array(  
   'title' => 'Score',  
   'page callback' => 'drupal_get_form',  
   'page arguments' => array('multi_step_two'),  
   'access callback' => TRUE,  
   'type' => MENU_NORMAL_ITEM,  
  );  
  $items['form-results'] = array(  
   'title' => 'Results',  
   'page callback' => 'multi_step_display_results',  
   'access callback' => TRUE,  
   'type' => MENU_NORMAL_ITEM,  
  );  
  return $items;       
 }  
 // Step 1  
 function multi_step_one($form, &$form_state) {  
  ctools_include('object-cache');  
  $object = ctools_object_cache_get('submission', 'multi_step_submission');  
      $form['#prefix'] = '<div id="multistep" class="step1">';  
      $form['#suffix'] = '</div>';  
      $form['score'] = array(  
       '#type' => 'radios',  
       '#options' => array(1,2,3,4,5,6,7,8,9,10),  
       '#required' => TRUE,  
       '#default_value' => isset($object->score) ? $object->score : array(null),  
      );  
      $form['multi_step_one_submit'] = array(  
       '#type' => 'submit',  
       '#value' => t('Next'),  
       '#ajax' => array(  
           'wrapper' => 'multistep',  
           'callback' => 'multi_step_one_callback',  
           'progress' => array('type' => 'throbber'),  
           'prevent' => 'submit click mousedown',  
       ),  
      );  
  return $form;  
 }  
 // Step 1 Ajax Submit  
 function multi_step_one_callback($form, &$form_state) {  
  drupal_validate_form('multi_step_one', $form, $form_state);  
  if (form_get_errors()) {  
   $form_state['rebuild'] = TRUE;  
   return $form;  
  }  
  multi_step_one_submit($form, $form_state);  
      $f = drupal_get_form('multi_step_two');  
      return drupal_render($f);  
 }  
 // Step 1 Submit  
 function multi_step_one_submit($form, &$form_state) {  
  ctools_include('object-cache');       
  $object = ctools_object_cache_get('submission', 'multi_step_submission');  
      if(isset($object) && $object != '') {  
       $object->score = $form_state['values']['score'];  
      }  
      else {  
           $object = (object) array(  
               'score' => $form_state['values']['score'],  
           );  
      }  
  ctools_object_cache_set('submission', 'multi_step_submission', $object);  
  $form_state['redirect'] = 'step2';  
 }  
 // Step 2  
 function multi_step_two($form, &$form_state) {  
      global $base_url;  
      ctools_include('object-cache');  
  $object = ctools_object_cache_get('submission', 'multi_step_submission');  
      $form['#prefix'] = '<div id="multistep" class="step2">';  
      $form['#suffix'] = '</div>';  
      $smoke_options = array('never' => "Never", 'notnow' => "Not now", 'yes' => 'Yes');  
      $form['smoke'] = array(  
           '#type' => 'radios',  
           '#title' => 'Do you smoke?',  
           '#options' => $smoke_options,  
           '#default_value' => isset($object->smoke) ? $object->smoke : '',  
           '#required' => TRUE,  
      );  
      $form['back'] = array(  
        '#type' => 'submit',  
        '#value' => t('back'),  
        '#limit_validation_errors' => array(),  
        '#ajax' => array(  
           'wrapper' => 'multistep',  
           'callback' => 'multistep_two_back_submit',  
           'progress' => array('type' => 'throbber'),  
           'prevent' => 'submit click mousedown',  
        ),  
        '#submit' => array('multistep_two_back_submit'),  
  );  
  $form['submit'] = array(  
   '#type' => 'submit',  
   '#value' => t('Next'),  
   '#ajax' => array(  
       'wrapper' => 'multistep',  
       'callback' => 'multistep_two_ajax_submit',  
       'progress' => array('type' => 'throbber'),  
       'prevent' => 'submit click mousedown',  
      ),  
   '#submit' => array('multistep_two_submit'),  
  );  
  return $form;  
 }  
 // Step 2 Back  
 function multistep_two_back_submit($form, &$form_state) {  
      $form_state['redirect'] = 'step1';  
      $f = drupal_get_form('multi_step_one');  
      return drupal_render($f);  
 }  
 // Step 2 Ajax Submit  
 function multistep_two_ajax_submit($form, &$form_state) {  
      drupal_validate_form('multi_step_two', $form, $form_state);  
  if (form_get_errors()) {  
   $form_state['rebuild'] = TRUE;  
   return $form;  
  }  
  multistep_two_submit($form, $form_state);  
      return multi_step_display_results();  
 }  
 // Step 2 Submit  
 function multistep_two_submit($form, &$form_state) {  
  ctools_include('object-cache');  
  $object = ctools_object_cache_get('submission', 'multi_step_submission');  
  $object->smoke = $form_state['values']['smoke'];  
  ctools_object_cache_set('submission', 'multi_step_submission', $object);  
  $form_state['redirect'] = 'form-results';  
 }  
 /******************************Results***********************************/  
 function multi_step_display_results() {  
  ctools_include('object-cache');  
  $object = ctools_object_cache_get('submission', 'multi_step_submission');   
      //step1  
      $scorethink = isset($object->score) ? $object->score : '';  
      //step2  
      $smoke = isset($object->smoke) ? $object->smoke : '';  
      db_insert('score')->fields(array(  
           'score' => $scorethink,  
           'smoke' => $smoke,  
      ))->execute();  
      ctools_object_cache_clear('submission', 'multi_step_submission');  
      if ($object) {  
   return array(  
    '#markup' => 'Submitted Values: <br/>Score='.$scorethink.'<br/>Smoke='.$smoke,  
   );  
  }  
  else {  
   drupal_goto('score');  
  }       
 }  

Thursday, September 18, 2014

How to change a dynamic block title

function hook_block_view_alter(&$data, $block) {
  if($block->delta == "XXX") { 
      $block->title = "YYY";
  }
}

Monday, March 31, 2014

500 – Internal server error from IIS on Windows server

Open a web.config file in root folder & add this line  <httpErrors errorMode="Detailed" />


Ex:
 <system.webServer>
 .....................................
// comment below lines
 <!--<httpErrors>-->
      <!--<remove statusCode="404" subStatusCode="-1" />-->
      <!--<error statusCode="404" prefixLanguageFilePath="" path="/index.php" responseMode="ExecuteURL" />-->
<!--</httpErrors>-->

........................................

 // Add this line
 <httpErrors errorMode="Detailed" />

</system.webServer>

Sunday, February 2, 2014

Drupal 7 - Using Ctools modal popup node view with comments

<?php
/**
 *  Implements of hook_menu()
 
*/
function hook_menu() {
  $items = array();
  $items['pop/%ctools_js/%'] = array(
    'page arguments' => array(1, 2),
    'page callback' => 'pop_modal_page',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

function pop_modal_page($js = NULL, $nid) {
  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
  } else return 'Your browser does not support javascript';

  $node = node_load($nid);
  $node_view = node_view($node);
  $node_view['comments'] = comment_node_page_additions($node);
  $contents = render($node_view); 
  return ctools_modal_render($node->title, $contents) ;
}