Drupal webform/custom form render multiple times on same page

I needed to place the same form multiple times on a drupal page but still be unique so that FAPI would know which one was being submitted.

To do that, first thing we need to render the same form in our function with non-existing form_id as follow:-

————

$form = drupal_get_form(‘webform_client_form_’ . $webform_nid . ‘_’ . $nid, $node);
drupal_render($form);

* it means ‘webform_client_form_’ . $webform_nid . ‘_’ . $nid  form_id does not exists in the Drupal site. 

————

Then we need to define hook_forms function in module.

function hook_forms($form_id, $args) {
$forms = array();
if (strncmp($form_id, ‘webform_client_form_’, 20) === 0) {
$forms[‘webform_client_form_’ . $args[0]->nid] = array(‘callback’ => ‘webform_client_form_’ . $args[0]->nid);
}
return $forms;
}

This is an example for same webform rendering multiple times in same page. It will work for custom form as well. It will also do the trick to track which form is submitted.  It will do validate only the form where button is clicked.

Advertisements

,

Leave a comment

Creating custom filters with drupal and views

If you want to know how to create custom filters with Drupal and view please follow the step from the link

http://www.chadcf.com/blog/creating-custom-filters-drupal-and-views

But you need to remember some missing information :-

it is done in views api 2 , so function definition you need to change. for example

$this->query->add_where($this->options[‘group’], “term_node.tid = %d”, $value); should be

$this->query->add_where($this->options[‘group’], “term_node.tid”, $value,’=’);

ofcourse remember to add those files inside module.info file, otherwise you will see broken handlers in your view.

Leave a comment

[SOLVED] [Drupal Webform] Altering a Webform’s submit button to an image causes it to stop working

[SOLVED] [Drupal Webform] Altering a Webform’s submit button to an image causes it to stop working

Styling a Webform is more or less like styling any other ordinary HTML form. Often you would like to change the style of the submit button and replace it with an image via code in your Drupal website. Although this is extremely easy it might cause the button to stop working, forcing it to reload and fail to show the confirmation message. If you are facing this problem while styling your Webform in your Drupal website read on to find the solution.  

,

Leave a comment

Call to undefined function entity_revision_is_default() on line 306 of field_collection.module

When i updated field collection module to beta7 to get support of version control , I start getting this error. It because of entity api module version.

Updated entity api module to 1.0 version solve the issue right away.

 

ref : https://drupal.org/node/1883924

Leave a comment

Drupal Search API configuration with views

With Drupal 7 there is now the search API. Apart from the existing database (that uses the same DB as drupal to index with) and Solr integration there are a few other backend options available too. As the open source community is quite prolific, a few supporting modules to allow fuzzy searching, multi index searching, saved searches, autocompletion, attachment indexing and more have sprung up. See the Search API project page for more detail.

When we started working on the Kamusi project – a dictionary for african languages – early in 2012, we decided to aim lower at first and set up plain old database indexing. The joy being that when the userbase, database of dictionary terms and thus the load increases, we can simply plug in a new server. Like Solr. Joy!

