I really need to have search result row styles in some of my views. I have only recently moved to D6 + Views 2 so I am still only learning the new template related changes in D6 and views 2.

BUT, this is how I got it to work without any errors.

I removed the return disabled code in search_views_plugins() in search.view.inc.
Then I created a tpl file called views-view-row-search.tpl.php and placed it in the views/theme directory.
I copied the contents of the core search-result.tpl.php. http://api.drupal.org/api/file/modules/search/search-result.tpl.php/6/so... (could also just include the core tpl file).
Cleared my cache and rescanned for template files and it worked.

Hope that helps other people and hopefully this can be in the next release.

Comments

dawehner’s picture

do you want to write a patch for it?

for example for the documentation.

I think you can copy the tpl also to your theme and not to views itself

merlinofchaos’s picture

Status: Needs review » Needs work

Interesting. I see that this works, and this is good, but it seems like we should be using theme('search_result') so that everything funnels into the right template. That means re-organizing things so that the data is put properly into a result array and passed through normally. And it should also use the views theme functions so that search_result__view__etc works along with everything else.

I started looking into doing this, but it's a little more work than I want to do right now, but I would welcome patches.

NaX’s picture

I don’t know about using theme('search_result') but another thing that I found is that in template_preprocess_views_view_row_search() we need to set the $node->build_mode

EG:

function template_preprocess_views_view_row_search(&$vars) {
  // ...
  $node = node_load($nid);

  if (empty($node)) {
    return;
  }
  
  $node->build_mode = NODE_BUILD_SEARCH_RESULT;
  // ...
}
dawehner’s picture

One quick observation:

views_view_row_search.tpl.php is missing

ptitb’s picture

This post was very usefull for me to display the exposed filter results as search results.
I only found that the search_excerpt function didn't highlight the search terms found.

With the following alterations I was able to get this working.
Not sure if this is the best way to get this done, but I'm not a php expert.

  // Get value from first key in exposed_input for search_excerpt
  $search_input = reset($vars['view']->exposed_input);
  // Use this value for highlighting search term
  $vars['snippet'] = search_excerpt($search_input, $node->body);
ambientdrup’s picture

Where is this file (search.view.inc.) mentioned at the beginning of this post? I can't locate it in Views2.

-backdrifting

NaX’s picture

@ambientdrup
This is all the files you need to deal with.

views/modules/search.views.inc
views/theme/views-view-row-search.tpl.php

@ptitb
To get the snippet to work I do the following. I also don't know what the best way is.
Your way looks simpler and I am hard coding the Filter identifier.

  // Get filter keys from views object
  // Need to find a way not to hard code 'keys'
  if (!isset($vars['filter_options'])) {
    $vars['filter_options'] = $vars['view']->display_handler->handlers['filter']['keys'];
  }
  // Reduce memory
  if (isset($vars['filter_options']->view)) {
    unset($vars['filter_options']->view);
  }
  // Set search identifier
  $vars['identifier'] = $vars['filter_options']->options['expose']['identifier'];
  
  $vars['snippet'] = search_excerpt($vars['view']->exposed_data[$vars['identifier']], $node->body);
esmerel’s picture

Status: Needs work » Postponed (maintainer needs more info)

Marking this "needs more info"; if the full doc writeup is going to be done, great! If not, then the issue needs to get cleaned out.

NaX’s picture

Version: 6.x-2.6 » 6.x-2.11
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new5.05 KB

The attached patch is what works best for me after some more experimentation.
This patch does not address merlinofchaos point in #2.

Please note that normally I generate patches with winmerger but this patch was made with Kompare on Suse and it’s my first time using Kompare.

dawehner’s picture

StatusFileSize
new5.06 KB

Here is a version which uses theme_search_result.

    // Reduce memory
    if (isset($vars['filter_options']->view)) {
      unset($vars['filter_options']->view);
    }

Is this only true for php4?

NaX’s picture

@dereine
No, that is only relevant to shared hosts that don’t give you enough memory to output view objects to screen while debugging. It can safely be removed.

dawehner’s picture

@NaX
If you use the devel module's dpm function you can do it. At least for me it's working perfect

NaX’s picture

@dereine
I was using dsm() and I was about the to ask about the difference when I saw that dsm() is now considered a legacy function. Thanks.

dawehner’s picture

