Drupal, Php

Programmatically get Drupal views exposed form

Sometime you may have to get the drupal views exposed filter form programmatically, Let say you implemented the custom search functionality in your site at <your-site.com>/customsearch with views module where you showing listing of different content type with summery and you exposed the filters (e.g simple text box) so that user can filter down the result based upon the search term he type in exposed search box. You may have to show the same exposed search form on site home page so that when user search something on homepage he will be redirected to your custom search page. You can get the views exposed form with following code:

$view = views_get_view('your_views_machine_name');
$view->set_display('Views display name');
$exposed_form = $view->display_handler->get_plugin('exposed_form');
print $exposed_form->render_exposed_form(true);

As code is self explanatory, the  views_get_view function will get your view from database, then set_display method on view object will set the display of your view. init_handlers method will initialise the handlers then with the help of get_plugin method of display_handler you can get the exposed form and render it.


Override Chrome selection background color

Have you ever noticed that some of the  cool well designed website have their selection background color according to theme of the website. For example if a website using green color for most of its design will show the green color when you select text  on website.  Here is a website http://www.impressivewebs.com/ if you select text on this website then it will show light brown color as background of selected text. You can do this with single line of CSS using CSS3 ::selection pseudo class. Put the following line in chrome’s user style custom.css file which is located at ~/Library/Application\ Support/Google/Chrome/Default/User\ StyleSheets/Custom.css  on mac.

