Category Archives: Drupal

How-to for Drupal

Adding Validation to Drupal 7 Contact Form

On your Drupal site, you may have the problem I have found. I wanted to do additional validation of contact form email before allowing the visitor to submit email. Your site may need this to protect against spam, or just to assure you are getting information that you need in the email. How do your add validation to a Drupal contact form?

Solution: You can create a module that will solve your problem, and the process is not complicate.

This post explains the adding of validation to a standard Drupal 7 site wide contact form. The post assumes that you know how to create a basic module, you know where to place custom modules, and the article restricts itself to the actual hooks needed to complete the functionality.

For completeness I will mention the creation of the module info file in passing, without much explanation. Create the file mailFilter.info, containing the following information:



  name = Mail Filter
  description = Offering a filter to your email needs.
  package = Core
  version = VERSION
  core = 7.x
  ; Information added by drupal.org packaging 
  version = "7.0-1.0-DEV"
  datestamp = "1399428226"

Create a file mailFilter.module to contain the actual validation functionality. Adding the actual validation requires 3 steps. The steps are:

  • Look at the form and determine its form_id
  • Use hook_form_alter to include the validation
  • Create the validation function

To find the form_id,

  • Add the following to your mailFilter.module
    
      function mailFilter_form_alter($form, &$form_state, $form_id) {
         echo $form_id ;
      }
    
  • Go to module administration and activate mailFilter
  • Clear your cache
  • Go onto your site and navigate to the form you want to add validated. For each form you pass, the form_id will be printed at the top of your page. When you get to the form you want, you will know the form_id. If you are planning to add validation to the main site wide contact form, you will find the form_id=”contact_site_form.”

Use mailFilter_form_alter to include the validation

Now that you know the form name, alter the mailFilter_form_alter to perform a more useful function. Ask form_alter to add the validation to the list of existing validation. To do that, alter the mailFilter_form_alter as follows:



  function mailFilter_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == "contact_site_form") {
       $form['#validate'][] = 'mailFilter_form_validate' ;
    }
  }    

The site will now want to do additional validate, as defined in mailFilter_form_validate module.

Create the validation function

Now, you must create the validation function. The validation function may do anything you like. In this sample, the validation will look for specific garbage frequently found in spam submitted from websites. When the spam marker is detected, the mail will be rejected. Validation will be included in mailFilter_form_validate as follows:



  function mailFilter_form_validate($form, &$form_state) {
    $msg = $form_state['input']['message'] ;
    $pos = strpos($msg, "[/url]") ;
    if ($pos !== false) {
      form_set_error('message', t('Due to spam problems, you may not include [url] tags in your email.'));
    }
    $pos = strpos($msg, "[/link]") ;
    if ($pos !== false) {
        form_set_error('message', t('Due to spam problems, you may not include [link] tags in your email.'));
    }  
  }

Using the above processing, those automated emails that include reference to “[/url]” or “[/link]” will be rejected by the mail processing. However, you can change this code to anything that helps you validate your input.

Hope this helps you.

Set up A Drupal Events Calendar with Recurring Dates

Do you need an event calendar that may need recurring events? I needed a calendar, installed calendar, loaded a bunch of events, then hit the need for recurring events. OOPS. Not supported. I had to go in, reload the calendar handling and reload the data. Boo. Well, for those of you needing a calendar with recurring events, here is what you need to do.
1. Download the three modules Ctools, Views, & Date. Install and activate the modules. Note: the Date module requires that you set the timezone and first day of the week settings. If you haven’t, it should alert you when you install the modules. Make certain you activate Date components Date Repeat API, Date Repeat Field, and Data Tools. You may also want to activate Date Popup if you want to use a popup calendar to help load dates.
2. Click on Structure > Views, and you’ll see what the Calendar module has done. You can see it has created a “Calendar” view. This calendar view includes a block, a feed, and a page. Don’t use it yet. Don’t run off and use the calendar until you follow the next step.
3. Go to Structure > Date Tools to create a calendar item. It will create a bunch of views, the correct content type, etc. Follow the date wizard to setup your calendar. Make sure you change the Content Type name, the default label “Date,” is just too ambiguous when looking to create a event. I suggest using the name “Event” as the name for the Content Type.

Some recommended settings and things to remember.
• In the “Date Field” section, select the correct option for “show repeating date options”. Changing this later creates problems. Also, decide now whether or not you want repeating dates. Select to allow repeating dates doesn’t hurt and protects you for later.
• In “Advanced Options”, confirm the way you want this calendar to handle the timezone.
• Select “yes” for “create a calendar for this date field”.
• And, you’re ready to go! If you go to “Add Content” you’ll see your brand new “Event” content type.

The calendar tool is fairly basic in this module, but you’ll see it has quite amazing recurrence features, which is nice.

Drupal 7; How to assign custom CSS and JS by node

If you need to have custom CSS or JS for a node, it is easy to set up.   This can be setup via the template.php file.   To customize available CSS or JS via template.php, modify the THEME_preprocess_node.  For example, if you have a special node type called “directory” in a theme called “boulder,” add the following code to your boulder_preprocess_node function.



  function boulder_preprocess_node(&$variables) {
    if ($variables['view_mode'] == 'full') {
      $node =& $variables['node'];
      if ($node->type == 'directory') {
        $path = drupal_get_path('theme', 'boulder');
        drupal_add_css($path . '/CSS/directory.css');
        drupal_add_js($path . '/js/directory.js', 
            array('type' => 'file', 'scope' => 'header'));
      }
    }
  }

I love this. It’s quick, easy and very useful.

Magic: How do you get Drupal 7 Views to expose filters in blocks?

Have you found that you created a view that needs a block with exposed filter, but, the exposed filter don’t show up?? There is magic that makes the “exposed” filter display. Exposed filters automatically display when selected on view pages, but, not on blocks.

Recently I needed to create a view block (via Views 3) with exposed filter. I defined a view page with an exposed filter. It worked great and I moved on to the Block. The exposed filter did not display in the block … What’s going on???? Well, I found how to create the block with exposed filter.

Solution:

To create view block in Views 3 … start fresh

  • Select to create a NEW view
  • Unclick the “Create a Page.” Currently, View 7x-3.7 requires you not create a Page when you require a Block to expose a filter in the block.
  • Click “Create a Block”
  • Define you block, select to expose your filter.
  • Voila!!

Drupal 7: stop insertion of <br/> in formated text

Folks,

If you are using Drupal 7 with full HTML enabled, you may notice that you begin having <br/> tags inserted in your HTML in locations that you do not want these tags. To prevent this, go to:

— admin/config/content/formats/full_html

Under “Enabled filters,” unclick “Convert line breaks into HTML (i.e. <br> and <p>).

Hopefully, this will resolve your problem.