0

I created a view block based search API module.

I want displayed in this block similar products to the product currently being viewed.

When I get the word "mint" in the value of fixed contexuel filter "full text search", I have products containing "mint" that appear in the right column.

I want the text title field and body field are sought instead of "mint".

I parameterize filters API search index, not to seek unnecessary words.

How to do this ? How to set the pop filter?

Thank you

enter image description here enter image description here enter image description here

1 Answer 1

0

You are viewing a Product Display Page. Therefore your context is the nid of the Product Display you are looking at. You now need to load the title and body field and pass that as a string to your view's contextual filter.

You can do this in various ways:

  1. Use hook_views_pre_view in a custom module

Allows altering a view at the very beginning of views processing, before anything is done.

Adding output to the view can be accomplished by placing text on $view->attachment_before and $view->attachment_after.

Parameters

$view: The view object about to be processed.

$display_id: The machine name of the active display.

$args: An array of arguments passed into the view.

  • You implement this hook in a custom module

  • check the $view argument for your view's name and display id

  • load the Product Display using menu_get_object which will return the $node.

  • Inspect $node to get the title and body, which you then concatenate and you then

  • set this as the argument on $args which is one of the parameters passed by reference to hook_views_pre_view

.

  1. Use Views Field View

There are a lot of cases in views where you want to embed a list inside each row. One example could be, you have a list of groups, and for each groups you want to list the first and last name of all of their members.

This can be achieved with this module, as views_field_view allows you to embed one view into another view and pass arguments along.

For this method you create a parent view block of Product Display (nodes) and for the contextual filter take the node id from the url. This is the view that will display your related products. After you have constructed it test it so that it shows the exact same product as the product page you are embedding it on.

So now you have a block that shows the same Product Display as the main page shows. Now you need to link it to the search api view.

First change the fields. Make sure that you have title field and body field added to your parent view, then set them to be hidden. Add a global text field and using the tokens from the above two fields add them with a space between them as in : [title] [body] so that the title and body is now one field. Again set this field to be hidden.

Because you installed Views Field View you can now add another field Global:View field. This field allows you to select a view to embed. You now select your search api view. It also allows you to pass parameters to the view, so you select the token for the global text field that is hidden.

Lastly set caching to some time value for this view so that it performs quickly.

ADDED

For option 1 (custom module) the code will go roughly like this (I currently don't have Drupal installed so I can only provide pointers. You will have to inspect the $node object via dpm() to get to the body field, which may be on the Product Display node or on the referenced Product type)

function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == "VIEWMACHINENAME" && $display_id == "VIEWDISPLAYID") {
    $node = menu_get_object(); // defaults are fine
    dpm($node); // comment out when code works
    if (isset($node->content...)) {  // inspect output from dpm to get field
      $argument = strip_tags($node->content...); // same field from if statement
      $args = array($argument);
    }
  }
}
8
  • Thank you for your reply. For solution 1, can I use in the contextual filters "provide the argument by default" and "type: php code" instead of a custom module ?
    – user54067
    Commented Jul 4, 2016 at 9:15
  • Yes you can although it is no longer considered good practice. Commented Jul 4, 2016 at 11:10
  • I know CSS and make use interface drupal but I very badly know the PHP language. I've updated the question with a new capture. Can you php code to clean up method 1? I have to create a "views-view--relation-produit--block.tpl.php" file in the templates folder of my theme? thank you
    – user54067
    Commented Jul 4, 2016 at 13:25
  • I thought I only want to use the body as a search field (so no title field). Can you tell me what php code must be inserted ?
    – user54067
    Commented Jul 4, 2016 at 21:42
  • I updated my answer. The code pointer is for a custom module and not for Views PHP Commented Jul 5, 2016 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.