Skip to content

Commit

Permalink
Merge pull request #233 from matthewperry/7.x-whole-field-truncation
Browse files Browse the repository at this point in the history
Adding the ability to truncate results based on the field.
  • Loading branch information
adam-vessey committed Jul 23, 2015
2 parents d3e2d0b + eb2d081 commit 03d0044
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
9 changes: 8 additions & 1 deletion includes/admin.inc
Expand Up @@ -1554,6 +1554,7 @@ function _islandora_solr_handle_solr_field_settings($solr_field_settings = NULL,
'link_rendering' => $solr_field_settings['link_rendering'],
'snippet' => isset($solr_field_settings['snippet']) ? $solr_field_settings['snippet'] : NULL,
'date_format' => isset($solr_field_settings['date_format']) ? trim($solr_field_settings['date_format']) : '',
'truncation_type' => isset($solr_field_settings['truncation_type']) ? trim($solr_field_settings['truncation_type']) : 'separate_value_option',
'maximum_length' => isset($solr_field_settings['maximum_length']) ? trim($solr_field_settings['maximum_length']) : '',
'add_ellipsis' => isset($solr_field_settings['add_ellipsis']) ? $solr_field_settings['add_ellipsis'] : FALSE,
'wordsafe' => isset($solr_field_settings['wordsafe']) ? $solr_field_settings['wordsafe'] : FALSE,
Expand Down Expand Up @@ -1780,12 +1781,18 @@ function islandora_solr_admin_settings_result_fields($form, &$form_state, $varia
'#description' => t('<strong>Note:</strong> Truncation can lead to unexpected results when used in secondary display profiles such as CSV and RSS.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'truncation_type' => array(
'#type' => 'radios',
'#title' => t('Truncation Type'),
'#options' => array('separate_value_option' => t('Limit length of each separate value'), 'whole_field_option' => t('Limit Length of the whole field')),
'#default_value' => isset($values['truncation_type']) ? $values['truncation_type'] : 'separate_value_option',
),
'maximum_length' => array(
'#type' => 'textfield',
'#title' => t('Maximum Length'),
'#default_value' => isset($values['maximum_length']) ? $values['maximum_length'] : '0',
'#element_validate' => array('element_validate_integer'),
'#description' => t('Maximum field length to render for display. A setting of 0 (default) renders the entire value.'),
'#description' => t('Maximum field length to render for display. A setting of 0 (default) renders the entire value.<br /> When truncating based on the whole field the max length may be exceeded by the length of ellispse string.'),
),
'add_ellipsis' => array(
'#type' => 'checkbox',
Expand Down
3 changes: 3 additions & 0 deletions includes/db.inc
Expand Up @@ -187,6 +187,9 @@ function islandora_solr_get_truncate_length_fields() {
$truncate_length_fields[$value['solr_field']]['wordsafe'] = $value['solr_field_settings']['wordsafe'];
$truncate_length_fields[$value['solr_field']]['wordsafe_length'] = $value['solr_field_settings']['wordsafe_length'];
}
if (isset($value['solr_field_settings']['truncation_type'])) {
$truncate_length_fields[$value['solr_field']]['truncation_type'] = $value['solr_field_settings']['truncation_type'];
}
}
}
return $truncate_length_fields;
Expand Down
67 changes: 65 additions & 2 deletions includes/utilities.inc
Expand Up @@ -317,7 +317,7 @@ function islandora_solr_prepare_solr_results($solr_results) {
}

// Truncate value lengths before linking, avoids destroying link tags.
if (array_key_exists($field, $truncate_length) && $truncate_length[$field]['maximum_length'] > 0) {
if (array_key_exists($field, $truncate_length) && $truncate_length[$field]['maximum_length'] > 0 && (!isset($truncate_length[$field]['truncation_type']) || $truncate_length[$field]['truncation_type'] == 'separate_value_option')) {
// Coder tends to not like inline anonymous functions.
$truncate_func = function (&$val_val) use ($field, $truncate_length) {
$tf =& $truncate_length[$field];
Expand Down Expand Up @@ -345,7 +345,10 @@ function islandora_solr_prepare_solr_results($solr_results) {
};
$value = array_map($map_to_link, (array) $original_value, (array) $value);
}

// Truncate output based on the field rather than by value.
if (array_key_exists($field, $truncate_length) && $truncate_length[$field]['maximum_length'] > 0 && isset($truncate_length[$field]['truncation_type']) && $truncate_length[$field]['truncation_type'] == 'whole_field_option') {
$value = islandora_solr_truncate_field_display($value, $truncate_length[$field]['maximum_length'], $truncate_length[$field]['add_ellipsis'], $truncate_length[$field]['wordsafe'], $truncate_length[$field]['wordsafe_length'], "<br />");
}
// Implode.
$value = is_array($value) ? implode(", ", $value) : $value;
// Add link to object.
Expand Down Expand Up @@ -387,3 +390,63 @@ function islandora_solr_islandora_basic_collection_backend_callable($collection_
array_map($map_to_pids, $qp->islandoraSolrResult['response']['objects']),
);
};

/**
* Truncate the field display based on entire field result(s).
*
*
* @param array $display_values
* An array of the values that are to be processed for truncation.
* @param int $max_length
* The maxium length of characters to display before truncating results.
* @param bool $add_ellipsis
* Boolean to enable the display of an ellispsis.
* @param bool $word_safe
* Boolean to enable word safe in truncate_utf8.
* @param int $wordsafe_length
* Min wordsafe length.
* @param string $separator
* A separator to use for display output.
*
* @return string
* The updated display values.
*/
function islandora_solr_truncate_field_display($display_values, $max_length, $add_ellipsis, $word_safe, $wordsafe_length, $separator) {
$updated_display_values = $display_values;
if (count($updated_display_values) > 0) {
$mod_path = drupal_get_path('module', 'islandora_solr');
drupal_add_js("$mod_path/js/truncation-toggle.js");
// Build two arrays for display, one filtered by size (max_values)
// and one with all the data.
$truncated_list = array();
$character_output_count = 0;
foreach ($updated_display_values as $index => $current_value) {
if ($character_output_count + drupal_strlen($current_value) <= $max_length) {
$truncated_list[] = $current_value;
$character_output_count += drupal_strlen($current_value);
}
elseif ($character_output_count + drupal_strlen($current_value) > $max_length) {
$truncation_length = $max_length - $character_output_count;
// Force the display of the full ellipsis.
if ($add_ellipsis) {
$truncation_length = max($truncation_length, drupal_strlen(t('...')));
}
$current_value = truncate_utf8($current_value, $truncation_length, $word_safe, $add_ellipsis, $wordsafe_length);
$truncated_list[] = $current_value;
break;
}
}
$updated_display_values
= '<span class="toggle-wrapper">' .
t("<span>!value !separator<a href='#' class='toggler'>Show more</a></span>", array(
'!separator' => $separator,
'!value' => implode($separator, $truncated_list),
)) .
t("<span>!original_value !separator<a href='#' class='toggler'>Show less</a></span>", array(
'!separator' => $separator,
'!original_value' => implode($separator, $display_values),
)) .
'</span>';
}
return $updated_display_values;
};
19 changes: 19 additions & 0 deletions js/truncation-toggle.js
@@ -0,0 +1,19 @@
(function ($) {
"use strict";

Drupal.behaviors.islandoraSolrTruncationToggle = {
attach: function (context, settings) {
$('.toggle-wrapper', context).once('truncation-toggle', function() {
var $this = $(this);
$this.find('> span').hide().first().show();
});
$('.toggle-wrapper .toggler', context).once('truncation-toggle', function(){
var $this = $(this);
$this.click(function (event){
event.preventDefault();
$this.closest('.toggle-wrapper').find('> span').toggle();
});
});
}
};
})(jQuery);

0 comments on commit 03d0044

Please sign in to comment.