Here’s how we set up the database indexing:

  1. Download and Install the Search API module. It requires the Entity API module as well as Views to build your search results with – That’s right, no more hacking the Search results page to get pretty! To be able to use your database as an index, also download and install Database search.
  2. Enable the Search API, Database Search and Search views modules.
  3. Go to admin/config/search/search_api. This is where your search servers and indexes are listed. The servers dictate where and how indexes reside, while indexes are the where the actual search data reside. We decided to create two indexes: one for word definition, and another for translation. We keep seperate nodes for each translation of a word, as Drupal multi-language is still a bit of a battlefield.
  4. Click “Add server”. Enter a name for your server (“Default database” is as good as any, I guess). Select “Database service” as the Service class and click “Create server”.
  5. Go back to admin/config/search/search_api and click “Add index”. Enter the name of the index (Eg. “Definition index”) and select the entity type you would like to index. Node is generally a good bet. Select the server we’ve created in the previous step, and leave the rest as is. If you have a large dataset, only enable “Index items immediately” after you’ve got your first set of data manually indexed. Set the chron batch size, careful not to make it too big and crash your cron runs (about 1000). Rather leave it at 50, for now. Click “Create index”.
  6. Select the fields you would like to index. You can index other details of a field by using the “Add related fields” fieldset below the form, particularly useful if you are using relations, or node references. Click “Save”.
  7. Index your existing data by running cron (in our case, a LOT), or on admin/config/search/search_api click edit>status and clicking “Index now” a few times (in our case, a LOT). Edit your index and enable “Index items immediately”.
  8. Go to admin/structure/views/add and create your search view (You know the drill) by selecting your index in the “Show” dropdown.
  9. This view will now show everything that is in your index. Add filter criteria to the view and expose some to the user. This this will become your search form.
  10. Add a page display so that your users may view search results. Edit the exposed form settings and change your submit button text. You may add a search block to your site by setting “Expose form in block” to “yes” under the exposed form settings.

Joy! Hopefully you will now have a pretty search form that will serve your users with pretty and relevant results!

Leave a comment

Disable form validation on ajax call in Drupal form

If you use multiple submit button in Drupal from and add ajax call to submit button then you ajax submit button will execute form default validate and submit function. To prevent it from calling those default function you just need to do two following steps.

1) rename my validation function from “myformname_validate” to “myformname_do_validate”. This caused my validate function to not be called during the ajax request.

2) attach the “myformname_do_validate” function to my submit button like so:

<?php
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Submit’),
‘#validate’ => array(‘myformname__do_validate’),
);
?>

, ,

Leave a comment

Saving node’s fields without saving the node itself

Drupal core function node_save() is used to save nodes and it is frequently used to programmatically alter and save nodes’ data, which includes fields (aka CCK fields) as well. If you want to update a field of a node you could do it like this,

<?php
$node = node_load($nid);
$node->field_fieldname[LANGUAGE_NONE][0][‘value’] = ‘some value’;
node_save($node);
?>

However, there may be a case when you might want to save a node’s field, but not the node itself. For example, you may not want to update the node’s timestamp for the last update, because you want it to be changed by human user activity only. In another case, you may not want to call node_save() and thus invoke chain reactions such as email notifications when the node is updated. These two cases are what actually happened to me.

A Drupal function field_attach_update() may save your day if you encountered a similar problem that I had. You could actually replace node_save() with field_attach_update(),

<?php
$node = node_load($nid);
$node->field_fieldname[LANGUAGE_NONE][0][‘value’] = ‘some value’;
field_attach_update(‘node’, $node);
?>

,

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

How to disable views programmatically when the offering module is disabled

Here is a snippet of code you would put in mymodule.install

/**
* Disable workbench views
*/
function mymodule_update_7001() {
// a list of views (their view name) I want to disable
$viewnames = array(
‘workbench_current_user’,
‘workbench_edited’,
);
// grab list of views that are already disabled
$views_status = variable_get(‘views_defaults’, array());
// add our views to be disabled to the list
foreach ($viewnames as $key => $viewname) {
$views_status[$viewname] = TRUE;
}
// reset the variable with the new list
variable_set(‘views_defaults’, $views_status);
// empty cache now
if (function_exists(‘views_invalidate_cache’)) {
views_invalidate_cache();
}
}

Leave a comment

How To Disable Custom Panel Page Programmatically?

You can automatically disabled custom panel page  via hook_update_N.  After little digging around in CTools module codebase, I ended up with the following code snippet.

$page = page_manager_get_page_cache('<page_name>'); // Replace <page_name> 
$function = ctools_plugin_get_function($page->subtask, 'enable callback');
$result = $function($page, TRUE);
menu_rebuild();

6 Comments