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

francort’s picture

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:

  function MYZENSUBTHEMENAME_preprocess_search_result(&$variables){
  $result = $variables['result'];
  $variables['url'] = check_url($result['link']);
  $variables['title'] = check_plain($result['title']);

  $info = array();
  if (!empty($result['type'])) {
    $info['type'] = check_plain($result['type']);
  }
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
  }
  if (!empty($result['date'])) {
    $info['date'] = format_date($result['date'], 'small');
  }
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
  }
  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  // Provide separated and grouped meta information..
  $variables['info_split'] = $info;
  $variables['info'] = implode(' - ', $info);
  // Provide alternate search result template.
  $variables['template_files'][] = 'search-result-'. $variables['type'];
}

..and then , you should comment what you need to hide

silurius’s picture

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.

francort’s picture

I think "authored on" information may be inside $result or $variables array. Perhaps doing this, it is possible to find it

  function MYZENSUBTHEMENAME_preprocess_search_result(&$variables){
  $result = $variables['result'];
  print '<pre>'; 
  print_r( $result );
//if it wasn't there, maybe it's here
//print_r( $variables );
  print '</pre>';
  die;
//.....etc

That one will find what's inside $result and/or $variables. If somewhere there is "authored on", then replace the line "date". Something like:

//just an example, but i hope it's there :)
if (!empty($result['node']->authored_on)) {
    $info['authored_on'] = format_date($result['node']->authored_on, 'small');
  }

I believe something like that should work :)

silurius’s picture

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:

  ob_start();
  print eval('?>'. $code);
  $output = ob_get_contents();
  ob_end_clean();

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.

francort’s picture

Sorry if i wasn't specific enough. I just have written parts of the entire code.
The complete code, should be as follows:

  function MYZENSUBTHEMENAME_preprocess_search_result(&$variables){
  $result = $variables['result'];
  print '<pre>'; 
  print_r( $result );
//if it wasn't there, maybe it's here
//print_r( $variables );
  print '</pre>';
  die;
  $variables['url'] = check_url($result['link']);
  $variables['title'] = check_plain($result['title']);

  $info = array();
  if (!empty($result['type'])) {
    $info['type'] = check_plain($result['type']);
  }
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
  }
  if (!empty($result['date'])) {
    $info['date'] = format_date($result['date'], 'small');
  }
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
  }
  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  // Provide separated and grouped meta information..
  $variables['info_split'] = $info;
  $variables['info'] = implode(' - ', $info);
  // Provide alternate search result template.
  $variables['template_files'][] = 'search-result-'. $variables['type'];
}

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.

if (!empty($result['date'])) {
    $info['date'] = format_date($result['date'], 'small');
  }

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 (!empty($result['authored_on'])) {
    $info['authored_on'] = format_date($result['node']->authored_on, 'small');
  }

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 :)

silurius’s picture

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:

Array
(
    [link] => http://qa.mysite.com/path_to_node
    [type] => [CONTENTTYPE]
    [title] => [NODETITLE]
    [user] => [USERNAME]
    [date] => 1217016392
    [node] => stdClass Object
        (
            [nid] => 43
            [type] => [CONTENTTYPE]
            [language] => 
            [uid] => 62
            [status] => 1
            [created] => 1180314600
            [changed] => 1217016392
            [comment] => 0
            [promote] => 0
            [moderate] => 0
            [sticky] => 0
            [tnid] => 0
            [translate] => 0
            [vid] => 44
            [revision_uid] => 1
            [title] => [NODETITLE]
            [body] =>
. . .

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.

francort’s picture

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:

  if (!empty($result['date'])) {
    $info['date'] = format_date($result['date'], 'small');
  }

for this

  if (!empty($result['node']->created)) {
    $info['date'] = format_date($result['node']->created, 'small');
  }

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!

silurius’s picture

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:

function MYZENSUBTHEMENAME_preprocess_search_result(&$variables){
  $result = $variables['result'];
//if it wasn't there, maybe it's here
  $variables['url'] = check_url($result['link']);
  $variables['title'] = check_plain($result['title']);

  $info = array();
  if (!empty($result['type'])) {
    $info['type'] = check_plain($result['type']);
  }
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
  }
  if (!empty($result['node']->created)) {
    $info['date'] = format_date($result['node']->changed, 'small');
  }
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
  }
  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  // Provide separated and grouped meta information..
  $variables['info_split'] = $info;
  $variables['info'] = implode(' - ', $info);
  // Provide alternate search result template.
  $variables['template_files'][] = 'search-result-'. $variables['type'];
}

I assume this is pretty much optimized? In any case, my searches now show the date corresponding with the "authored on" field.

Thanks again!

francort’s picture

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.

thomasmurphy’s picture

can this be applied to 5.x search module? Or any suggestions as to how to modify it to work for 5.x?

francort’s picture

You can do something similar. What do you want to do? Are you using zen subtheme also?

thomasmurphy’s picture

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?

francort’s picture

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 )

function phptemplate_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'];
  }
//change this date as you want
  if ($item['date']) {
    $info[] = format_date($item['date'], 'large');
  }
  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;
}

I hope this can solve your problem

Good luck!!!

thomasmurphy’s picture

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

francort’s picture

try it replacing $item['date'] for one of these

$item['node']->created
$item['node']->changed

inside the function above, like:


//bla bla , code before

//change this date as you want
  if ($item['node']->created) {
    $info[] = format_date($item['node']->created, 'large');
  }

//bla bla code after

Try it with $item['node']->changed as well

thomasmurphy’s picture

Thank you very much, worked like a charm