Posts Tagged Drupal7

Content_connected module in

Content connected is a lightweight module which allow administer to know whether content is connected with some other content before it is about to be deleted from the system. So administer should know whether content should be deleted or not.

Content connected is also added as sub tab in node pages to view content connection with other contents. This can be restricted by permissions for different roles.

Content connection based on entity reference field and any textarea/long field for any content type.

Connected content is listed in a table where it can be identified how it is connected(Reference field or textarea/long field).

, ,

Leave a comment

Similarterms module in

This Drupal module attempts to provide context for content items by displaying a view block with links to other similar content. Similarity is based on the taxonomy terms assigned to content. Views are available based on similarity within each of the defined vocabularies for a site as well as similarity within all vocabularies.

What does this really mean? How should you use this module?

1) Create a freetagging vocabulary called “Tags” assigned to the content types on which you’d like to display the “Similar” view block.
2) Enable the view block called “Similar entries from the Tags vocabulary”.
3) Add keyword tags to the content.

, , ,

Leave a comment

Programmatically create user account in drupal 7

It is the clean and tested code for programmatically create a user account in drupal system. Anyone can use this code according to their requirement and fields.

////// Creating user //////
$password = user_password(8);
$roles = user_roles(TRUE);
$your_rid = array_search(‘YOUR OTHER ROLE’, $roles);
//set up the user fields
$fields = array(
‘name’ => $node->field_mail[‘und’][0][’email’],
‘mail’ => $node->field_mail[‘und’][0][’email’],
‘pass’ => $password,
‘status’ => 1,
‘roles’ => array(
DRUPAL_AUTHENTICATED_RID => ‘authenticated user’,
$your_rid => ‘YOUR OTHER ROLE’,
‘field_first_name’ => array(
‘und’ => array(
0 => array(
‘value’ => $node->field_first_name[‘und’][0][‘value’]
‘field_last_name’ => array(
‘und’ => array(
0 => array(
‘value’ => $node->field_last_name[‘und’][0][‘value’],
‘field_telephone’ => array(
‘und’ => array(
0 => array(
‘value’ => $node->field_telephone[‘und’][0][‘value’],
// First parameter is left blank so a new user is created.
$account = user_save(NULL, $fields);


Leave a comment

SOLVED!! How can you make form_set_error() affect the only form submitted?

Drupal form_set_error() function provide validation message based field name. So if you have same field name in same page , after the submission you can see both fields are marked as red color(Error class). It validate or show message for right form but it provide error class for all element which are same.

I found a way to solve it. If you render those form using drupal_get_form then you can add one extra line in top to get rid of this problem.

for example :-

drupal_static_reset('form_set_error'); // adding this line makes you life out of hell.-)
$form = drupal_get_form('webform_client_form', $node, $submission);
return drupal_render($form);



Leave a comment

Floating a textfield next to a radio button in Drupal form

This can actually be done entirely with the Forms API. Here is an example:


// This will mimic the normal form element title as well as provide the group for our radio options.
$form[‘nations’] = array(
‘#type’ => ‘item’,
‘#title’ => t(‘Nationality’),
‘#description’ => t(‘<Enter your field description here.>’)
$form[‘nations’][‘american’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘American’),
‘#default_value’ => 1,                    // If there is a default value, this should also be specified for each radio button.
‘#return_value’ => 1,
‘#parents’ => array(‘nations’),           // You must specify this for each radio button for them to act as a group.
‘#prefix’ => ‘<div>’  // The first radio button needs to make the style match a normal radios group.
$form[‘nations’][‘german’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘German’),
‘#return_value’ => 2,
‘#default_value’ => 1,
‘#parents’ => array(‘nations’)
$form[‘nations’][‘other’] = array(
// The ‘container-inline’ class places elements next to each other, while the ‘form-item’ class provides the correct spacing between options.
‘#prefix’ => ‘<div>’,
‘#suffix’ => ‘</div>’
// By supplying the title here, instead of using the ‘#field_prefix’ property of the textfield, clicking the text will also select the radio button.
$form[‘nations’][‘other’][‘other_option’] = array(
‘#type’ => ‘radio’,
‘#title’ => t(‘Other:’),
‘#return_value’ => 3,
‘#default_value’ => 1,
‘#parents’ => array(‘nations’)
$form[‘nations’][‘other’][‘other_textfield’] = array(
‘#type’ => ‘textfield’,
‘#default_value’ => ”,
‘#size’ => 20,         // The default size is a bit large…
‘#suffix’ => ‘</div>’  // End of the “form-radios” style.

In your validation and/or submit callbacks, the selected radio option will then be in $form_state['values']['nations'] and the textfield value will be in $form_state['values']['other_textfield'].

With a little more work, you can change the example so that you don’t have to create each element manually .-)

, ,

Leave a comment

Solution – How to work Ajax property in view exposed filter in drupal 7

It takes a lot of time to find out what is exact reason views exposed filter does not work with #ajax property in form element. In order for Ajax to work, we need the form build info where we check i #ajax has been added to any form elements, and if so, pass this info as settings via Javascript, which get attached to the submitted form on Ajax form submission.

Sample code in your .module file for Drupal 7 as follow :-

* Implements hook_form_views_exposed_form_alter().

function MYMODULE_form_views_exposed_form_alter(&$form, $form_state) {
$form += MYMODULE_view_enable_ajax($form, $form_state);

/* * *
* functions for enable ajax call for exposed form elements in views.

function MYMODULE_view_enable_ajax($form, $form_state) {
// Since the exposed form is a GET form, we don’t want it to send a wide
// variety of information.
$form[‘form_build_id’][‘#access’] = FALSE;
$form[‘form_token’][‘#access’] = FALSE;
$form[‘form_id’][‘#access’] = FALSE;

// In order for Ajax to work, we need the form build info. Here we check if
// #ajax has been added to any form elements, and if so, pass this info as
// settings via Javascript, which get attached to the submitted form on Ajax
// form submissions.
foreach (element_children($form) as $key) {
if (isset($form[$key][‘#ajax’])) {
$form[‘#attached’][‘js’][] = array(
‘type’ => ‘setting’,
‘data’ => array(
‘exposed_form_info’ => array(
‘form_id’ => $form[‘#form_id’],
‘form_build_id’ => $form[‘#build_id’],
‘form_token’ => $form[‘#token’],
$form[‘#attached’][‘js’][] = array(
‘type’ => ‘inline’,
‘weight’ => 100,
‘data’ => ‘(function ($) {
Drupal.behaviors.ViewsExposedFormAjax = {
attach: function(context, settings) {
for (ajax_object in Drupal.ajax) {
if (Drupal.ajax[ajax_object].options) {
jQuery.extend(Drupal.ajax[ajax_object], Drupal.settings.exposed_form_info);

return $form;



Create Custom Search Criteria Page in Drupal7

function my_module_menu(){
$items = array();

$items[‘my_search_page’] = array(
‘title’ => t(‘my search screen’),
‘description’ => ‘when the present views filter can not suit your need such as dependent dropdown’,
‘page callback’ => ‘drupal_get_form’,
‘page arguments’ => array(‘my_search_page’),
‘access callback’ => TRUE,
‘type’ => MENU_CALLBACK,
‘access arguments’ => array(‘access content’)

return $items;

function my_search_page($form, &$form_state) {

if (!isset($form_state[‘storage’][‘my_module’][‘search_criteria’])) {
return drupal_get_form(‘my_search_form’);

$build = array();
$rows = array();

$header = array(
array(‘data’ => ‘Title’, ‘field’ => ‘title’, ‘sort’ => ‘asc’),
array(‘data’ => ‘Node ID’, ‘field’ => ‘nid’),
array(‘data’ => ‘Type’, ‘field’ => ‘type’),
array(‘data’ => ‘Created’, ‘field’ => ‘created’),
array(‘data’ => ‘Published’),

$title = $form_state[‘input’][‘title’];

$query = db_select(‘node’, ‘n’)
->condition(‘status’, 1) //Only published nodes, change condition as it suits you
->condition(‘title’, $title.’%’, ‘LIKE’)
->extend(‘PagerDefault’) //Pager Extender
->limit(10) //10 results per page
->extend(‘TableSort’) //Sorting Extender
->orderByHeader($header) //Field to sort on is picked from $header
->fields (‘n’, array (

$results = $query->execute();

foreach ($results as $node) {
$rows[] = array(
l($node->title, ‘node/’. $node->nid),

//Theme the html table:–
$content = theme(‘table’,
‘header’ => $header,
‘rows’ => $rows,
‘sticky’ => TRUE, //Optional to indicate whether the table headers should be sticky
’empty’ => ‘Find not found..’, //Optional empty text for the table if resultset is empty

$build[‘content’] = array(
‘dummy1’ => my_search_form($form, $form_state),
‘dummy2’ => array(‘#markup’ => $content,),

$build[‘pager’] = array(
‘#theme’ => ‘pager’,
‘#weight’ => 5,

return $build;

function my_search_form($form, &$form_state) {

$form[‘criteria’] = array(
‘#type’ => ‘fieldset’,
‘#title’ => ‘Search Criteria’,
‘#attributes’ => array(‘class’ => array(‘container-inline’)),

$form[‘criteria’][‘title’] = array(
‘#type’ => ‘textfield’,
‘#title’ => t(‘title’),
‘#value’ => (isset($form_state[‘input’][‘title’])) ? $form_state[‘input’][‘title’] : “”,
‘#size’ => 10,
‘#maxlength’ => 20,

$form[‘criteria’][‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => ‘search’,
‘#submit’ => array(‘my_search_form_submit’),

return $form;

function my_search_form_submit($form, &$form_state) {
$form_state[‘storage’][‘my_module’][‘search_criteria’] = $form_state[‘values’];
$form_state[‘rebuild’] = TRUE;

Ref :


Leave a comment