Helpful Drupal Modules

Its that time again; for nwe post with findings of the week of course. So, I was searching for some modules that could help me with my development and came across few. I believe, if used properly, these can be very helpful modules.

Of course, some of you might not agree so please feel free to share your ideas. I am always eager to learn more and will keep learning through out my career; at least that is what I am supposed to say to portrait me as a humble person.

Well, let’s dive into some of the modules that I think would’ve greatly helped me had I known about these before. So, I decided to post these here in case it helps someone else. 

Disclaimer: I just found out about these modules and wanted to share these before I forgot. So, I haven’t actually used these but thinkthat these will be helpful.

Features: I came across a a podcast which helped me understand how this can be very useful. That podcast not only went through steps on how to use this module but also mentioned other 2 modules that, after reading about those, I found useful (and are listed below). I’d also like to share that podcast as a thank for taking time and help us Drupal developers; http://mustardseedmedia.com/podcast/episode43

Context: Now this is something that I have used a lot; programatically. It would’ve saved a lot of time had I known about this before. But, as they say, better late than never.

Demo: Ever thought that you’d like to revert your site database back to previous state after doing a bit of testing? I have; so imagine my surprise that there is a module for that and I don’t have to do this manually.

Security Review: How often have you thought to see if you can check whether your site is secure or not? I ofte have. So, this module can walk through several known scenarios and generate a report whether there are any security issues with your current installation or not. A nice find that I got from http://drupal.org/node/244924.

Email Registration: Recently, I had to use e-mail address as username. I was thinking of ways I had to get this done; change registration and login form, implement hooks to use e-mail as username and more. But, I found this Email Registration form that does that for you. Following article helped me compare it with Logintoboggan module and made it easier to understand the differences; Using Email as Username. This is the only module that I have already used and it works well.

Well, those are new modules that I learned last week. Hope it can help someone. If not then I’ll continue to look for ideas that can help others because that’s what PHP and Drupal are; Open Source and share with & help others.

Drupal 7: Tweet from Drupal

I wanted to be able to post my from Drupal to Twitter without having to tweet it twice. I found this very nice Twitter module within Drupal to be able to do that. Some instructions are quite simple but it still took me around 2 hours to figure out which setting to use to tweet from Derupal.

Most of these instructions came from Sign in to drupal with your Twitter account page. But there is s very subtle detail that is needed to be able to post to Twitter and most of my time was spent on that; and I fround that option accidentally.

You will need to install OAuth and Twitter modules. I will not go into details as that is very staright forward. You will need to enable OAuthm, Twitter, Twitter Actions and Twitter Post modules; all Twitter modules are included in one Twitter module.

Before moving ahead with any settings, you will need to create an application in Twitter Developer enviroment; you will need consumer key and consumer secret from there. You will need to save these in admin/config/services/twitter. Following 2 steps need to be following before moving ahead:

  1. Give Read and Writeaccess in Application Typein Settings tab
  2. Create Access Tokenin Overview Tab
    1. This step needs to be performed after providing Read and Write access so your application will have write access to post
    2. This will allow this application access to your own account

After you are done creating that app, you will need to provide your site and user access to that application so it can post to your account. For that, you will need to go to users/USERNAME. Edit your account and then go to Twitter Accounts tab. Once there, click on Add Account. This will take you to a Twitter page which will ask you to confirm access to your site to post tweets in the Twitter user account; you will need to be logged in as that user or it will ask you to login as a user before proceeding. Click on Authorize App and it will take you back to your site where the request originated from ; Twitter account will be automatically added to your Twiter account list.

Last change you will need it make is to enable which content type will have Post to Twitter checkbox enabled. For that, you can go to admin/config/services/twitter/post. Check from which content type you want to post to Twitter. Save changes.

Now you are ready to post to Drupal and Twitter at the same time. Once you add/edit content in that that content type, you will have the option of posting to Twitter. By default, its not enabled. So, you will need to enable it for each post. 

This is it. Tweets from Drupal are just one content entry away.

