1

I'm trying to create a restful view which takes a string as input, search specific content-type titles for similar nodes and return them as result.

The problem is I can't figure out how to configure the contextual operation to act as contains.

4
  • You wouldn't do this in a contextual filter, do this in a regular filter & you'll need to programmatically pass the value to the regular filter.
    – No Sssweat
    Commented Dec 31, 2017 at 3:12
  • @NoSssweat Can you guide me how to "programmatically pass the value to the regular filter" in drupal 8? Is there any replacement for hook_views_pre_build ? Commented Dec 31, 2017 at 7:04
  • When you expose a filter, you can set an argument key to use as input to this exposed filter. E.g. foo?search=xxx, Given your requirements, you might want to either use the SearchAPI and it's views structure or code your own rest handler. The SearchAPI lets you easily set up full text searches and manage results order based on boosts (e.g. words found in titles are more important). The DB backend works well in D8 so you don't need Solr.
    – CG Monroe
    Commented Jan 3, 2018 at 20:57
  • @CG Monroe thanks, solution helped, plz add it as an answer so I can accept and assign bounty to it Commented Jan 3, 2018 at 21:39

3 Answers 3

3
+50

When you expose a filter, you can set a filter identifier. This can be used to set the filter value via the URL, E.g.: /search?keywords=foo or /search?keyworks=foo+bar.

Note that if you need to hide this filter in a user view, you can do that with CSS or the Views Block View Filter module which allows you to move exposed filters to separate blocks that don't need to be placed on a user page. Not part of your specs.. but nice to know if you need REST and user pages.

Also, given your requirements, you might want to consider either using the Search API module with it's views structure or code your own rest handler.

The Search API module will lets you easily set up full text searches and manage results order based on boosts, e.g. words found in titles are more important. The DB backend works well in D8, so you don't need Solr server for this. (Though Solr has a lot of neat features).

Coding your own shouldn't be too hard with the D8 rest implementation. You can then do any query variations you need.

0

Create a rest view as per the image and access the results by this link YOUR-WEBSITE.COM/api/v1/schools?name=COLLEGE_NAME

enter image description here

0

I copied this answer from this Drupal Stack Exchange Page since it was still relevant.

The following code searches the standard body field for a string based on a URL Path specified in Views without the Search API module. In my case, the search string was the 5th element in the path. Not sure it's necessary, but it would be good to escape the URL element to prevent SQL injection. Here is a link to the updated Drupal documentation for the hook. You will want to include this in a .module file. The view this function was attached to was a REST export for a node-based content type which included several fields. You will want to replace the text in all caps.

/**
 * Implements hook_views_query_alter().
 */
function MYMODULE_views_query_alter(ViewExecutable $view, Drupal\views\Plugin\views\query\QueryPluginBase $query) {
  if ($view->id() ==  'MY_VIEW_ID') {
    $configuration = [
      'type'       => 'LEFT',
      'table'      => 'node__body',
      'field'      => 'entity_id',
      'left_table' => 'node_field_data',
      'left_field' => 'nid',
      'operator'   => '=',
    ];
    $join = \Drupal\views\Views::pluginManager('join')->createInstance('standard', $configuration);
    $rel = $query->addRelationship('my_alias', $join, 'node__body');
    $query->addTable('node__body', $rel, $join, 'my_alias');
    $current_path = \Drupal::service('path.current')->getPath();
    $path_args = explode('/', $current_path);
    $value = $path_args[5];
    $query->addWhere('0', 'my_alias.body_value', $value, 'REGEXP');
  }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.