Hi, I'm customizing my search.module (the basic search module). So I'm primarily working with template.php, search-result, and search-results (tpl files). My issue has to do with simply updating a variable every time drupal passes through the code I've customized in search-result.tpl.php (see below for code).
Setup:
In my code for search-result, which is the display for each returned item in the search results, I have some custom PHP code that I'm using (with a switch statement) to test if a particular field for a returned item matches a particular type. Note that in template.php these results will be grouped and ordered using SQL. So, I have three types: conference, articles, and tools. If a result has conference (and we'll say there are 5 results with conference as the type), then I want the header 'Timely Events' to display ONCE, after which all those conferences are displayed, with links to their nodes, etc. THEN, once it's looped through those I want it to display a header for 'Quick Study' and then have it display all the results for articles. Etc.
Problem:
Now, I have the switch statement working fine based on the type, but it's outputting a header for EVERY SINGLE returned result. I'm having trouble updating my variable $b so that $a=$b and returns the result without a header. Right now $a NEVER equals $b it seems. So what I need help with is how do I update $b so that it sticks...or is there another way to do this?
Here's my code for search-result.tpl.php (please ignore some of the formatting, I removed some of the < and > and php stuff to make this easier to read...the code basically works except for the updating of the variable:
if (isset($b)) {
$b = $b;
} else {
$b = "";
}
switch ($teaser['field_type']['#items'][0]['value']) {
case "Conference";
$a = "Timely Events";
break;
case "Article or Report";
$a = "Quick Study";
break;
case "Tool":
$a = "Deep Dive";
break;
default:
$a = "Other";
}
/ the following NEVER runs
if ($a == $b) {
/ basic code that search.module uses to output search result here
} else {
div class="search-header" print $a;
/ basic code that search.module uses to output search result here
$b = $a; }
Comments
First you should not be
First you should not be modifying a core module, it will make upgrading a problem.
I suspect you want to use views but it would help to know logically what you are trying to achieve.
Fair enough. But I'm not
Fair enough. But I'm not customizing the module itself, just adding some preprocess functions to template.php and modifying the search result tpl files in my theme.
At any rate, here's the goal:
User should be able to search with basic (or advanced search...ie. options for searching by 'containing the phrase', 'containing the words', etc. - the basic search module advanced search criteria optimally). User searches only by 1 custom content type by default (they don't select it). Results are displayed such that items are grouped with headers, in a very specific order.
For example, the content type is 'resources'. Each resource has a field list item called Category. Categories include: webinar, conference, article, book, blog, etc. (there are 10 of them). When a user searches by a term all results need to show up IN ORDER (by category first, then date, then title)...so, all webinars display first, then conferences, then articles, then blogs, etc. Moreover, webinars and conferences need to show up under a HEADER called Timely Events (styled in stylesheet). Once webinars and conferences are displayed another header called 'Deep Dive' will display followed by all the articles THEN books THEN blogs, sorted by category then title.
An example of how it might look:
User searches for: 'directories'
Results:
Timely Events Header
Result 1 - webinar (link, description, date)
Result 2 - webinar
Result 3 - conference
Deep Dive Header
Result 4 - article
Result 5 - article
Result 6 - article
Result 7 - blog
etc.
First < > Last
You'd be better off using the
You'd be better off using the views module than customizing the search module for this.
@Jaypan
@Jaypan
People keep suggesting views. I'm so close with the search it kills me. I've been customizing it for months.
If I were to move to Views to accomplish this can you give me some advice on how to move forward with creating a search view (for users) that returns results sorted in a very specific way? (sorted in a way I setup in advance, not something the user chooses)
A link to an article or to would be fine if you can think of anything. Thanks.
I don't actually use Views
I don't actually use Views myself (I'm a bit of an anomaly, I prefer to create my own DB queries), but it's one of the most used module out there. As such, there are many, many tutorials on how to use it. Google 'Drupal Views tutorial' (https://www.google.com/#q=drupal+views+tutorial) and you will find lots of information.
You'll want to create a page view that returns the content type(s) you want, and you can set your own methods of sorting the results.
After you have your results, google 'theming drupal views' and you'll find plenty of information on that as well.
May I ask why are you setting
May I ask why are you setting
$b = $b
? That does not make sense to me. You have the right idea of using a preprocessing function. Also, make sure to use single-quotes for strings unless you are including variables in them. This can slow down the server. How about if you modify the code to look like this:Thanks - I went with Views
Thank you both for your replies. @Jaypan I went ahead and used Views and got it working like I wanted. However @bribread22 I also appreciate your feedback and would like to test this out to see if I can't get this working, just for completeness of my learning experience if nothing else. The problem I was experiencing is that every time this code runs $b gets reset to '' because it's apparently never set when this code runs. I tried creating a $variables['b'] in my preprocess function but that didn't seem to help.
Views are definitely good to
Views are definitely good to use for anything that involves interacting with the database. It's nice how it is very customizable and they have an API that you can use from.
If you also want to try doing something using the template approach (using $variables) just for practice, you can implement
hook_theme()
,hook_menu()
and your own page callback function to display your rendered template to the page.Then your mytemplate.tpl.php file might look something like this: