Just started using Joomla and ran into this problem. I'm surprised it's not a feature of the built in search component.
Here's my solution - would like to hear ideas for something more elegant:
1. add a section id parameter to the search module (mod_search.xml)
Code:
<param name="section_search_id" type="text" default="" label="Section Search Id" description="Search articles by a particular section" />
2. pass section id to search component via post (mod_search/tmpl/default.php)
Code:
<input type="hidden" name="section_id" value="<?=$params->get('section_search_id')?>" />
3. add section id to model parameters from component controller (com_search/controller.php)
Code:
$post['section_id'] = JRequest::getString('section_id', null, 'post');
4. add section_id parameter to setSearch method in model and add to constructor call (com_search/models/search.php)
Code:
$section_id = urldecode(JRequest::getString('section_id'));
$this->setSearch($keyword, $match, $ordering,$section_id);
5. add section_id to search plugin and call via dispatcher in model search->getData method (plugins/search/content.php, com_search/models/search.php)
Code:
function plgSearchContent( $text, $phrase='', $ordering='', $areas=null, $section_id=null ) {
...
and
Code:
$results = $dispatcher->trigger( 'onSearch', array(
$this->getState('keyword'),
$this->getState('match'),
$this->getState('ordering'),
$areas['active'],
$this->getState('section_id'))
);
6. add section_id conditionally to article search query and return results (plugins/search/content.php)
Code:
. ($section_id ? " AND a.sectionid=$section_id " : '')
and, right after the article query,
Code:
if ($section_id) {
$results = array();
if(count($rows)) {
foreach($rows as $row) {
$new_row = array();
foreach($row AS $key => $article) {
if(searchHelper::checkNoHTML($article, $searchText, array('text', 'title', 'metadesc', 'metakey'))) {
$new_row[] = $article;
}
}
}
$results = array_merge($results, (array) $new_row);
}
return $results;
}
hth