Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cwrc#4: Enable Solr in place of the RI query #5

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 177 additions & 1 deletion islandora_object_field.module
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,30 @@ function islandora_object_field_field_is_empty($item, $field) {
* Implements hook_field_formatter_info().
*/
function islandora_object_field_field_formatter_info() {
return array(
$formatters = array(
'islandora_object_field_label_formatter' => array(
'label' => t('Object label'),
'field types' => array('islandora_object_field'),
),
);
if (module_exists('islandora_solr')) {
$formatters['islandora_object_field_solr_formatter'] = array(
'label' => t('Solr search'),
'field types' => array('islandora_object_field'),
);
$formatters['islandora_object_field_solrmetadata_byo_formatter'] = array(
'label' => t('Solr metadata (named association)'),
'field types' => array('islandora_object_field'),
'settings' => array(
'metadata_configuration' => FALSE,
),
);
$formatters['islandora_object_field_solrmetadata_formatter'] = array(
'label' => t('Solr metadata (default association)'),
'field types' => array('islandora_object_field'),
);
}
return $formatters;
}

/**
Expand All @@ -150,6 +168,9 @@ function islandora_object_field_field_formatter_view($entity_type, $entity, $fie

switch ($display['type']) {
case 'islandora_object_field_label_formatter':
case 'islandora_object_field_solr_formatter':
case 'islandora_object_field_solrmetadata_formatter':
case 'islandora_object_field_solrmetadata_byo_formatter':
foreach ($items as $delta => $item) {
try {
$object = $repository->getObject($item['pid']);
Expand All @@ -159,6 +180,31 @@ function islandora_object_field_field_formatter_view($entity_type, $entity, $fie
'#pid' => $item['pid'],
'#label' => $object->label,
);
// Solr formatter adds a rendered Solr search result
if ($display['type'] == 'islandora_object_field_solr_formatter' && module_exists('islandora_solr')) {
drupal_load('module', 'islandora_solr');
$query = format_string('!field:!value', array('!field' => 'PID', '!value' => islandora_solr_lesser_escape($item['pid'])));
$qp = new IslandoraSolrQueryProcessor();
$qp->buildQuery($query);
$qp->executeQuery();
$results = new IslandoraSolrResults();
$solr = $results->printResults($qp->islandoraSolrResult);
$element[$delta]['#solr'] = $solr;
}
// Solr metadata formatter adds a rendered Solr metadata display
if ($display['type'] == 'islandora_object_field_solrmetadata_formatter' && module_exists('islandora_solr')) {
$settings = $display['settings'];
drupal_load('module', 'islandora_solr_metadata');
$solr = islandora_solr_metadata_display_callback($object, TRUE);
$element[$delta]['#solr'] = $solr;
}
// Solr metadata BYO formatter adds a rendered Solr metadata display, but independent of cmodel associations
if ($display['type'] == 'islandora_object_field_solrmetadata_byo_formatter' && module_exists('islandora_solr')) {
$settings = $display['settings'];
drupal_load('module', 'islandora_solr_metadata');
$solr = islandora_object_field_solr_metadata_display_override($object, $settings['metadata_configuration']);
$element[$delta]['#solr'] = $solr;
}
}
catch (Exception $e) {
// This is likely a permissions issue, continue on.
Expand All @@ -170,6 +216,91 @@ function islandora_object_field_field_formatter_view($entity_type, $entity, $fie
return $element;
}

/**
* Metadata display callback for rendering metadata from Solr.
* @see islandora_solr_metadata_description_callback
*
* @param AbstractObject $object
* An AbstractObject representing an object within Fedora.
* @param string $metadata_configuration_name
* The machine name of the selected metadata configuration
*
* @return string
* Markup representing the metadata display pulled from Solr.
*/
function islandora_object_field_solr_metadata_display_override(AbstractObject $object, $metadata_configuration_name) {
module_load_include('inc', 'islandora_solr_metadata', 'includes/db');
$metadata_configuration_id = null;
$associations = islandora_solr_metadata_get_associations();
foreach ($associations as $a) {
if ($a['machine_name'] === $metadata_configuration_name) {
$metadata_configuration_id = $a['id'];
}
}
$elements = array(
'islandora_object' => $object,
'print' => TRUE,
'associations' => array($metadata_configuration_id => array('configuration_id' => $metadata_configuration_id, 'cmodel' => '*')),
);
drupal_alter('islandora_solr_metadata_display_elements', $elements);
if (count($elements['associations']) > 0) {
return theme('islandora_solr_metadata_display', $elements);
}
else {
return FALSE;
}
}

/**
* Implements hook_field_formatter_settings_summary().
*/
function islandora_object_field_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];

$summary = array();
if ($display['type'] == 'islandora_object_field_solrmetadata_byo_formatter') {
module_load_include('inc', 'islandora_solr_metadata', 'includes/db');
$value = t('Broken/Missing handler');
if ($settings['metadata_configuration']) {
$associations = islandora_solr_metadata_get_associations();
foreach ($associations as $a) {
if ($a['machine_name'] === $settings['metadata_configuration']) {
$value = $a['name'];
}
}
}
$summary[] = $settings['metadata_configuration'] ? t('Solr Metadata Configuration: !c', array('!c' => $value)) : t('No Solr Metadata Configuration selected');
}

return implode('<br />', $summary);
}

