Skip to content

Commit

Permalink
feature(views): allow providing alternative views for list items
Browse files Browse the repository at this point in the history
Alternative view to render list items can now be passed as 'item_view' parameter
to list generating views and functions
  • Loading branch information
hypeJunction committed Feb 18, 2015
1 parent 286be71 commit 85c22f3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
1 change: 1 addition & 0 deletions engine/lib/entities.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ function _elgg_get_entity_time_where_sql($table, $time_created_upper = null,
* @internal If the initial COUNT query returns 0, the $getter will not be called again. * @internal If the initial COUNT query returns 0, the $getter will not be called again.
* *
* @param array $options Any options from $getter options plus: * @param array $options Any options from $getter options plus:
* item_view => STR Optional. Alternative view used to render list items
* full_view => BOOL Display full view of entities (default: false) * full_view => BOOL Display full view of entities (default: false)
* list_type => STR 'list' or 'gallery' * list_type => STR 'list' or 'gallery'
* list_type_toggle => BOOL Display gallery / list switch * list_type_toggle => BOOL Display gallery / list switch
Expand Down
1 change: 1 addition & 0 deletions engine/lib/river.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ function _elgg_prefetch_river_entities(array $river_items) {
* List river items * List river items
* *
* @param array $options Any options from elgg_get_river() plus: * @param array $options Any options from elgg_get_river() plus:
* item_view => STR Alternative view to render list items
* pagination => BOOL Display pagination links (true) * pagination => BOOL Display pagination links (true)
* no_results => STR|Closure Message to display if no items * no_results => STR|Closure Message to display if no items
* *
Expand Down
79 changes: 52 additions & 27 deletions engine/lib/views.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -771,16 +771,15 @@ function elgg_view_menu_item(\ElggMenuItem $item, array $vars = array()) {
* The entity view is called with the following in $vars: * The entity view is called with the following in $vars:
* - \ElggEntity 'entity' The entity being viewed * - \ElggEntity 'entity' The entity being viewed
* *
* Other common view $vars paramters:
* - bool 'full_view' Whether to show a full or condensed view. (Default: true)
*
* @tip This function can automatically appends annotations to entities if in full * @tip This function can automatically appends annotations to entities if in full
* view and a handler is registered for the entity:annotate. See https://github.com/Elgg/Elgg/issues/964 and * view and a handler is registered for the entity:annotate. See https://github.com/Elgg/Elgg/issues/964 and
* {@link elgg_view_entity_annotations()}. * {@link elgg_view_entity_annotations()}.
* *
* @param \ElggEntity $entity The entity to display * @param \ElggEntity $entity The entity to display
* @param array $vars Array of variables to pass to the entity view. * @param array $vars Array of variables to pass to the entity view.
* In Elgg 1.7 and earlier it was the boolean $full_view * In Elgg 1.7 and earlier it was the boolean $full_view
* 'full_view' Whether to show a full or condensed view. (Default: true)
* 'item_view' Alternative view used to render this entity
* @param boolean $bypass If true, will not pass to a custom template handler. * @param boolean $bypass If true, will not pass to a custom template handler.
* {@link set_template_handler()} * {@link set_template_handler()}
* @param boolean $debug Complain if views are missing * @param boolean $debug Complain if views are missing
Expand Down Expand Up @@ -813,25 +812,25 @@ function elgg_view_entity(\ElggEntity $entity, $vars = array(), $bypass = false,


$vars['entity'] = $entity; $vars['entity'] = $entity;



// if this entity has a view defined, use it
$view = $entity->view;
if (is_string($view)) {
return elgg_view($view, $vars, $bypass, $debug);
}

$entity_type = $entity->getType(); $entity_type = $entity->getType();

$entity_subtype = $entity->getSubtype();
$subtype = $entity->getSubtype(); if (empty($entity_subtype)) {
if (empty($subtype)) { $entity_subtype = 'default';
$subtype = 'default';
} }


$entity_views = array(
elgg_extract('item_view', $vars, ''),
$entity->view,
"$entity_type/$entity_subtype",
"$entity_type/default",
);

$contents = ''; $contents = '';
if (elgg_view_exists("$entity_type/$subtype")) { foreach ($entity_views as $view) {
$contents = elgg_view("$entity_type/$subtype", $vars, $bypass, $debug); if (elgg_view_exists($view)) {
} else { $contents = elgg_view($view, $vars, $bypass, $debug);
$contents = elgg_view("$entity_type/default", $vars, $bypass, $debug); break;
}
} }


// Marcus Povey 20090616 : Speculative and low impact approach for fixing #964 // Marcus Povey 20090616 : Speculative and low impact approach for fixing #964
Expand Down Expand Up @@ -905,6 +904,7 @@ function elgg_view_entity_icon(\ElggEntity $entity, $size = 'medium', $vars = ar
* *
* @param \ElggAnnotation $annotation The annotation to display * @param \ElggAnnotation $annotation The annotation to display
* @param array $vars Variable array for view. * @param array $vars Variable array for view.
* 'item_view' Alternative view used to render an annotation
* @param bool $bypass If true, will not pass to a custom * @param bool $bypass If true, will not pass to a custom
* template handler. {@link set_template_handler()} * template handler. {@link set_template_handler()}
* @param bool $debug Complain if views are missing * @param bool $debug Complain if views are missing
Expand Down Expand Up @@ -934,11 +934,21 @@ function elgg_view_annotation(\ElggAnnotation $annotation, array $vars = array()
return false; return false;
} }


if (elgg_view_exists("annotation/$name")) { $annotation_views = array(
return elgg_view("annotation/$name", $vars, $bypass, $debug); elgg_extract('item_view', $vars, ''),
} else { "annotation/$name",
return elgg_view("annotation/default", $vars, $bypass, $debug); "annotation/default",
);

$contents = '';
foreach ($annotation_views as $view) {
if (elgg_view_exists($view)) {
$contents = elgg_view($view, $vars, $bypass, $debug);
break;
}
} }

return $contents;
} }


/** /**
Expand All @@ -959,6 +969,7 @@ function elgg_view_annotation(\ElggAnnotation $annotation, array $vars = array()
* 'full_view' Display the full view of the entities? * 'full_view' Display the full view of the entities?
* 'list_class' CSS class applied to the list * 'list_class' CSS class applied to the list
* 'item_class' CSS class applied to the list items * 'item_class' CSS class applied to the list items
* 'item_view' Alternative view to render list items
* 'pagination' Display pagination? * 'pagination' Display pagination?
* 'list_type' List type: 'list' (default), 'gallery' * 'list_type' List type: 'list' (default), 'gallery'
* 'list_type_toggle' Display the list type toggle? * 'list_type_toggle' Display the list type toggle?
Expand Down Expand Up @@ -1040,6 +1051,7 @@ function elgg_view_entity_list($entities, $vars = array(), $offset = 0, $limit =
* 'limit' The number of annotations to display per page * 'limit' The number of annotations to display per page
* 'full_view' Display the full view of the annotation? * 'full_view' Display the full view of the annotation?
* 'list_class' CSS Class applied to the list * 'list_class' CSS Class applied to the list
* 'item_view' Alternative view to render list items
* 'offset_key' The url parameter key used for offset * 'offset_key' The url parameter key used for offset
* 'no_results' Message to display if no results (string|Closure) * 'no_results' Message to display if no results (string|Closure)
* *
Expand Down Expand Up @@ -1212,7 +1224,7 @@ function elgg_view_module($type, $title, $body, array $vars = array()) {
* *
* @param \ElggRiverItem $item A river item object * @param \ElggRiverItem $item A river item object
* @param array $vars An array of variables for the view * @param array $vars An array of variables for the view
* * 'item_view' Alternative view to render the item
* @return string returns empty string if could not be rendered * @return string returns empty string if could not be rendered
*/ */
function elgg_view_river_item($item, array $vars = array()) { function elgg_view_river_item($item, array $vars = array()) {
Expand Down Expand Up @@ -1245,7 +1257,20 @@ function elgg_view_river_item($item, array $vars = array()) {


$vars['item'] = $item; $vars['item'] = $item;


return elgg_view('river/item', $vars); $river_views = array(
elgg_extract('item_view', $vars, ''),
"river/item",
);

$contents = '';
foreach ($river_views as $view) {
if (elgg_view_exists($view)) {
$contents = elgg_view($view, $vars);
break;
}
}

return $contents;
} }


/** /**
Expand Down Expand Up @@ -1340,9 +1365,9 @@ function elgg_view_tagcloud(array $options = array()) {
/** /**
* View an item in a list * View an item in a list
* *
* @param mixed $item An entity, an annotation, or a river item to display * @param \ElggEntity|\ElggAnnotation $item
* @param array $vars Additional parameters for the rendering * @param array $vars Additional parameters for the rendering
* * 'item_view' Alternative view used to render list items
* @return string * @return string
* @since 1.8.0 * @since 1.8.0
* @access private * @access private
Expand Down
3 changes: 2 additions & 1 deletion views/default/page/components/gallery.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* *
* Implemented as an unorder list * Implemented as an unorder list
* *
* @uses $vars['items'] Array of ElggEntity or ElggAnnotation objects * @uses $vars['items'] Array of ElggEntity, ElggAnnotation or ElggRiverItem objects
* @uses $vars['offset'] Index of the first list item in complete list * @uses $vars['offset'] Index of the first list item in complete list
* @uses $vars['limit'] Number of items per page * @uses $vars['limit'] Number of items per page
* @uses $vars['count'] Number of items in the complete list * @uses $vars['count'] Number of items in the complete list
Expand All @@ -13,6 +13,7 @@
* @uses $vars['full_view'] Show the full view of the items (default: false) * @uses $vars['full_view'] Show the full view of the items (default: false)
* @uses $vars['gallery_class'] Additional CSS class for the <ul> element * @uses $vars['gallery_class'] Additional CSS class for the <ul> element
* @uses $vars['item_class'] Additional CSS class for the <li> elements * @uses $vars['item_class'] Additional CSS class for the <li> elements
* @uses $vars['item_view'] Alternative view to render list items
* @uses $vars['no_results'] Message to display if no results (string|Closure) * @uses $vars['no_results'] Message to display if no results (string|Closure)
*/ */


Expand Down
3 changes: 2 additions & 1 deletion views/default/page/components/list.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* *
* @package Elgg * @package Elgg
* *
* @uses $vars['items'] Array of ElggEntity or ElggAnnotation objects * @uses $vars['items'] Array of ElggEntity, ElggAnnotation or ElggRiverItem objects
* @uses $vars['offset'] Index of the first list item in complete list * @uses $vars['offset'] Index of the first list item in complete list
* @uses $vars['limit'] Number of items per page. Only used as input to pagination. * @uses $vars['limit'] Number of items per page. Only used as input to pagination.
* @uses $vars['count'] Number of items in the complete list * @uses $vars['count'] Number of items in the complete list
Expand All @@ -14,6 +14,7 @@
* @uses $vars['full_view'] Show the full view of the items (default: false) * @uses $vars['full_view'] Show the full view of the items (default: false)
* @uses $vars['list_class'] Additional CSS class for the <ul> element * @uses $vars['list_class'] Additional CSS class for the <ul> element
* @uses $vars['item_class'] Additional CSS class for the <li> elements * @uses $vars['item_class'] Additional CSS class for the <li> elements
* @uses $vars['item_view'] Alternative view to render list items
* @uses $vars['no_results'] Message to display if no results (string|Closure) * @uses $vars['no_results'] Message to display if no results (string|Closure)
*/ */


Expand Down

0 comments on commit 85c22f3

Please sign in to comment.