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:
- Drupal 7.x
- Search API Solr Search
- 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;
}
}
Comments
Post a Comment