Archive for category Drupal

Content_connected module in drupal.org

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).

Advertisements

, ,

Leave a comment

Similarterms module in drupal.org

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.

https://www.drupal.org/project/similarterms

, , ,

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);

Ref:- http://drupal.stackexchange.com/questions/29113/how-do-i-make-form-set-error-affect-the-submitted-form-only

,

Leave a comment

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.

,

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

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