::selection {
  background: teal; /* -webkit browser*/


Apachesolr, Drupal, Php

Drupal 7 apache solr search within attached document

Since last couple of week i was working with Drupal and apache solr. There is very good module Apache solr search integration to integrate apache solr with Drupal search. This module provide number of hooks to modify solr search index, alter the document before it being indexed on solr, to alter query parameters, alter the result before it being displayed on result page and several other hooks. Here are my other posts to add custom field in apache solr index and add custom sort to apache solr query parameter.

I had the requirement to search within the drupal content as well as within the attached document with any of the node, if user search for some terms and if that trem exists within the attached document but not within node content itself then result should return that node in search result.

There are existing module apachesolr_file and apachesolr_attachments. Apachesolr attachment module was working quite near to my requirement, As apache solr create one document in index for each drupal node, but this module create separate document  for attached file with any of node. So when i search for some keyword then it return that attached file as separate document not the node with which this file is attached, Then i have to apply some hack over that module to index the attached file content with the node itself not as a separate document.


Before solr index the drupal content i simply alter the document being indexed. I check whether the node being index have file attached with iteself, if file exits then add a new custom field to solr index and using the apachesolr_attachment module’s apachesolr_attachments_get_attachment_text() function grab the text of attached file and add this text to this custom field. Apachesolr attachement module uses tika library to extract the text from attached document.

I created custom module which uses hook_apachesolr_index_document_build() and hook_apachesolr_query_alter(). These two hooks are provided by Apache solr search integration module.

Let say you have a custom file field with node which contain the file(doc, pdf etc). In my case this field is field_download_file. 

function apache_solr_attachments_custom_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
  if ($entity_type == 'node') {
    if(isset($entity->field_download_file)) {
      module_load_include('inc', 'apachesolr_attachments', 'apachesolr_attachments.index');
      $text = apachesolr_attachments_get_attachment_text((object)$entity->field_download_file['und'][0]);
      $document->addField('ts_attachment_text', $text,3);
function apache_solr_attachments_custom_apachesolr_query_alter($query) { 
  $query->addParam('qf', 'ts_attachment_text');

To use apachesolr_attachments_get_attachment_text function you have to install apachesolr_attachement module and enable it.

Apachesolr, Drupal, Php

Drupal 7 Apache solr add custom sort to solr query parameter

Drupal 7 Apache solr add custom field to solr search index was my last post where i described how to add custom field to solr search index. Here i am going to describe how to add custom sort to solr query parameter so that you get result sorted with that field.

To modify the query parameter there is a hook provided by Apache solr search integration module hook_apachesolr_query_prepare() this hook is similar to hook_apachesolr_query_alter() but it is called before this hook.

You can apply sort on the basis of any single value field in apache solr search index like score, create date, update data, sticky etc. Before applying sort you have to make that available as sort field by calling the method setAvailableSort of DrupalSolrQueryInterface and then calling setSolrSort method you can set ascending or descending sort on that field. As here i am going to apply sort based on score field.

function apache_solr_search_facet_block_apachesolr_query_prepare(&amp;$query){
  $query->setAvailableSort('score', array('title' => t('Most Relevant'), 'default' => 'asc'));
  $query->setSolrSort('score', 'desc');

You can also remove the existing sort field with removeAvailableSort method.

For drupal 6 these method were named as set_available_sort() and set_solr_sort(). See the following post  for more detail:


Apachesolr, Drupal, Php

Drupal 7 Apache solr add custom field to solr search index

Drupal 7 Apache solr search integration module is most useful to integrate Apache Solr with drupal. Apache solr search integration module will index all the content of your sites at solr which is full with advance search feature. You can exclude some of content types from being index under setting of this module. When you index your content at solr it will index most of the field of your content type but some of the custom field will not be indexed but Apache solr integration module provides number of hooks which you can use to add custom field to solr index, to alter the query before it sent to apache solr or alter the search result before being displayed.

For Solr Indexing:

You need to use hook_apachesolr_index_document_build. This hook is fairly new in the Solr module (since version 7.x-1.0-beta14) and it replaces the deprecated hook_apachesolr_update_indexhook. Here’s how to use it in your custom module:

function MY_MODULE_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
  $price = $entity->field_price;
  $document->addField('ss_product_price', $price / 100);

Here notice i added the field the name ss_product_price. This is to follow the apache solr naming convention for dynamic field. Here ss means string single field. The first ‘s’ means its string value field, this field will hold only string value, and second ‘s’ means this is single value field. When we will search for some terms then solr will expect to search for single string in this field. If we store text (containing more string) value to this field then search will not work as expected. If the field you are indexing can have more then one value then loop over each value and us sm_ prefix with the field name. If you adding text field then try ts_ and tm_ for text single and text multi value respectively. Drupal Apache solr integration module support to index dynamic fields. For detail read the Dynamic field section in schema.xml shipped with apache solr module.


This tells Solr to add the new field into the set of returned fields.

For Sorl Searching:

Now you’ve got the new field in Solr’s search index. By default this new field doesn’t get returned when you search from Solr and therefore you need to use the hook_apachesolr_query_alter hook.

function MY_MODULE_form_apachesolr_query_alter($query) {
  $query->addParam('fl', 'ss_product_price');

Here i added ‘fl’ which stands for field in addParam function to add parameter to query.

End result:

Using these two hooks you can add custom field to solr index and can query to solr to search in that custom field. These two hooks will only work for Drupal 7. For drupal 6 follow this post.


If adding custom field in solr index works but search within that field doesn’t work then there is chance that you indexed the field as String field and you are trying to search complete text within that field in this case search will not work. You need to index field as text field with ts_ prefix in custom field name and altering the query by adding ‘qf’ parameter in query like this:

  function MY_MODULE_form_apachesolr_query_alter($query) {
    $query->addParam('qf', 'ts_product_price');

Have a look at this post on drupal.org for more clarification.


Loading image through Ajax?

Loading image through ajax! Is it possible? I thought it is possible before i really need to implement it. During some project there was a requirement to load a gif image through ajax. So i just simply tried

$('#popup-container').click(function() {
    type: "GET",
    url: "img/popup_calculating.gif",
    dataType: "image/gif",
    success: function(img) {
      i = new Image();
      i.src = img;
    error: function(error, txtStatus) {

But rather then getting  a image what i get was error !! It was parsererror. It mean browser is making request asynchronously and getting the data but its not able to understand the returned data. Because the dataType supported by ajax are  xml, html, script and json.

So what should i do? I should think how browser load images when i do something like this

<img title="myimage" src="myimage.png" alt="" >

Browser send the request to url specified in src property of img tag and load image asynchronously with image/png specified in contentType header request but in case of ajax it only accept xml, html, script, and json datatype.
So what should i do!! Think?

When browser already loading images asynchronously then why don’t i use that functionality. Just create a img tag on the fly and put the src property to url. Yeah i got the solution. So here is simple jquery plugin to load image asynchronously.

$.fn.image = function(src, f) {
  return this.each(function() {
    var i = new Image();
    i.src = src;
    i.onload = f;

Simply put this code in script tag or in js file. And to load image in a container call the image() function like this

  alert("The image is loaded now");

That’s it!


Changes Are Good

“Chages Are Good !” Really they are damn good.

Changes are good

Changes is good you go first

My life is going to change and i accepted this change. I was doing freelancing since last 3 years and never thought i would join any full time job. I thought i will never fit with job. But the good & bad experiences i had in last year and changes i felt now i ended up with joining a full time job. I am joining a web development company srijan.in in Gurgaon Haryana. Really the changes are not accepted so easily. Our mind resist to changes but once we make our mind & heart to accept changes this change may lead to us where we really want to go.

Definitely this changes are going to good for my life because i have made my mind for this change and have positive approach for this change. Its going to be great to work with new people, learn and share ideas and knowledge. I accepted this change with lots of dream in my eyes hope this change will lead to me there. 😛