From 360cec85a153aa627993fa587e4480617edea156 Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht Date: Mon, 10 Jun 2019 15:44:07 -0400 Subject: [PATCH] added image thumbnails to search results --- ryf.theme | 16 +++++++ search.pages.inc | 16 +++++++ templates/search-result.html.twig | 74 +++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 search.pages.inc create mode 100644 templates/search-result.html.twig diff --git a/ryf.theme b/ryf.theme index fcf4fcb..f9a7a6f 100644 --- a/ryf.theme +++ b/ryf.theme @@ -6,3 +6,19 @@ * * Place your custom PHP code in this file. */ + +/** + * Implements hook_theme(). + * + * Used to show image thumbnails in search results + * + */ +function ryf_theme($existing, $type, $theme, $path) { + return [ + 'search_result' => [ + 'variables' => ['result' => NULL, 'plugin_id' => NULL], + 'file' => 'search.pages.inc', + ], + ]; +} + diff --git a/search.pages.inc b/search.pages.inc new file mode 100644 index 0000000..ff7e378 --- /dev/null +++ b/search.pages.inc @@ -0,0 +1,16 @@ +toArray()['field_image']))); + + // this attaches the 'node' object so that we can access the image in field_image, via our twig template. + $variables['node'] = $variables['result']['node']; + +} + diff --git a/templates/search-result.html.twig b/templates/search-result.html.twig new file mode 100644 index 0000000..9809a16 --- /dev/null +++ b/templates/search-result.html.twig @@ -0,0 +1,74 @@ +{# +/** + * @file + * Default theme implementation for displaying a single search result. + * + * This template renders a single search result. The list of results is + * rendered using '#theme' => 'item_list', with suggestions of: + * - item_list__search_results__(plugin_id) + * - item_list__search_results + * + * Available variables: + * - url: URL of the result. + * - title: Title of the result. + * - snippet: A small preview of the result. Does not apply to user searches. + * - info: String of all the meta information ready for print. Does not apply + * to user searches. + * - plugin_id: The machine-readable name of the plugin being executed,such + * as "node_search" or "user_search". + * - title_prefix: Additional output populated by modules, intended to be + * displayed in front of the main title tag that appears in the template. + * - title_suffix: Additional output populated by modules, intended to be + * displayed after the main title tag that appears in the template. + * - info_split: Contains same data as info, but split into separate parts. + * - info_split.type: Node type (or item type string supplied by module). + * - info_split.user: Author of the node linked to users profile. Depends + * on permission. + * - info_split.date: Last update of the node. Short formatted. + * - info_split.comment: Number of comments output as "% comments", % + * being the count. (Depends on comment.module). + * @todo The info variable needs to be made drillable and each of these sub + * items should instead be within info and renamed info.foo, info.bar, etc. + * + * Other variables: + * - title_attributes: HTML attributes for the title. + * - content_attributes: HTML attributes for the content. + * + * Since info_split is keyed, a direct print of the item is possible. + * This array does not apply to user searches so it is recommended to check + * for its existence before printing. The default keys of 'type', 'user' and + * 'date' always exist for node searches. Modules may provide other data. + * @code + * {% if (info_split.comment) %} + * + * {{ info_split.comment }} + * + * {% endif %} + * @endcode + * + * To check for all available data within info_split, use the code below. + * @code + *
+ *     {{ dump(info_split) }}
+ *   
+ * @endcode + * + * @see template_preprocess_search_result() + * + * @ingroup themeable + */ +#} +{{ title_prefix }} + + {{ title }} + +{{ title_suffix }} +{% if node.field_image.0 %} + +{% endif %} +{% if snippet %} + {{ snippet }}

+{% endif %} +{% if info %} +

{{ info }}

+{% endif %} -- 2.25.1