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: // For D6 $nodes = array(); if ($term->tid) { $result = taxonomy_select_nodes(array($term->tid), 'or', 0, FALSE); while ($row = db_fetch_object($result)) { $nodes[] = $row->nid; } } if (!empty($nodes)) { // The taxonomy term is used from at least a node. } // For D7 $nodes = array(); if ($term->tid) { $result = taxonomy_select_nodes(array($term->tid), FALSE); while ($row = db_fetch_object($result)) { $nodes[] = $row->nid; } } if (!empty($nodes)) { // The taxonomy term is used from at least a node. }

How to reset Login password ?

// 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?

// D7 Truncate table flood

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

// 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->rowCount(); // for D7 $query = db_select('node', 'n'); $query->fields('n', array('nid', 'title', 'status')); $results = $query->execute()->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->num_rows;

How to add JS code Properly in Drupal?

(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.

// for D6 $term = taxonomy_get_term($tid=2); $term_name = $term->name; echo $term_name; // for D7 $term = taxonomy_term_load($tid=2); $term_name = $term->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
// 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 ?

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

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.

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

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

The above code will produce following outputs:

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

The output will be