drupal quick tips

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 remove_duplicates_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:


$format = 'd-m-y';
  $form['date_from'] = array(
    '#title' => t('Date From'),
    '#type' => 'date_popup',
    '#date_format' => $format,
    '#description' => t('Please select a date'),
  );

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.

Another suggestion is to use the Statistics Advanced Settings module that comes with this setting.

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">

<?php print format_date($node->created, 'custom', "F d, Y");  print ' | ' . theme('username', $node); print ' | ' . $node->comment_count . ' Comments'; ?>

</span>

 

Draw circle overlay in all points in gmap from Radius in a CCK field | Dynamic gmap Macro in Views

Lately I needed to add a circle in every marker in a gmap provided by Views.

So in order to achieve this, I filled in the Macro textarea of the Style:Gmap settings of the View with the following code. But first, I added the field that contains the Radius value and excluded it from Display, to make the value available in the view result set. This value was in miles, so I made the conversion from km as well. 

<?php

$circles = array();

$view = views_get_current_view();

foreach ($view->result as $result) {

Show taxonomy terms Grouped By Taxonomy Parent

Lately I was assigned with the task to display all terms assigned to a node, grouped by their parent. To better explain this, let's say for example that we have the Vocabulary 1 assigned to Content Type A, with the following terms:
 
Parent 1
-- term 1
-- term 2
-- term 3
Parent 2
-- term 4
-- term 5
Parent 3
-- term 6
-- term 7
etc. 
 

Show Blocks only on user's pages and not on login, edit etc. pages

If we need to display a block in user pages, and not in /user/login or /user/register etc pages, we need to field in some custom php code under the Page specific visibility settings, we select the radio "Show if the following PHP code returns TRUE (PHP-mode, experts only)" and we fill in the following code:

PHP Fatal error: Class 'Console_Table' not found in... drush error

After a fresh drush installation, drush status returned this error message:

Syndicate content