Drupal

drupal console can’t connect to MAMP mysql server

Do you ever encountered an error while running drupal command inside drupal root directory where you using MAMP as your development stack?

$ drupal
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory

OR while trying to connect mysql client with mysql server run by MAMP or MAMP Pro?

$ mysql -u root -ppassword
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This is because drupal console or mysql client try to connect to mysql server by default location of mysql socket file which is supposed to exist at /tmp/mysql.sock

But mysql server run by MAMP create this socket file at /Applications/MAMP/tmp/mysql/mysql.sock location which is different from default expected mysql socket file

Solution:

Simply create a symlink to this mysql socket file under /tmp/ directory.

$ ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

BOOM! now you can run drupal console or mysql command.

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

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

Image

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.

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

Update

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.

Standard