function tripal_core_entity_property_info_alter
2.x tripal_core.search.inc | tripal_core_entity_property_info_alter(&$info) |
3.x tripal_core.search.inc | tripal_core_entity_property_info_alter(&$info) |
Implements hook_entity_property_info_alter().
This is where we actually add the properties to the node entity in order to indicate which chado fields should be indexed.
File
- legacy/
tripal_core/ includes/ tripal_core.search.inc, line 33 - Adds support for Drupal indexing of Chado. It's important to note that not all of Chado is indexed but instead Only fields indicated in hook_search_include_chado_fields().
Code
function tripal_core_entity_property_info_alter(&$info) {
// We provide a hook to allow Tripal admin to easily add fields to the search api.
// We want to invoke all implementations of that hook now for use below.
$fields_to_include = module_invoke_all('search_include_chado_fields');
$fields_to_include = array_unique($fields_to_include);
// Retrieve information for all nodes.
// We focus on nodes at this point because we need to link search results back to
// the entity and we have no entites for non-node chado content in Tripal2.
$node_info = module_invoke_all('node_info');
foreach ($node_info as $n) {
// Now keep in mind this hook is defined for ALL THE NODE TYPES and we only want
// to add extra support for chado so we onle care about chado node types.
// We can distinguish chado node types from all others by the existence of
// the 'chado_node_api' key which is used for all sorts of beautiful tripal/chado
// node integration (ie: adding properties, relationships and dbxrefs to node forms).
if (isset($n['chado_node_api'])) {
$schema = chado_get_schema($n['chado_node_api']['base_table']);
// Now we are going to start by adding some defaults. It feels safe to say, we
// probably want to index all the "names" so we are going to look through
// all the fields and if they contain "name" we are going to add them automatically.
foreach ($schema['fields'] as $field_name => $details) {
$machine_name = $n['chado_node_api']['base_table'] . '.' . $field_name;
// Try to create a readable label.
$label = ucwords(str_replace(array('.', '_'), ' ', $machine_name));
// We want to add all name fields and any fields previously indicated to be indexed.
if (preg_match('/name/', $field_name) OR in_array($machine_name, $fields_to_include)) {
if (!isset($info['node']['bundles'][$n['base']]['properties'][$machine_name])) {
$info['node']['bundles'][$n['base']]['properties'][$machine_name] = array(
'label' => $label,
'description' => (isset($details['description'])) ? $details['description'] : '',
'type' => ($details['type'] == 'varchar') ? 'text' : $details['type'],
'schema field' => '[' . $machine_name . ']',
// The following getter callback is a generic function that can retrieve
// values for any chado field.
'getter callback' => 'tripal_search_chado_token_getter_callback'
);
}
}
}
// We want to add any base foreign keys. This allows you to search for all features
// from a given organism. Furthermore, we want to add a single field for each foreign
// key that will span content types in order to be exposed as facets.
foreach ($schema['foreign keys'] as $table => $fk_details) {
foreach ($fk_details['columns'] as $left_field => $right_field) {
$machine_name = $n['chado_node_api']['base_table'] . '.' . $left_field;
$field_details = $schema['fields'][$left_field];
// Try to create a readable label.
$label = $table . ' (' . $machine_name . ')';
if (preg_match('/(\w+)_id/', $left_field, $matches)) {
// Key only field.
$key_label = ucwords(str_replace('_', ' ', $matches[1]));
// Expanded field.
$label = str_replace('_', ' ', $n['chado_node_api']['base_table']);
$label .= ' ' . str_replace('_', ' ', $matches[1]);
$label = ucwords($label);
}
$keytoken = '[BASE.' . $left_field . '>' . $table . '.' . $right_field . ']';
$format = chado_node_get_readable_format($keytoken);
// First, create the key version. This is best used for facets since it
// won't/can't be tokenized along with the other fields. This will be shared
// among node types to facillitate use as a facet.
$info['node']['properties'][$table . '.' . $right_field . ' key'] = array(
'label' => $key_label . ' (All Content Types)',
'description' => (isset($field_details['description'])) ? $field_details['description'] : '',
'type' => 'text',
// We include both the token for the current node type and the token for
// the parent table. That way the organism node will appear in the results
// for the organism key.
'schema field' => $format,
// The following getter callback is a generic function that can retrieve
// values for any chado foreign key.
'getter callback' => 'tripal_search_chado_token_across_nodetypes_getter_callback'
);
$pretoken = '[' . $n['chado_node_api']['base_table'] . '.' . $left_field . '>' . $table . '.' . $right_field . ']';
$format = chado_node_get_readable_format($pretoken);
// Add a more readable version that will be tokenized so users can
// search for fruitfly and get all features with that as an organism.
$info['node']['bundles'][$n['base']]['properties'][$machine_name . ' expanded'] = array(
'label' => $label . ' (Expanded)',
'description' => (isset($field_details['description'])) ? $field_details['description'] : '',
'type' => 'text',
'schema field' => $format,
// The following getter callback is a generic function that can retrieve
// values for any chado foreign key.
'getter callback' => 'tripal_search_chado_token_getter_callback'
);
}
}
}
}
// Provide our own hook for altering properties to make it easier for our users.
drupal_alter('tripal_search_properties', $info);
}