Drupal

Drupal 7 node title show error when enter special characters

If you don’t want to allow special character in node title of Drupal node then you can do this with one simple hook hook_node_validate. This hook will be called when you create a new node or you update any existing hook.

function custom_node_validate($node, $form, $form_state){
 // special character list, Add more special character if you want.
 $special_character_list = '\'^£$%&*()}{@#~?>,|=_+¬\-\[\]';
 if(preg_match('/[' . $special_character_list . ']/', $node->title, $matches) !== 0){
 form_set_error('title', format_string("Special characters !special_character_list are not allowed in title.", array('!special_character_list' => $special_character_list)));
 }
}
Standard
Linux, Php, Tools

Make any directory as web server root directory

In your local system temporarily you can make any directory as localhost web server root directory. If you have some static web application or static pages and you want to test. One option is you copy that web application folder in your localhost root directory then browse that directory through localhost. Another thing you can do is make that directory as web server root directory temporarily. You can do this in two ways one with PHP build in server and another with python. For PHP you should have php 5.4 or higher.

With PHP


php -S localhost:<port number>

Here -S tell php to start builtin server

example :


php -S localhost:9000

Now the current directory in which you run this command will become the web server root directory and web server will listen at 9000 port.

With Python:


python -m SimpleHTTPServer 9000

here -m tell python to import SimpleHTTPServer module.

Now the current directory in which you run this command will become the web server root directory and web server will listen at 9000 port.

Of course another way to do this is changing the configuration in apache’s httpd.conf file.

I think Ruby also provide builtin server for development, i am not aware about that. Please leave in comments if you know some another languages also provide builtin server.

Standard
Linux, Tools

Grab a static website with Wget linux command

You can get any static websites on your local system with wget linux command. Let say there is simple static web application which you want to run on your local system. One option could be that you save each page and every script and css files embedded on each page as well of that application, and then you have to correct the path of the script and css files if the paths are not relatives. Wget can do this for you. For example: css3shapes.hertzen.com is very good static web application which create awesome css shapes just using drag and drops. If you want to run this application in your local system, in case you don’t have internet access. Using the following wget command you can get this application running on your local system.


wget --recursive --no-clobber --page-requisites --convert-links --no-parent http://css3shapes.hertzen.com/
 

you can pass several option to wget command. The options are:


--recursive: download the entire Web site.

--domains website.org: don't follow links outside website.org.

--no-parent: don't follow links outside the directory tutorials/html/.

--page-requisites: get all the elements that compose the page (images, CSS and so on).

--html-extension: save files with the .html extension.

--convert-links: convert links so that they work locally, off-line.

--restrict-file-names=windows: modify filenames so that they will work in Windows as well.

--no-clobber: don't overwrite any existing files (used in case the download is interrupted and
resumed).

 

Thats all now you can open index.html file in browser and everything works, in case if this doesn’t work as expected then you can put this grabbed website directory in your localhost document root.

Standard
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:

<?php
$view = views_get_view('your_views_machine_name');
$view->set_display('Views display name');
$view->init_handlers();
$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.

Standard
CSS3

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*/
}

 

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

Approach:

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. 

<?php
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.

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

<?php
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:

http://drupal.org/node/715276

Standard