Drupal 7: Custom Module – Part 1

When I first started using Drupal, it was simple matter of installing and using it. Then came the task of learning how to theme. But there came a point where required functionality needed to be coded in Drupal rather than just being able to use the GUI interface. One requirement, that started my path towards learning on creating Drupal modules, was custom front-end page and serving ajax pages. I found creating those pages with a custom module easier than some other ideas. But before being able to create a custom page (which wil be discussed in later tutorial) I needed to create a custom module where I could code custom page module. This tutorial will go through steps on how to create a custom module.

A quick note, I will be going through just the basics steps on how to add module. But wherever apppicable, I will be providing links where you can get detailed information. So, think of this tutorial as well explained reference document with information on each resource and not just the reference.

[b]Requirements[/b]: Drupal 7

To start with, we will assume that the name of this module will bemymodule. So, create a folder called mymodule in sites/all/modules folder. We need at least 2 files for a module to work. You can add as many other files as you need to extend your module but these are only 2 required files for a module to work.

First file is to module information like name, description, version etc. This will be used by Drupal to determine what name, version etc. to show while viewing module list. We will now create [i]mymodule.info[/i] file in mymodule folder. This file will contain following information:

[code]
name = My Custom Module
description = “Custom module to add custom functionality”
version = 7.x-1.x-dev
core = 7.x
project = rattanpal
[/code]

Most of this file is self-explanatory. Every module has a name, description, version and core. More information on these options can be found at http://drupal.org/node/542202. Next and last required file for adding a custom module is [i]mymodule.module[/i] file. This file controls options for module access, custom menu entries, module settings etc. Following is the code that we will put in our myodule.module file; which will be explained shortly.

[code type=”php”]
<?php
/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function mymodule_help($path, $arg) {
switch ($path) {
case "admin/help#mymodule":
return '

‘. t(“Displays Help for MyModule”) .’

‘;
break;
}
}
?>
[/code]

[i]mymodule_help[/i] is Hook to implement Help for your module. You will eventually want to enter more information for users to help them how to use your module. This is explained in detail at http://drupal.org/node/1095546

If you were osave these changed and go to admin/modules at this moment, you will see your module listed in list of modules. At this point, you should be able to enable/install your custom module.

We will continue with this in another tutorial where we will use this module to add a custom page. We can use that page to deliever any type of content (for example, ajax to server json content or a custom front page). We will also discuss how to use permissions to secure that page, if needed.

EDIT: Part 2 of this tutorial is at Drupal 7: Custom Module – Part 2.

Drupal 7: Custom Module – Part 2

This is second and last part in Custom Module series. We will use custom module that we created in last tutorial and create a custom front page for a freshly installed Drupal installation

[b]Requirements:[/b] http://www.rattanpal.com/content/drupal-7-custom-module-part-1.aspx

Create a new template called [i]sites/all/theme/page–front.tpl.php[/i]. This template will override the default template and will be used for any page that we decide to use as front page

Edit [i]mymodule.info[/i]. We will ue this file to add custom page. Following code needs to be added to this file. Comments have been added to all code snippets which describe all the items in each function.

