Webemania | Drupal Recipe | Common Drupal problems and their solutions

| Published Date: 03 Aug 2014 | Last Updated Date: 04 Sep 2017
Total Reads: 3,593 | Last Visit: 21 Sep 2017 02:23:15 AM

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:
  1. // For D6
  2. $nodes = array();
  3. if ($term->tid) {
  4. $result = taxonomy_select_nodes(array($term->tid), 'or', 0, FALSE);
  5. while ($row = db_fetch_object($result)) {
  6. $nodes[] = $row->nid;
  7. }
  8. }
  9. if (!empty($nodes)) {
  10. // The taxonomy term is used from at least a node.
  11. }
  12.  
  13. // For D7
  14. $nodes = array();
  15. if ($term->tid) {
  16. $result = taxonomy_select_nodes(array($term->tid), FALSE);
  17. while ($row = db_fetch_object($result)) {
  18. $nodes[] = $row->nid;
  19. }
  20. }
  21. if (!empty($nodes)) {
  22. // The taxonomy term is used from at least a node.
  23. }
  24.  

How to reset Login password ?

  1. // D6
  2. // Go to phpmyadmin, Select users table and write the following code and execute
  3.  
  4. UPDATE users set pass=md5('123456') where uid=1;
  5.  
  6. // D7
  7. // Open index.php file. Add these two line after
  8. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  9. require_once 'includes/password.inc';
  10. echo user_hash_password('123456'); die();
  11.  
  12. //No Run you website. Copy this password . Open your database , visit table: users and replace this password.
  13.  

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

  1. // D7
  2. Truncate table flood
  3.  

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

  1. // for D6
  2. $result = db_query("SELECT COUNT(*) total_row FROM node");
  3. $num_of_rows = db_result($result);
  4.  
  5. // for D7
  6. $result = db_query("SELECT COUNT(*) total_row FROM node");
  7. $num_of_rows = $result->rowCount();
  8.  
  9.  
  10. // for D7
  11. $query = db_select('node', 'n');
  12. $query->fields('n', array('nid', 'title', 'status'));
  13. $results = $query->execute()->fetchAll();
  14.  
  15. $count = count($results);
  16.  
  17. foreach ($results as $result) {
  18. // ...
  19. }
  20.  
  21. // for D7
  22. $res = db_query("SELECT title FROM {node} WHERE status = '%d'", 1);
  23. $num_of_rows = $res->num_rows;
  24.  

How to add JS code Properly in Drupal?

  1. (function ($) {
  2. // Disable checkboxes when the user clicks on one of them. (prevent
  3. // multi-click
  4. Drupal.behaviors.kickstartSearch = {
  5. attach:function (context) {
  6. $('.facetapi-checkbox').live('click', function(e) {
  7. $('.facetapi-checkbox').attr("disabled", true);
  8. });
  9. }
  10. }
  11. })(jQuery);
  12.  

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.

  1. // for D6
  2. $term = taxonomy_get_term($tid=2);
  3. $term_name = $term->name;
  4. echo $term_name;
  5.  
  6. // for D7
  7. $term = taxonomy_term_load($tid=2);
  8. $term_name = $term->name;
  9. echo $term_name;
  10.  

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
  1. // for D7
  2. // Replacing MYTHEME with the name of your theme
  3. function MYTHEME_date_combo($variables) {
  4. return theme('form_element', $variables);
  5. }
  6.  

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

  1. $query = db_select('node', 'n');
  2. $query->join('users', 'u', 'n.uid = u.uid'); //JOIN node with users
  3. $query->groupBy('u.uid');//GROUP BY user ID
  4. $query->fields('n',array('title','created'))//SELECT the fields from node
  5. ->fields('u',array('name'))//SELECT the fields from user
  6. ->orderBy('created', 'DESC')//ORDER BY created
  7. ->range(0,2);//LIMIT to 2 records
  8.  
  9. // Print Query as string
  10. print $query->__toString();
  11.  
  12. // or
  13. // Second way
  14. // echo (string)$query;
  15.  
  16. // print arguments in query
  17. echo '<pre>';print_r($query->arguments()); echo '</pre>';
  18. ?>
  19.  

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.

  1. <?php
  2. // implement hook_form_alter()
  3.  
  4. // replace MYMODULE to your module name
  5. function MYMODULE_form_alter(&$form, &$form_state, $form_id){
  6. if ($form_id == 'event_node_form'){
  7. // add event.js file
  8. $form['#attached']['js'] = array(drupal_get_path('module', 'MYMODULE') . '/MYMODULE.js');
  9. }
  10. }
  11. ?>

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
  1. <?php
  2. // at the end of the settings.php file, write this
  3. /* Fix notice error. Best config for production server */
  4. ini_set('error_reporting', 'E_ALL ^ E_NOTICE');
  5. ?>

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:

http://example.com/admin/config/administration/admin_menu

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
Example
  1. <?php
  2. for($i = 0; $i < 3; $i++) {
  3. $form['contact'][$i]['value'] = array(
  4. '#type' => 'textfield',
  5. '#title' => 'Contact Name',
  6. '#size' => 50,
  7. );
  8. }
  9. ?>

The above code will produce following outputs:

  1. <input type="text" value="" size="50" name="0" />
  2. <input type="text" value="" size="50" name="1" />
  3. <input type="text" value="" size="50" name="2" />

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

  1. <?php
  2. for($i = 0; $i < 3; $i++) {
  3. $form['contact'][$i]['value'] = array(
  4. '#type' => 'textfield',
  5. '#title' => 'Contact Name',
  6. '#size' => 50,
  7. );
  8. }
  9. $form['contact']['#tree'] = TRUE;
  10. ?>

The output will be

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

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
Max length 128 characters
Image CAPTCHA
Enter the characters shown in the image.

About This Mania

Hasan Hafiz, Blogger at web mania ltd (www.webemania.com). He has more than 5 years experience in web development platform. He worked as a Sr. Software Developer at Canadian based outsourcing software firm, Lead Developer at countries top most VOIPswitch development company and Sr. Software Engineer at USA based outsourcing software firm . His expertise areas are PHP, MySQL, CSS, Javascript, JQuery, HTML, XML and MVC Framework Codeigniter. He has been along with Drupal from more than 4 years. You may call him Drupal Addicted.