Skip to content

Instantly share code, notes, and snippets.

@mrded
Created November 20, 2013 17:01
Show Gist options
  • Save mrded/7566818 to your computer and use it in GitHub Desktop.
Save mrded/7566818 to your computer and use it in GitHub Desktop.
Drupal: Add custom field to Search API Solr index
<?php
/**
* Implements hook_entity_property_info_alter().
*/
function example_search_api_property_entity_property_info_alter(&$info) {
$info['node']['properties']['created_week_day'] = array(
'type' => 'text',
'label' => t('Week day of node creation'),
'sanitized' => TRUE,
'getter callback' => 'example_search_api_property_weekday_getter_callback',
);
$info['node']['properties']['test_multiple_field'] = array(
'type' => 'list<text>',
'label' => t('Test multiple text'),
'sanitized' => TRUE,
'getter callback' => 'example_search_api_property_random_text_getter_callback',
);
}
/**
* Getter callback for created_week_day property.
*/
function example_search_api_property_weekday_getter_callback($item) {
return format_date($item->created, 'custom', 'D');
}
/**
* Getter callback for multiple field.
*/
function example_search_api_property_random_text_getter_callback($item) {
$strings = array('one', 'two', 'three', 'four', 'five');
$number = rand(1, 5);
$values = array();
while ($number > 0) {
$values[] = $strings[rand(0, 4)];
$number--;
}
return $values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment