Drupal Solr: How to add custom field in solr fields.


This time I am going with Drupal Solr. You can know more about what is solr and how it is suitable for your web site.

Problem: How to add custom field(Article Ownership Text) in solr search index.

Requirement:
  1. Drupal 7.x
  2. Search API Solr Search
  3. Solr server should enabled and running.

Solution:


You can see in above image I have created the Article Index and in field list it shows all fields which is related to Article content type. Now I want my custom code field "show legal ownership text" for every article.

Step 1: Create on custom module solr_field and in solr_field.module file implement hook_entity_property_info_alter.


/**
 * Implements hook_entity_property_info_alter().
 */
function solr_field_entity_property_info_alter(&$info) {
    $info['node']['properties']['legal_text'] = array(
        'type' => 'text',
        'label' => t('Legal Text'),
        'sanitized' => TRUE,
        'getter callback' => 'get_legal_text_for_article',
    );
}

/**
 * Getter callback for get_legal_text_for_article
 */
function get_legal_text_for_article($entity, $options, $name, $entity_type, &$item) {
    if ($entity_type == 'node') {
        $legal_text = "This article is own by ... you should not use this to commercial use.";
        return $legal_text;
    }
}
       
 


Step 2: Now enable module, clear the cache and got to "admin/config/search/search_api/index/article/fields", now you can see in field list our new custom field is there.




Step 3: Select this field and save configuration and that sit.





Comments

Popular posts from this blog

Install PHP 7.x on Linux Mint 18

Install Redis on ubuntu 14.04, 14.10, 15.04, 16.04 etc

Disable Drupal 8 caching during development