From e0b868fe0d5c9cb7c75bd82ac57b4625a499bd8a Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 13 Apr 2015 10:46:31 -0300 Subject: [PATCH 1/3] Implement "islandora_objects_subset". Instead of passing the entire array of objects in, we can pass the current page and info required for paging. --- islandora.api.php | 5 ++ islandora.module | 12 ++++ theme/theme.inc | 165 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 165 insertions(+), 17 deletions(-) diff --git a/islandora.api.php b/islandora.api.php index e8296d47..e7026923 100644 --- a/islandora.api.php +++ b/islandora.api.php @@ -3,6 +3,7 @@ * @file * This file documents all available hook functions to manipulate data. */ + /** * Generate a repository objects view. * @@ -561,8 +562,10 @@ function hook_islandora_object_access($op, $object, $user) { switch ($op) { case 'create stuff': return TRUE; + case 'break stuff': return FALSE; + case 'do a barrel roll!': return NULL; } @@ -598,8 +601,10 @@ function hook_islandora_datastream_access($op, $object, $user) { switch ($op) { case 'create stuff': return TRUE; + case 'break stuff': return FALSE; + case 'do a barrel roll!': return NULL; } diff --git a/islandora.module b/islandora.module index f1d0db80..2dec1fd9 100644 --- a/islandora.module +++ b/islandora.module @@ -474,6 +474,18 @@ function islandora_theme() { 'limit' => 10, ), ), + // Render a bunch of objects as either a grid or a list. + 'islandora_objects_subset' => array( + 'file' => 'theme/theme.inc', + 'template' => 'theme/islandora-objects', + 'variables' => array( + 'objects' => NULL, + 'display' => 'grid', + 'limit' => 10, + 'total' => NULL, + 'pager_element' => 0, + ), + ), // Render a bunch of objects as a grid. 'islandora_objects_grid' => array( 'file' => 'theme/theme.inc', diff --git a/theme/theme.inc b/theme/theme.inc index 884025b4..5b9302ae 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -202,7 +202,7 @@ function theme_islandora_object_print(array &$variables) { /** * Implements hook_preprocess_theme(). */ -function islandora_preprocess_islandora_objects(array &$variables) { +function template_preprocess_islandora_objects(array &$variables) { $display = (empty($_GET['display'])) ? 'grid' : $_GET['display']; $grid_display = $display == 'grid'; $list_display = !$grid_display; @@ -210,17 +210,21 @@ function islandora_preprocess_islandora_objects(array &$variables) { $variables['display_links'] = array( array( 'title' => t('Grid view'), - 'href' => url($_GET['q'], array('absolute' => TRUE)), + 'href' => current_path(), 'attributes' => array( - 'class' => $grid_display ? 'active' : '', + 'class' => array( + $grid_display ? 'active' : '', + ), ), 'query' => array('display' => 'grid') + $query_params, ), array( 'title' => t('List view'), - 'href' => url($_GET['q'], array('absolute' => TRUE)), + 'href' => current_path(), 'attributes' => array( - 'class' => $list_display ? 'active' : '', + 'class' => array( + $list_display ? 'active' : '', + ), ), 'query' => array('display' => 'list') + $query_params, ), @@ -228,18 +232,49 @@ function islandora_preprocess_islandora_objects(array &$variables) { // Pager. $objects = $variables['objects']; $limit = $variables['limit']; - $page_size = $variables['page_size']; $page = pager_default_initialize(count($objects), $limit); $objects = array_slice($objects, $page * $limit, $limit); $variables['pager'] = theme('pager', array('quantity' => 10)); - // Content. - $map_objects = function($o) { - $o = islandora_object_load($o); + + $objects = array_map('islandora_objects_object_mapper', $objects); + $theme = $grid_display ? 'islandora_objects_grid' : 'islandora_objects_list'; + $variables['content'] = theme($theme, array('objects' => $objects)); + $module_path = drupal_get_path('module', 'islandora'); + drupal_add_css("$module_path/css/islandora.objects.css"); +} + +/** + * Helper function to map objects to their values to be used in templates. + * + * @param string $object_id + * The ID of the object for which to produce a list of values. + * + * @return array + * An associative array of values, including: + * - label: A string containing object's label. + * - class: A string containing an HTML class to add to markup representing + * the object. + * - link: A string containing a textual HTML link to the object. + * - thumb: A string containing an image HTML link to the object. + * - description: A string containing a description of the object. + */ +function islandora_objects_object_mapper($object_id) { + $o = islandora_object_load($object_id); + + $module_path = drupal_get_path('module', 'islandora'); + + $img = array( + '#theme' => 'image', + '#path' => ($o && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $o['TN']) ? + "$url/datastream/TN/view" : + "$module_path/images/folder.png"), + '#attributes' => array(), + ); + $img = drupal_render($img); + + if ($o) { $url = "islandora/object/{$o->id}"; $link_options = array('html' => TRUE, 'attributes' => array('title' => $o->label)); - $img = islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $o['TN']) ? - theme('image', array('path' => url("$url/datastream/TN/view"), 'attributes' => array())) : - ''; $description = NULL; if (isset($o['DC']) && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $o['DC'])) { $dc = DublinCore::importFromXMLString($o['DC']->content); @@ -255,12 +290,108 @@ function islandora_preprocess_islandora_objects(array &$variables) { 'thumb' => l($img, $url, $link_options), 'description' => $description, ); - }; - $objects = array_map($map_objects, $objects); - $theme = $grid_display ? 'islandora_objects_grid' : 'islandora_objects_list'; - $variables['content'] = theme($theme, array('objects' => $objects)); + } + else { + $url = "islandora/object/$object_id"; + $link_options = array('html' => TRUE, 'attributes' => array('title' => t('(Unknown)'))); + return array( + 'label' => t('(Unknown)'), + 'class' => drupal_strtolower(preg_replace('/[^A-Za-z0-9]/', '-', $object_id)), + 'link' => l(t('(Unknown)'), $url, $link_options), + 'thumb' => '', + 'description' => '', + ); + } +} + +/** + * Prepares variables for islandora_objects_subset templates. + * + * A variant of "islandora_objects" which accepts a subset of object to theme. + * + * @see template_preprocess_islandora_objects() + * + * @param array $variables + * An associative array containing: + * - objects: An array of PIDs to render for the given page. + * - limit: An integer indicating the number of items per page, so we can + * render a pager. + * - total: An integer indicating the total number of items in the set, so + * can render a pager. + * - pager_element: An integer identifying which "pager" this display should + * use. Defaults to 0. + * - display: The default display to use when one is not provided in the URL. + * One of: + * - 'grid' + * - 'list' + * This function sets: + * - display_links: An array containing link structure, to allow the view to + * be toggled between a "grid" and "list" view. + * - pager: A renderable array for the pager. + * - content: A renderable array for the main content of the page. + */ +function template_preprocess_islandora_objects_subset(&$variables) { + $display = (empty($_GET['display'])) ? $variables['display'] : $_GET['display']; + $grid_display = $display == 'grid'; + $list_display = !$grid_display; + $query_params = drupal_get_query_parameters($_GET); + + // XXX: In l(), Drupal automatically adds the "active" class if it looks like + // you are generating a link to the same page, based on the path and language. + // Here, we use the "language" side of things to assert our links belong to a + // non-existent language, so we can take control of adding our "active" class. + $language_hack = new stdClass(); + $language_hack->language = 'a-non-existent-language'; + + $variables['display_links'] = array( + array( + 'title' => t('Grid view'), + 'href' => current_path(), + 'attributes' => array( + 'class' => array( + $grid_display ? 'active' : '', + ), + ), + 'query' => array('display' => 'grid') + $query_params, + 'language' => $language_hack, + ), + array( + 'title' => t('List view'), + 'href' => current_path(), + 'attributes' => array( + 'class' => array( + $list_display ? 'active' : '', + ), + ), + 'query' => array('display' => 'list') + $query_params, + 'language' => $language_hack, + ), + ); + + $variables['pager'] = array( + '#theme' => 'pager', + '#element' => $variables['pager_element'], + ); $module_path = drupal_get_path('module', 'islandora'); - drupal_add_css("$module_path/css/islandora.objects.css"); + $variables['content'] = array( + '#attached' => array( + 'css' => array( + "$module_path/css/islandora.objects.css", + ), + ), + '#theme' => $grid_display ? 'islandora_objects_grid' : 'islandora_objects_list', + '#objects' => $variables['objects'], + ); +} + +/** + * Process variables for islandora_objects_subset templates. + */ +function template_process_islandora_objects_subset(&$variables) { + pager_default_initialize($variables['total'], $variables['limit'], $variables['pager_element']); + $variables['pager'] = drupal_render($variables['pager']); + $variables['content']['#objects'] = array_map('islandora_objects_object_mapper', $variables['content']['#objects']); + $variables['content'] = drupal_render($variables['content']); } /** From 510b6b2d323afe0e0e4b0f858cf11ee14aa6233b Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 13 Apr 2015 11:14:29 -0300 Subject: [PATCH 2/3] Alter "islandora_objects" to match the collection display by default. --- css/islandora.objects.css | 49 ++++++++++++++++------------ theme/islandora-objects-grid.tpl.php | 2 +- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/css/islandora.objects.css b/css/islandora.objects.css index 5e03d3f7..ff34dca8 100644 --- a/css/islandora.objects.css +++ b/css/islandora.objects.css @@ -2,11 +2,26 @@ * @file * Styles for rendering grids/lists of objects. */ -.islandora-objects-display-switch { - float: right; + +.islandora-objects-list-item { + clear: both; + width: 100%; } -.islandora-objects-grid-item { - display: inline-block; + +.islandora-objects-list-item dl dt { + clear: left; + float: left; + padding: 3px 0 0; + text-align: center; + width: 100px; +} + +.islandora-objects-list-item dl dd { + margin: 0 0 0 130px; +} + +/*Grid - Displays grid using dl formatted inline */ +.islandora-objects-grid dl { width: 20%; min-width: 100px; min-height: 180px; @@ -18,22 +33,16 @@ *display: inline; _height: 180px; } -.islandora-objects-list-item { - padding-bottom: 1.5em; - border-bottom: 1px solid #ddd; -} -.islandora-objects-list-item .islandora-object-thumb { - clear: left; - float: left; - padding: 3px 0 0; - text-align: center; - width: 100px; + +.islandora-objects-grid-item { + display: inline; } -.islandora-objects-list-item .islandora-object-caption, .islandora-objects-list-item .islandora-object-description { - margin: 0 0 0 130px; - padding-top: 2px; - padding-bottom: 2px; + +.islandora-objects-grid dd { + margin: 0; + padding: 0; } -.islandora-object-thumb img { - width: 100%; + +.islandora-objects-display-switch { + float: right; } diff --git a/theme/islandora-objects-grid.tpl.php b/theme/islandora-objects-grid.tpl.php index e6d7e140..10007a32 100644 --- a/theme/islandora-objects-grid.tpl.php +++ b/theme/islandora-objects-grid.tpl.php @@ -7,7 +7,7 @@ ?>
-
+
From d4163aa022420879c73d17accbc96325c572059d Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 13 Apr 2015 15:54:19 -0300 Subject: [PATCH 3/3] Move variable creation, 'cause derp. --- theme/theme.inc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/theme/theme.inc b/theme/theme.inc index 5b9302ae..1ddad1db 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -263,6 +263,7 @@ function islandora_objects_object_mapper($object_id) { $module_path = drupal_get_path('module', 'islandora'); + $url = "islandora/object/{$object_id}"; $img = array( '#theme' => 'image', '#path' => ($o && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $o['TN']) ? @@ -273,7 +274,6 @@ function islandora_objects_object_mapper($object_id) { $img = drupal_render($img); if ($o) { - $url = "islandora/object/{$o->id}"; $link_options = array('html' => TRUE, 'attributes' => array('title' => $o->label)); $description = NULL; if (isset($o['DC']) && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $o['DC'])) { @@ -292,7 +292,6 @@ function islandora_objects_object_mapper($object_id) { ); } else { - $url = "islandora/object/$object_id"; $link_options = array('html' => TRUE, 'attributes' => array('title' => t('(Unknown)'))); return array( 'label' => t('(Unknown)'),