Category Archives: Drupal Modules

How-to for Drupal Modules

Drupal 7: Display Suites, Helpful for Custom Display of User Account Page

Perhaps you want to customize the User Account Page. You have multiple options. You can create custom user-profile.tpl.php file and link it into your site with complete control. Or you can have a bit less control and use the Display Suite Module and CSS.

If you need complete control of the content of the user Account Page, create of custom user-profile.tpl.php file, add it to your template directory and add code to your template.php to use the new user-profile.tpl.php file.

However, if your needs are simply to move things around in a minor way and hide or display some content from the Accounts Page, you may prefer to load Display Suite, select to adjust the user accounts page and make high level adjustments.

Once you use Display Suite, you may even find you really like using it to help doing adjustments and helping for format other pages.

Drupal 7: menu token and me Alias are not working

Currently, modules Menu Token and me Alias are not working (8/1/2014). There may be a way to get them to work … each has lots of notes around the internet that indicates they are working on & off, with lots of proposed workarounds. Trying workaround after workaround, I could not get them to work consistently or conveniently. Trying to use either, insert tokens in a menu is a problem. I have a solution. The solution is not sophisticated or convenient, but, it appears to work consistently. Hopefully, it will work consistently until the menu token or me Alias modules work consistently.

Unfortunately, the work around requires:

  • Some familiarity with php
  • Faking out the menu validation
  • use of hook_url_outbound_alter

To insert the current user_id tokens into the path in a menus:

  • Step 1: Realize you really want the path “user/uid/history”
  • Step 2: Create a basic content page with url of “user/uid/history”
  • Step 3: Select the menu you would like to add this entry too, add the menu label, path user/uid/history and save, the entry will be allowed because their is a valid page defined at user/uid/history.
  • Step 4: Before leaving the menu, check the actual node id that is automatically inserted in place of path you specified (ex. node/13 might be the real address)
  • Step 5: In the template.php, create the THEME_url_outbound_alter function as follows:
    
     
      function THEME_url_outbound_alter(&$path, &$options, $original_path) {
        if ($path == "node/13") {     // check $path == 
                                      //       the dummy address you created
          global $user;               // prepare to get the token uid value
                                      // build the $path with the current 
                                      // user uid 
          $path = 'user/' . $user->uid . "/history" ;
                                      // Set $options['alias'] = path you 
                                      // want accessed
          $options['alias'] = $path;  
        }
        // repeat the previous path redefine for all pages you would like
        // to include the token uid.   Other tokens may be dealt with in 
        // a similar manner. 
      
      }   
    
    

Drupal 7: Secret to using hook_menu_alter

Are you trying to use hook_menu_alter in Drupal 7 and finding that hook_menu_alter is not appearing to have any effect? There is a reason. Hook_menu_alter is not called every time you run your site. Hook_menu_alter only runs when the menu module is initialized or a menu is initially constructed. If you have an existing menu and you would like hook_menu_alter to affect that menu, you must do a menu rebuild. In theory, there are two trivial ways to do this. However, the 2 trivial methods may not work for you. The trivial methods are:

  • Use Devel module option to request menu rebuild
  • Use drush to request menu rebuild

However, these 2 methods may not be convenient for you. I know that right now (8/2/2014), Devel will not run correctly on my install because there appears to be problem with the current combination of devel, devel_theme & simplehtmldom that is currently available seem to crash my install. At the same time, drush is inconvenient for the install I have available. My solution involved creation of a simple file that will force the menu rebuild.

To do a menu rebuild programattically:

  • Create a file in your site’s base directory, perhaps menuRebuild.php
  • Include the following code in the new file
    
      <?php
      
        chdir("< your-root-directory >") ;
        define("DRUPAL_ROOT", getcwd());
        require_once("./includes/bootstrap.inc") ;
        drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL) ;
        // Rebuild
        menu_rebuild() ;
        echo "rebuilt" ;
    
      ?>
    
    
  • run the file … the menu will be built.

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.