Thursday, December 31, 2015

Hook Drupal Ajax Function on Webform Submit

(function($) {

    Drupal.subscribeAjax = {
        form_id: 'webform-client-form-34'  // your webform id

    Drupal.behaviors.subscribeAjax = {
        attach: function(context, settings) {
            // Extend Drupal.ajax object
            for (ajax_el in settings.ajax) {
                if (typeof Drupal.ajax[ajax_el] != 'undefined' && Drupal.ajax[ajax_el].element.form) {
                    if (Drupal.ajax[ajax_el] === Drupal.subscribeAjax.form_id) {
                        Drupal.ajax[ajax_el].beforeSend = Drupal.subscribeAjax.beforeSend;
                        Drupal.ajax[ajax_el].success = Drupal.subscribeAjax.success;
                        Drupal.ajax[ajax_el].error = Drupal.subscribeAjax.error;

    Drupal.subscribeAjax.beforeSend = function(first, second) {
        console.log("before submit");, first, second);
    Drupal.subscribeAjax.success = function(response, status) {
        console.log("on success");
        //console.log(JSON.stringify(response));, response, status);
    Drupal.subscribeAjax.error = function(response, uri) {
        console.log("on error");
        Drupal.subscribeAjax.enableForm(this.element.form);, response, uri);

// ajaxComplete
(function($) {
$(document).ajaxComplete(function(event, xhr, options) {
            var data = $.httpData(xhr,options.dataType);
                $('#webform-ajax-wrapper-34 .error').delay(5000).fadeOut(400);

Wednesday, September 9, 2015

Programmatically Creating a new Content Type in Drupal7

To create a module, in the sites/all/modules directory of your Drupal installation create a new directory named mymodule.
 Create following three files in mymodule folder:
·– will contain the information about the module so that Drupal will recognize it and show it in the list.
·         mymodule.install – will have details about things you are going to install or uninstall for the module.
·         mymodule.module – will contain code to implement the Drupal hooks used by the module.
Once you have made the directory, open and add the following code to it:
name = mymodule
description = A new custom content type creation by mujuonly
package = mymodule Custom node Type
version = "7.x-1.12"
core = 7.x
files[] = mymodule.install 
files[] = mymodule.module 
The above simply defines some parameters for our module (mymodule) so Drupal can use it and display the information about  mymodule. Once this is done we should see mymodule in the module list as shown below.

Installing and Uninstalling Content Type
Once we have the basic files for the module in place, we can start writing code for installing the custom node type. The first hook we need to implement is hook_node_info. Using this hook, a module can define one or more node types in Drupal. The hook implementation returns an array defining the new node type which the module is going to add.
To implement this hook, add the following code to mymodule.module
 * Implements hook_node_info()
function mymodule_node_info() {
    return array(
        'news' => array(
            'name' => t('News'),
            'base' => 'news',
            'description' => t('You can add  News here'),
            'has_title' => TRUE,
            'title_label' => t('News title')
The implementation returns an array defining a new node type news  along with some of its properties, such as its name, description, title, and base.
As we have defined that this node type has a title, we need to show the title text field when the form for this node is displayed to add content. To do this, we will have to implement the hook hook_form.
The hook_form is used to show the form for create/edit nodes. The hook is implemented in mymodule.module as follows:
 * Implement hook_form()
function mymodule_form($node, $form_state) {
    return node_content_form($node, $form_state);
We simply use the Drupal API which gives an implementation of hook_form and adds a title field provided the node definition has the has_title attribute set .Once we have got this done, we need to implement the hook_install hook to add the body field to the new node type.
Add the implementation to mymodule.install is as follows:
 * Implements hook_install().
function mymodule_install() {
    $types = node_type_get_types();|
We first save all of the new node types created by different modules by calling the Drupal API node_types_rebuild() function. Then we get all of the node types and call node_add_body_field() on our type to add the body field.
Once we have done this we can enable mymodule which will install our new node type. Then we should be able to see our new type when we click on add content as follows:

If we add a new news, it will also be seen on the Drupal front page.
All modules in Drupal should clean up any data they create in the database or any types they have added when it is uninstalled by the user. To support this, we have to implement the hook_uninstall in ou mymodule.install file as follows:
 * Implements hook_uninstall().
function mymodule_uninstall() {
    $customtype = 'news';
    $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
    $result = db_query($sql, array(':type' => $customtype));
    $nodeids = array();
    foreach ($result as $row) {
        $nodeids[] = $row->nid;
We first find out all of the node IDs which are nodes of our installed content type. Once we have collected the IDs, we use the API function node_delete_multiple() to delete multiple nodes. Then we use the node_type_delete() function to delete our node type.

Now if we uninstall our module, all of the nodes of our type and our type itself should be deleted.

Tuesday, September 8, 2015

Resetting the admin password of Drupal 7

Frequently we come across Drupal sites where admin  password is not known to the owner of the site. You can retrieve the password by changing the email address of user 1 to your email address and then use the forgot password option to reset your password. But if you don't want to change the user 1 email address but instead just want to change the password of user 1 there is an easier alternative

Use below PHP Code in index.php of drupa with URL paramas as  ?pass=PASSWORD and run. After that roll back to old index.php
It Will  set your admin password to PASSWORD

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/';drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
require_once DRUPAL_ROOT . '/includes/';
if (isset($_GET['pass']) && !empty($_GET['pass'])) {
  $newhash user_hash_password($_GET['pass']);
else {
  die('Retry with ?pass=PASSWORD set in the URL');
$updatepass = db_update('users') 
    'pass' => $newhash,//    'name' => 'admin',
//    'mail' => ''
  ->condition('uid', '1', '=')
print "Done. Please delete this file immediately!";drupal_exit();?>

Sunday, September 6, 2015

Simple SEO things with Google Webmaster Tools

Below are some points that makes your website indexed in google search results with help of google webmaster tools

Check Gzip compression enabled in website

Below is an online tool to test if Gzip compression is working on your webpage.


Use Google PageSpeed Insights to check speed of your website.

You can check your website speed here

There is mobile and desktop view checking in one go.

CSS , JS and Image optimization tips are there , if not optimized.

Also Googlechrome plugin is available in chrome store

Drupal 7 reset password programmatically

You can use the _user_mail_notify() function, along with a user object, to invoke the password reset email:
// Load a user (admin in this case).
$account = user_load(1);

// Invoke the email. It will be queued along with other system mail to be sent during cron
_user_mail_notify('password_reset', $account);
A one time login URL with a 24 hour expiry is included by default with that particular email.

Friday, September 4, 2015

Drupal - tune speed and performance

Drupal is really good for a lot of things when it comes to websites. One thing that can sometimes get overlooked or can lag behind (if you don't hire the right team) is the speed of your site. With all of the modules being added and configured, some things are forgotten that help to fine tune speed and performance.
This situation is not uncommon for websites. If you're stuck with a slow site, and think you have to fork over more money to fix it, think again. Before you spend more money on additional server resources or hire a consultant, here are some tips and tricks you can do to help speed up your site. I've done my best to order these by complexity.
  • Drupal Performance - Drupal 7 comes with some pretty good tools for increasing the performance of your site.  Enabling these is always a good first step in increasing your site performance.  You can find these configuration options on the admin/configuration/development/performance page.
    • Page Caching for Anonymous users
    • Block Caching
    • Aggregate/Optimize Stylesheets and Scripts
  • CSS Emimage(link is external) - This is a contributed Drupal module which helps speed the rendering of background images from your site's CSS files.  It's a plug-and-play module that you enable from the modules page and then activate from the performance page (mentioned above) - that's it.
  • Views Cache (If your site isn't views-heavy this won't help you much.) By default, views within Drupal aren't cached. Therefore, these pages (and blocks) are exempted from the cache settings you configured above. This is because views are intended to be dynamic aggregations of content and, therefore, by definition, excluded from cache. However, if you have a complex view, it might be good to have some cache settings on your view to help that page load more quickly.  NOTE: if you enable views caching, make sure you configure the expiration settings so that new content is shown on the view when rendered.