/**
* Implements hook_menu()
*
* This will create a URL that we can use to access this page
* This page then can be accessed using http://www.example.com/home
*
* 'page callback': Function to be called to get content when this page is accessed. Output returned by that function is used for "$content" value on that Drupal page
*
* 'access arguments': It adds a key which will be used by Drupal to store ermissions for this page
*
*/
function mymodule_menu() {
$items['home'] = array(
'title' => 'Home',
'page callback' => 'mymodule_home',
'access arguments' => array('access_mymodule_home_page'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*
* 'access_mymodule_home_page': Its the same access key defined when 'home' menu item was defined in mymodule_menu()
*
* 'title' & 'description': These will appear on Drupal "Permissions" page to describe what these are for
*/
function mymodule_permission() {
return array(
'access_mymodule_home_page' => array(
'title' => t('Access mymodule Home Page'),
'description' => t('Use mymodule Home Page.'),
),
);
}

/**
* Implement home page
*
* Any string value returned by this function will be used by Drupal for "$content" variable as output of that page
* This can be anything. We could even use it to deliver RSS/JSON/JSONP output if needed
*/
function mymodule_home() {
$content = '';
return $content;
}

 

Clear Drupal cache for Drupal to read new changes to this file.

If you haven’t already then enable this module in admin/modules.

Now go to Site Information (admin/config/system/site-information) and add “home” in “Default front page”. This will ensure that this new custom home page will become our front page and that will also make Drupal use page–front.tpl.php as template

Before your front page can be accessible to anonymous users, you need to give permissions to anonymous users to access this new custom page. To allocate permissions, go to User Permissions (admin/people/permissions). Look for “Access mymodule Home Page” (title of the page). Now check permission box for anonymous user and save permissions.

Voila!. You have just changed your site’s front page to be a custom page. You can add your content directly in page–front.tpl.php or in mymodule_home() function.

This concludes our 2 part series on how to add Custom Module and Custom front page.

Drupal 7: Configure Wysiwyg with TinyMCE

Requirements: Drupal 7

Modules: http://drupal.org/project/wysiwyg

This tutorial will walk you through steps on how to configure wysiwyg editor with TinyMCE. You can, of course, use TinyMCE directly but I like this option as I can use any editor (TinyMCE, TinyMCE, YUI Editor) and can change quickly to another if needed. For that, I will need to download that editor and place it in the libraries folder, configure wysiwyg profile (we will get to it soon) and new editor is ready for use.

Now that we have that out of the way, lets get down to installing and configuring wysiwyg.

Install Wysiwyg modules as per instructions.

Download TinyMCE from http://www.tinymce.com/download/download.php  and unzip it in sites/all/libraries/tinymce folder.

Go to admin/config/content/wysiwyg to configure wysiwyg. I used Full HTML input format for this tutorial because this will ensure that any HTML that we use in content is not filtered; feel free to use Filtered HTML if you want to restrict HTML tags in the content.

You will need to select an editor for this profile. Editor drop down should have ” TinyMCE  n.n” listed. If its not listed then make sure you have properly downloaded  TinyMCE  and unzipped it in sites/all/libraries/tinymce folder (not libraries/tinymce/tinymce)). If you want, you can download multiple versions and select different  TinyMCE  version for each profile (although not sure why you would need to do it but feel free).

Save changes.

This should create a profile for Full HTML with the selected  TinyMCE  version.

Click on Edit in operations column after saving  TinyMCE  version for selected profile. There are many options which are self explanatory. This uorial will only describe minimum setup needed to get this up and running. You can test any options as needed.

Expand “Buttons and Plugins”. This should show all the possible button options that Wysiwyg has to offer that you want to use; I had hoped to select all option in newest version but its not available (yet).

Expand “Cleanup and Output”. I prefer to Uun-check “Remove Line Breaks” option as it allows me to view content easily when viewing in Source Code (or HTML) mode. If this is checked then all code will appear on one line and very hard to read.

Expand “CSS”. Here you can provide path to any CSS file from your theme that you want wysiwyg editor to parse and show styles in “Styles” drop down. I, personally, create a special CSS file for styles that I want to display in styles drop down in wysiwyg editor. This way, I only see styles that I want to use in content rather than all the styles that are used through out the site (for example, menu or sidebar styles).  For this to work, you will need to select “Define CSS” in “Editor CSS” drop down. Now enter “CSS Path” to your style sheet like “%b/sites/all/themes/rattanpal/styles/wysiwyg.css” (%b is the root of your site to ensure it uses absolute to path to avoid any path issues).

That is all we need fo basic configuration of wysiwyg editor in Drupal 7. Its time to save your changes.

If you selected Full HTML like me then you will need to go admin/config/content/formats. Drag full HTML so that its first item in all 3 formats. This will ensure that Full HTML is selected by defauly when you add/edit content.