Drupal Quick Tips

User login

Drupal 7: Reset User 1 Password quick and easy using Drush

There are many solutions out there to reset User 1's password in a Drupal 7 website. A really convenient way is to use Drush, to generate a one time login URL - no mails involved. The output would be directly on the console, and it is simple to just copy and paste it in a browser. 
The drush command is the following:
 
drush php-eval 'echo user_pass_reset_url(user_load(1));
 
and once logged in, the password can be easily changed in the user edit screen. 

Drupal 7: How to remove "More information about formatting options" link

The code below goes in your template.php and removes the "More information about formatting options" link.

<?php
/**
 * Remove the comment filters' tips
 */
function MyThemeName_filter_tips($tips, $long = FALSE, $extra = '') {
  return
'';
}
/**
 * Remove the comment filter's more information tips link
 */
function MyThemeName_filter_tips_more_info () {
  return
'';
}
?>

Please remember to clear the cache in order for the changes to trigger.

Drupal: Send file programmatically

Here is a handy, general function for Drupal 6 that takes as an argument a file id and returns the file for downloading.

Drupal Commerce - "Select a product" element: How to change the select list to radio buttons

Here is the code that changes the default select list of "Select a product" element of Drupal Commerce to radio buttons, for a standalone product list.

The following goes in a custom module (tested in Drupal 7).

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if (
strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0 && isset($form['product_id'])) {
   
$form['product_id']['#type'] = "radios";   
  }
}
?>

Remove duplicate entries from views results... the hard way.

In some cases it is seems impossible to get rid of the duplicate entries that views return in the output.
Not an elegant way, but this small module would do the trick...

So let's create a module the usual way, and put the following code in the .module file.
This one is called remove_duplicates.


<?php
function <MY_MODULE>_views_pre_render(&$view)
{
 
$used_nids = array();

          foreach (
$view->result as $row)
      {
        if (!
in_array($row->nid, $used_nids))
        {
         
$new_view_result[] = $row;
         
$used_nids[] = $row->nid;
        }
?>

How to use datepicker pop up in a form field in a custom module?

Ever needed to use datepicker pop up in a custom module? That would be the most usable way to allow users to choose a date.

Here is the code to be used in the form function:

Drupal: How to add another recipient in personal user contact form

Drupal's core contact module comes with the feature that allows users to have their own personal contact forms.
We were asked to provide a way so that User 1 would receive a copy of any message exchanged between users via the forms.

Our first thought was to use Rules, but it seemed that creating an event for contact form submission was the long way to go.

The solution was quite simple: Using hook_mail_alter.

Reset drupal statistics - The Read counter

Here is a quick and easy way to reset the drupal statistics stored in the database with a couple of SQL queries.

You can reset the Total counters with:

UPDATE `node_counter` set `totalcount` = 0;

And the day counters with:

UPDATE `node_counter` set `daycount` = 0;

Important: You should keep a backup of the database before performing any actions.

Translate <Any> in Views Exposed Filter

While trying to translate the <Any> string in Views, locale returned the error "unsupported HTML".

A quick workaround is to switch from <Any> to -Any-; There is an option available under the Views Tools tab just for that.

The string "-Any-" is translatable, so from this point on it is easy to search for the string "-Any-" in locale search. If it is not found, you would probably need to check that you typed Any in the search form (case sensitive!).

Once found, it is easy to enter the required translation.

Custom submitted by in tpl files

Here is a line of code to override Drupal's submitted by elements in node.tpl.php file.

<span class="submitted">

Pages

Subscribe to Drupal Quick Tips