Yes, dsm is now just an alias for dpm.

Sorry for "spamming" the issue queue.

merlinofchaos’s picture

Category: bug » feature

I would love it if some bug squad folks could test this patch. Code review optional but valuable, but just knowing that people have gone through the steps to make sure this does what it says it does would help me.

interestingaftermath’s picture

subscribe - could this also be considered for 3.x?

dawehner’s picture

The question is whether this will be considered for 6.x-2.x or not. :)

If you subscribe, did you tested the patch?

interestingaftermath’s picture

I did test the patch in 3.x and it seems to work if I would get the theme templates setup correctly. However, I couldn't take the risk of having to worry about keeping up with the patch as 3.x continues to be upgraded. The site I would be using this for uses views heavily and wants to be using the latest/greatest. I subscribe in hopes that someone smarter than I can upgrade this to 3.x and get it committed :)

mikeker’s picture

Version: 6.x-2.11 » 6.x-2.x-dev
StatusFileSize
new5.35 KB

Corrects syntax error (missing semicolon).

I'm not "bug squad" but I'll give it a bit more testing this evening...

Dasha_V - old’s picture

During the testing for the views 3.x it seems work fine initially.
But when theming 'search-result.tpl.php' - results stop showing because the available variables changed.
There is $view array instead of $url, $title, $info_split, etc.
This fix on the top of overwritten 'search-result.tpl.php' works for me

if (!is_array($info_split) && $view) {
  $info_split = $view;
  extract($info_split);
}

Do you know what can cause this issue?

NaX’s picture

Status: Needs review » Needs work

Using Views 2.12 and the latest patch from #19 I ran into some minor problems.

First I got include errors.

warning: include(./sites/all/modules/views/theme/search-result.tpl.php) [function.include]: failed to open stream: No such file or directory in

I solved this by copying the search modules search-result.tpl.php into the views/theme directory. I assume putting it into my theme would also solve this error.

But then I ran into the same problem that Dasha_V ran into. I put his workaround into the views/theme/search-result.tpl.php.

I also tried the search-results.tpl.php with the correct filename "views-view" prefix, that also did not work.

It looks like we need a preprocess function for something, to change the template file being used to the search modules template file and to extract the correct variables.

NaX’s picture

To get the info variables working in the tpl file I also changed the render function to build the info variables.

    $info = array(
      'type' => check_plain(node_get_types('name', $node)),
      'user' => theme('username', $node),
      'date' => $node->changed,
    );    
    if (isset($extra) && is_array($extra)) {
      $info = array_merge($info, $extra);
    }    
    $result = array(
      'link' => url('node/' . $nid, array('absolute' => TRUE)),
      'type' => $info['type'],
      'title' => $node->title,
      'user' => $info['user'],
      'date' => $node->changed,
      'node' => $node,
      'extra' => $extra,
      'score' => $score,
      'snippet' => $snippet,
      'info' => implode(' - ', $info),
      'info_split' => $info,
    );

Is there a reason we are doing this. If we are suppose to divide the score by the total, should the total not be relative the view instead of the entire node table. If this is correct should we not be using a static cache for this query.

    if ($this->options['score']) {
      if ($total = db_result(db_query('SELECT COUNT(*) FROM {node} WHERE status = 1'))) {
        $score = $row->score / $total;
      }
    }
damienmckenna’s picture

StatusFileSize
new815 bytes

How about exposing it using the NODE_BUILD_SEARCH_RESULT node build mode?

damienmckenna’s picture

FYI you can also implement my suggestion via hook_form_alter():

/**
 * Implementation of hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  // Only work with the Views edit form for modifying the node output style.
  if ($form_id == 'views_ui_edit_display_form' && $form['#help_topic'] == 'style-node') {
    // The Search module does not have a build mode exposed, it is a
    // "hidden" build mode, so manually expose it.
    if (module_exists('search')) {
      $form['row_options']['build_mode']['#options'][NODE_BUILD_SEARCH_RESULT] = t('Search Result');
    }
  }
}
NaX’s picture

Version: 6.x-2.x-dev » 7.x-3.x-dev
StatusFileSize
new8.86 KB

If anybody is interested the attached patch is how I hacked D7 to get it to work. Its mostly based on the above. It should only work for nodes, but with the entity systems it should be possible to make it work for any entity.