3

Drupal 7. In a node/edit form panel I have added a view that returns nodes. I whish to filter thoses nodes and keep only the nodes whose nid is less than the nid of the node beeing edited.

the url of the node/edit form is : /node/11541/edit?step=11538

the nid of the node being edited is : 11541

I wish the view to return only the nodes whose nid < 11541

I use Views PHP

  • this is for Drupal 7? Because I don't have test my code in Drupal 8 – Adrian Cid Almaguer Feb 5 '16 at 18:10
4

You can implement the hook_views_query_alter() to alter your query and change the = operator by the < operator.

function YOUR_MODULE_NAME_views_query_alter(&$view, &$query) {
  if($view->name == 'YOUR_VIEW_NAME') {
    /* Using the following line dmp($query)
     * (you need the devel module to use dpm) you will be able to search
     * where is condition to alter, you will see that in the 
     * $query->where[0]['conditions'][0]['field'] you will have:
     * node.nid = :node_nid
     * so your solution will be replace the 
     * = operator by the < operator
     * at this moment you can't do this from the Views UI
     */
    dpm($query);

    $query->where[0]['conditions'][0]['field'] = str_replace('=', '<', $query->where[0]['conditions'][0]['field']);
  }
}

I test the code and in my environment it works as I want it to.

Here is my view for if you want test the code (in this case the view name is articulos):

$view = new view();
$view->name = 'articulos';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'articulos';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'articulos';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: Content: Nid */
$handler->display->display_options['arguments']['nid']['id'] = 'nid';
$handler->display->display_options['arguments']['nid']['table'] = 'node';
$handler->display->display_options['arguments']['nid']['field'] = 'nid';
$handler->display->display_options['arguments']['nid']['default_argument_type'] = 'fixed';
$handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'article' => 'article',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'articulos/%';
$translatables['articulos'] = array(
  t('Master'),
  t('articulos'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort by'),
  t('Asc'),
  t('Desc'),
  t('All'),
  t('Page'),
);
  • Thanks Adrian. It works great for one view in ma page, but not for the second view (see my code in my edited question), can my function handle 2 views with same query variable ? – Brigitte Charpent Feb 7 '16 at 21:06
  • Bienvenue dans le monde +5K ... PS: "... and it works" sounds a bit strange/bizarre. I understand you use it as a kind of quality assurance label (which is fine), but maybe you should use a phrasing like "in my environment it works as I want it to" ... – Pierre.Vriens Mar 18 '16 at 20:13
  • 1
    @Pierre.Vriens merci, ;-) – Adrian Cid Almaguer Mar 18 '16 at 20:14
2

You can use a contextual filter for your view:

  • If you are using a page display, you will be able to retrieve the nid from the URL.
  • If you are using a block display, you will need to do it a bit differently. See Contextual filter with a block view
  • Thanks for helping. I use a page display and a contextual filter for another parameter : [view:metier=page_1=1] If I'd wish to filter on "nid= " I know that I could use a contextual filter, but how to use a contextual filter in order to filter nid " is less than " ? – Brigitte Charpent Feb 5 '16 at 14:34
  • Sorry I read you to fast. you say for a page display I could retrieve nid from URL. But I don't see how to do it. – Brigitte Charpent Feb 5 '16 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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