How to Highlight Search Query in a Return List

I've been working on registering Search on Elgg3.* for specific plugin and got it to work partially. What I cannot figure out is how to highlight a search query in the return list. I have looked at the Search plugin as a guidance but none of what I have tried is working.

Please see my code below:

elgg_register_plugin_hook_handler('search', 'object:my_subtype', 'my_subtype_site_search');

function my_subtype_site_search($hook, $type, $terms, $params) {

   $query = $params['query'];

   $items = elgg_get_entities[ 'query' => $query, 'type' => 'object', 'subtype' => 'my_subtype', ];

   foreach ($items as $item) {

      $item->setVolatileData('search_matched_title', $item->title);

      $item->setVolatileData('search_matched_description', $item->description);

   }

   return array( 'count' => count($items), 'entities' => $items );

}

Thanks in advance!

  • If all you're doing in your custom search hook is elgg_get_entities() why not simply register your subtype for search in the elgg-plugin.php (see http://learn.elgg.org/en/stable/guides/plugins.html#elgg-plugin-php)  and the system will handle the rest.

  • Thanks Jerome!

    I registered my subtype in elgg-plugin.php of my plugin and it highlights only title and description of return entities not an owner of the entities when searching by username.

    For example, Joe wrote blog posts and I wanted his name highlighted when searching for 'joe'. At the moment, it lists his posts without highlighting his name. 

  • I found a solution for my issue. What I did was:

    - inside my_plugin/views/default/search/object/filename.php, I added:

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

        2) $service = new \Elgg\Search\Search($vars);

        3) $user = $entity->getOwnerEntity( );

        4) $username = $service->getHighlighter( ) -> highlightWords($user->username);

    Then echo $username or put the $username in desired format. 

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking