I've built a new drupal6 site (using a zen subtheme) with older content that must continue to reflect the official and original publication dates and times. All is well except for my search results, which return the 'submitted on' date/time.
COSMOS Magazine seems to have the exact same problem. When you search for 'platypus', for example, the results page displays incorrect date information (not to mention incorrect authoring information, but I don't have that issue). I think they are on an older version of drupal but I'm sure this is the same problem.
After searching around, I came up with a potential solution using this and this. The idea is to update template.php with something like the following:
function MYZENSUBTHEMENAME_search_item($item, $type) {
$output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
$info = array();
if ($item['type']) {
$info[] = check_plain($item['type']);
}
if ($item['user']) {
$info[] = $item['user'];
}
if ($item['date']) {
$info[] = format_date($item['date'], 'small');
}
if (is_array($item['extra'])) {
$info = array_merge($info, $item['extra']);
}
$output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '') .'<p class="search-info">'. implode(' - ', $info) .'</p></dd>';
return $output;
}
...and then you throw a copy of "search-result.tpl.php" into your theme directory.
I have been experimenting in template.php (lines 11 & 12) substituting "date" with words like "created" and "submitted" with no success. Can someone provide input as to haw to actually effect the "Created on" or "Authored on" date?
I suppose another alternative to consider would be to use a SQL query and modify the actual "created on" fields in my database. I might actually prefer this approach, now that I think about it, but I have no idea whether or not it's really the best solution.
Comments
If i have understood right (
If i have understood right ( i'm not native english speaker ), you want to remove "date" on search results.
The function you have written is not going to work in Drupal 6. What you have found is for Drupal 5.
What you need to do is add this function into your template.php of MYZENSUBTHEMENAME:
..and then , you should comment what you need to hide
"authored on" instead of "last updated" (or "created on")
Thanks francort. This may be useful as a secondary option, if all else fails. And I appreciate your taking the time to post the drupal6 version of the code.
My primary goal is to use the actual "authored on" date in my nodes, which I have artificially set to be the original publication date/time for each piece of content (~1-2 years ago in most cases). Unfortunately the search results page actually returns the date/timestamp for when the node was last updated instead of displaying values for the authored on field, incorrectly indicating that the content was written this very month. (Actually, maybe it's returning the node creation date, I'm not sure, but that's still not what I want).
So rather than editing the code above to hide the date I would like to replace a variable and/or string with whatever variable and/or string that will return the "authored on" field in my nodes.
If I can't do that, I may end up hiding the date altogether, but that's not ideal.
find data
I think "authored on" information may be inside $result or $variables array. Perhaps doing this, it is possible to find it
That one will find what's inside $result and/or $variables. If somewhere there is "authored on", then replace the line "date". Something like:
I believe something like that should work :)
Parse error
Hmm, upon preview the first one produces a parsing error, but it's late here and I'll have to troubleshoot somewhere in my theme in the morning.
Parse error: syntax error, unexpected $end in /somepath/qa.mysite.com/includes/common.inc(1537) : eval()'d code on line 11(Later)
Not sure what's wrong. Here are lines 1536-1539 for context:
First I took the revised function in this post and added it to my zen subtheme's template.php (substituting "MYZENSUBTHEMENAME" for my theme name). Then I took the first of the two pieces of code you posted in this post and previewed it as a PHP type "page", which is when the error appeared.
More specific
Sorry if i wasn't specific enough. I just have written parts of the entire code.
The complete code, should be as follows:
After having the function in zen subtheme, a search must be performed.( with results )
That search will stop the site from working and should show on the screen the values inside $result.
case a)
If you'll see the "authored on" over there, replace this part of the code above.
for the variable that contains "authored on". The folowing code, it's just an example since i don't know where "authored on" is. I will asume that it is part of the node:
if "authored on" isn't a valid timestamp, you shouldn't use format_date
After that, remove the part of the function with print's and die( or your site will stop working with any seach )
case b
If "authored on" is not in $result, comment line with print_r( $result ) and uncomment print_r( $variables )
then do the same as case b.
case c
If "authored on" is not there at all, then you will need to add some lines, to get the information directly from database.
Nothing of this is going to work as PHP type "page".
If you need more help in any case, i'm here :)
Getting closer, I think
Excellent, this looks like the right approach. Unfortunately, although I used the function in my template exactly as you posted (again, substituting "MYZENSUBTHEMENAME" with my zen subtheme's name), I had only partial success.
The first time I conducted a search with that function in my template.php, the search results did indeed "break the site" in a sense, just as you said it would, and I saved the results locally for analysis before reverting the template change back to normal without the new function.
The results file did not contain any instances of the string "$result" or "$results" and the only instance of the word "result" at all was in node content, not code. I searched also for "author" or "authored" and found nothing. Nor did I see any instances of dates in the search results, other than what you see here in this sample:
The "created" and the "changed" dates do not seem to correspond with the "authored" date I used while saving the nodes, incidentally.
After this I had a thought, and went to admin/build/themes/settings. There, I disabled "Display post information on" for "Book review", thinking that perhaps if any content type had this disabled that it would interfere with the testing. I ran the search test again, and searched for the same things as the above with the same results.
Then I went back to admin/build/themes/settings once more, and enabled show post information for all content types. This time, my search did not "break" as it had previously, and yet it also did not show the authored on information. I've since disabled display post information and cleared my site cache several times, but I cannot get the search to "break" in the same way now. Nevertheless, I think that first test should have brought back something more than it did. Tomorrow I'll run through all the tests again just in case.
The difference between
The difference between "created" and "changed" it's huge, more than a year( something like 420 days). Even if you have saved the date as string, the system would be converting that string into an integer.
Edit this code as follows:
1)Erase any line(the entire line) which contains "print" or "print_r"
2)Erase line with "die"
3)Replace this part inside the code:
for this
By doing that , you will see in your searchs not the date where the node was last changed but the date when the node was created. If you want larger fotmat, you can replace 'small' for 'medium' or 'large'.
If it's not that date what you're looking for, send me an email with the complete information of the array that you have printed on screen the first time. Also, a little explanation about how did you create that field "authored on"( through a custom module or CCK or however you did it ).
My email address is francort@ing.uchile.cl
good luck!
Success!
To answer your question first, the "authored on" date in question was native to Drupal and was not created by me.
After experimenting with the info from the array, I believe the 1217016392 date in the array above is the date I'm after ($result['node']->changed, 'small'). Here what I'm now using in template.php:
I assume this is pretty much optimized? In any case, my searches now show the date corresponding with the "authored on" field.
Thanks again!
one last thing
I'm glad to head that the problem was fixed :)
The function should be optimized since it was copied from seach module.
Just one more thing:
You must replace the line with:
if (!empty($result['node']->created)) {for
if (!empty($result['node']->changed)) {Because If you're printing $result['node']->changed then that's the condition for not being empty.
can this be applied to 5.x
can this be applied to 5.x search module? Or any suggestions as to how to modify it to work for 5.x?
You can do something
You can do something similar. What do you want to do? Are you using zen subtheme also?
not using a zen subtheme,
not using a zen subtheme, just wanted to alter the theme_search_item in template.php, something like what's been done here
http://drupal.org/node/86987
I think I need to change this line
if ($item['date']) {
$info[] = format_date($item['date'], 'small');
}
but I don't know what to? Any ideas?
This is an example. In this
This is an example. In this example i've changed the "small" date to a "large" date.
This funtion should be in template.php for drupal 5.x( of course for a garland type theme )
I hope this can solve your problem
Good luck!!!
hey, thanks, but all my
hey, thanks, but all my created dates are the same because of a node import. what I need to do is substitute the authored on date, which has been copied over from the old content. do you know what the variable is, or even better, I tried to find where all this kind of stuff is listed in the API, but didn't have much luck
try it replacing
try it replacing $item['date'] for one of these
$item['node']->created
$item['node']->changed
inside the function above, like:
Try it with $item['node']->changed as well
Thank you very much, worked
Thank you very much, worked like a charm
bookmarking for later...
bookmarking for later...
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.