Drupal Recipe | Common Drupal problems and their solutions

August 08, 2014


Here Developer will find most important Drupal problems and their solutions

How do I find if a taxonomy term is used in a node?

use taxonomy_seletct_nodes() function.
Example: <?php // For D6 $nodes = array(); if ($term-&gt;tid) { $result = taxonomy_select_nodes(array($term-&gt;tid), 'or', 0, FALSE); while ($row = db_fetch_object($result)) { $nodes[] = $row-&gt;nid; } } if (!empty($nodes)) { // The taxonomy term is used from at least a node. } // For D7 $nodes = array(); if ($term-&gt;tid) { $result = taxonomy_select_nodes(array($term-&gt;tid), FALSE); while ($row = db_fetch_object($result)) { $nodes[] = $row-&gt;nid; } } if (!empty($nodes)) { // The taxonomy term is used from at least a node. } ?>

How to reset Login password ?

<?php// D6 // Go to phpmyadmin, Select users table and write the following code and execute UPDATE users set pass=md5('123456') where uid=1; // D7 // Open index.php file. Add these two line after drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); require_once 'includes/password.inc'; echo user_hash_password('123456'); die(); //No Run you website. Copy this password . Open your database , visit table: users and replace this password. ?>

Login blocked after 5 failed login attempts or how to unblock my own admin account after 5 failed login attempts?

<?php// D7 Truncate table flood ?>

Best way to find out number or rows returned after executing drupal query?

<?php// for D6 $result = db_query("SELECT COUNT(*) total_row FROM node"); $num_of_rows = db_result($result); // for D7 $result = db_query("SELECT COUNT(*) total_row FROM node"); $num_of_rows = $result-&gt;rowCount(); // for D7 $query = db_select('node', 'n'); $query-&gt;fields('n', array('nid', 'title', 'status')); $results = $query-&gt;execute()-&gt;fetchAll(); $count = count($results); foreach ($results as $result) { // ... } // for D7 $res = db_query("SELECT title FROM {node} WHERE status = '%d'", 1); $num_of_rows = $res-&gt;num_rows; ?>

How to add JS code Properly in Drupal?

<?php(function ($) { // Disable checkboxes when the user clicks on one of them. (prevent // multi-click Drupal.behaviors.kickstartSearch = { attach:function (context) { $('.facetapi-checkbox').live('click', function(e) { $('.facetapi-checkbox').attr("disabled", true); }); } } })(jQuery); ?>

How to get Taxonomy term name from Taxonomy term id ?

Most simplest way to find Term name is : First Load the term by term id using Drupal's built in function and then get Term Name from that object.

<?php// for D6 $term = taxonomy_get_term($tid=2); $term_name = $term-&gt;name; echo $term_name; // for D7 $term = taxonomy_term_load($tid=2); $term_name = $term-&gt;name; echo $term_name; ?>

Drupal 7 How to remove fieldset on a date field ?

When we create date field in Drupal 7, its automatically adds a Fieldset around this field. In order to remove this write the below code into your template.php file

Clear your cache after writing this code
<?php// for D7 // Replacing MYTHEME with the name of your theme function MYTHEME_date_combo($variables) { return theme('form_element', $variables); } ?>

How to print an SQL query with arguments in Drupal 7 ?

<?php$query = db_select('node', 'n'); $query-&gt;join('users', 'u', 'n.uid = u.uid'); //JOIN node with users $query-&gt;groupBy('u.uid');//GROUP BY user ID $query-&gt;fields('n',array('title','created'))//SELECT the fields from node -&gt;fields('u',array('name'))//SELECT the fields from user -&gt;orderBy('created', 'DESC')//ORDER BY created -&gt;range(0,2);//LIMIT to 2 records // Print Query as string print $query-&gt;__toString(); // or // Second way // echo (string)$query; // print arguments in query echo '?>
<?php';print_r($query-&gt;arguments()); echo '?>

<?php'; ?&gt; ?>

You do not have sufficient permissions to edit Drupal fields?

Suppose you create a role 'content manager' and assign a user to this role and give all necessary permissions. But when user wants to create or edit content he see the following errors

This field has been disabled because you do not have sufficient permissions to edit it

You are two steps behind to solve this problem.

Step 1

You need to give the 'content manager' role access to use one or more of the filters (Full HTML, Filtered HTML, etc) otherwise they won't be able to edit the body. To do this Configure TEXT FORMATS . To do this go to: http://www.example.com/admin/config/content/formats

Step 2

Now go to Drupal permissions page, just search for 'Filter' and give permission Administer text formats and filters. Save Configuration.

How to add JS file in Drupal 7 to Specific Form or Node type or based on Some condition

Use Drupal API hook_form_alter() function. Some time we need to add external Javascript file in Drupal based on node type or Different Drupal form based on Condition.

// implement hook_form_alter()

// replace MYMODULE to your module name
function MYMODULE_form_alter(&$form, &$form_state$form_id){
if (
$form_id == 'event_node_form'){
// add event.js file
$form['#attached']['js'] = array(drupal_get_path('module''MYMODULE') . '/MYMODULE.js');

How to Fix Drupal notice error undefined Index or Undefined Variable Probblem ?

As we know, PHP is a loosely couple language. We can Use variable anywhere without declaring it. But in Drupal this produce an error message and each time we access any page this error is written to Drupal Watchdog table. So, If you see your Recent log entry page you will see lots of errors regarding this. A line of code can solve this !

It is best practice to turn off notice error in Production server.
Update your settings.php file
// at the end of the settings.php file, write this
/* Fix notice error. Best config for production server */
ini_set('error_reporting''E_ALL ^ E_NOTICE');

Now clear your Recent log entries. Browse you site and at the end of the days, you found no notice errors

Do not try to put this above code any where else like your module page or inside function, because it won't work.

How to Fix Administration menu Disappears problem ?

Configure your administration menu.

Go to admin menu config page:
Select Performance Tab and Uncheck "Cache menu in client-side browser"
Hit Save Configuration button and Finally Clear your cache.

How to create form data as array?

Use $form['#tree'] = true, property
for($i 0$i 3$i++) {
$form['contact'][$i]['value'] = array(
'#type' => 'textfield',
'#title' => 'Contact Name',
'#size' => 50,

The above code will produce following outputs:

<?php <input name="0" size="50" type="text" value="" /> <input name="1" size="50" type="text" value="" /> <input name="2" size="50" type="text" value="" /> ?>

But In order to product following output use form tree property. Our final codes will be

for($i 0$i 3$i++) {
$form['contact'][$i]['value'] = array(
'#type' => 'textfield',
'#title' => 'Contact Name',
'#size' => 50,
$form['contact']['#tree'] = TRUE;

The output will be

<?php <input name="contact[0][value]" size="50" type="text" value="" /> <input name="contact[1][value]" size="50" type="text" value="" /> <input name="contact[2][value]" size="50" type="text" value="" /> ?>