/**
* Implements hook_field_formatter_settings_form().
*/
function islandora_object_field_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];

if ($display['type'] == 'islandora_object_field_solrmetadata_byo_formatter') {
module_load_include('inc', 'islandora_solr_metadata', 'includes/db');
$associations = islandora_solr_metadata_get_associations();
$options = array('' => '');
foreach ($associations as $a) {
$options[$a['machine_name']] = $a['name'];
}
$element['metadata_configuration'] = array(
'#title' => t('Solr metadata configuration'),
'#description' => t('Select the desired Solr metadata configuration to use with this field. Note that the Content Model association is ignored'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $settings['metadata_configuration'],
);
}
return $element;
}

/**
* Implements hook_theme().
*/
Expand All @@ -179,6 +310,18 @@ function islandora_object_field_theme($existing, $type, $theme, $path) {
'template' => 'templates/islandora-object-field-label',
'variables' => array('label' => NULL, 'pid' => NULL, 'object' => NULL),
),
'islandora_object_field_solr' => array(
'template' => 'templates/islandora-object-field-solr',
'variables' => array('label' => NULL, 'pid' => NULL, 'object' => NULL, 'solr' => NULL),
),
'islandora_object_field_solrmetadata_byo' => array(
'template' => 'templates/islandora-object-field-solrmetadata',
'variables' => array('label' => NULL, 'pid' => NULL, 'object' => NULL, 'solr' => NULL),
),
'islandora_object_field_solrmetadata' => array(
'template' => 'templates/islandora-object-field-solrmetadata',
'variables' => array('label' => NULL, 'pid' => NULL, 'object' => NULL, 'solr' => NULL),
),
);
}

Expand Down Expand Up @@ -279,6 +422,39 @@ function islandora_object_field_autocomplete($field, $search) {
$cmodels = array_filter($field['settings']['content_models']);
$repository = islandora_get_tuque_connection()->repository;

if (module_exists('islandora_solr')) {
module_load_include('inc', 'islandora_solr', 'includes/utilities');
$qp = new IslandoraSolrQueryProcessor();
$qp->buildQuery($search);
$fq = '';
foreach ($cmodels as $cmodel) {
$fq .= ($fq ? ' OR ' : '').'"info:fedora/'.$cmodel.'"';
}
if ($fq && count($cmodels) > 1) {
$fq = '('.$fq.')';
}
if ($fq) {
$qp->solrParams['fq'][] = islandora_solr_lesser_escape(variable_get('islandora_solr_content_model_field', 'RELS_EXT_hasModel_uri_ms')).':'.$fq;
}
$label_field = variable_get('islandora_solr_object_label_field', 'fgs_label_s');
$qp->solrParams['fl'] = implode(',', array(
'PID',
$label_field,
));
$qp->solrParams['defType'] = 'dismax';
$qp->executeQuery();
$return = array();
foreach ($qp->islandoraSolrResult['response']['objects'] as $result) {
$return[$result['PID']] = t('@label (@pid)', array(
//TODO: why is $result['solr_doc'] empty? We should be able to pull $result['solr_doc'][$label_field].
'@label' => $result['object_label'],
'@pid' => $result['PID'],
));
}
drupal_json_output($return);
return;
}

// Build UNION statement.
$where_strings = array();
foreach ($cmodels as $cmodel) {
Expand Down
1 change: 1 addition & 0 deletions templates/islandora-object-field-label.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* Available variables:
* - $label: The label text.
* - $object: The Fedora object
* - $pid: The ID of the object whose label is being displayed.
*/
echo l($label, 'islandora/object/' . $pid); ?>
18 changes: 18 additions & 0 deletions templates/islandora-object-field-solr.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* @file
* Displays an Islandora object as a Solr search result
*
* Available variables:
* - $label: The label text.
* - $object: The Fedora object
* - $pid: The ID of the object whose label is being displayed.
* - $solr: The rendered solr search result for the object
*/
?>
<div class="islandora-object-field-wrapper">
<?php
echo $solr;
?>
</div>
19 changes: 19 additions & 0 deletions templates/islandora-object-field-solrmetadata.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file
* Displays an Islandora object as a Solr metadata display
*
* Available variables:
* - $label: The label text.
* - $object: The Fedora object
* - $pid: The ID of the object whose label is being displayed.
* - $solr: The rendered solr metadata for the object
*/
?>
<div class="islandora-object-field-wrapper">
<?php
echo l($label, 'islandora/object/' . $pid);
echo $solr;
?>
</div>