Skip to main content
Tweeted twitter.com/StackDrupal/status/711379629836267520
remove erroneus informations
Source Link

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


I have followed Adrian's code : I use the CONTEXTUAL FILTER with a condition on "equal to nid", and I alter the condition to relace "equal to" by "< to" .

it works when I do it for only one view on my page. For the second view :) (same view but with a different condition , it doesn't work. Below is my code. is it possible that the function cannot handle twice the query variable ?

`function fdrav_views_query_alter(&$view, &$query) {
  if($view->name == 'metier2') {
    /* 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'][]['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'][1]['field'] = str_replace('=', '>', $query->where[0]['conditions'][1]['field']);
  }

  if($view->name == 'metier3') {
    $query->where[0]['conditions'][1]['field'] = str_replace('=', '<', $query->where[0]['conditions'][1]['field']);
  }
}

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


I have followed Adrian's code : I use the CONTEXTUAL FILTER with a condition on "equal to nid", and I alter the condition to relace "equal to" by "< to" .

it works when I do it for only one view on my page. For the second view :) (same view but with a different condition , it doesn't work. Below is my code. is it possible that the function cannot handle twice the query variable ?

`function fdrav_views_query_alter(&$view, &$query) {
  if($view->name == 'metier2') {
    /* 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'][]['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'][1]['field'] = str_replace('=', '>', $query->where[0]['conditions'][1]['field']);
  }

  if($view->name == 'metier3') {
    $query->where[0]['conditions'][1]['field'] = str_replace('=', '<', $query->where[0]['conditions'][1]['field']);
  }
}

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

I post my new code here, since it is to long to go in a comment
Source Link

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

 

In FILTER CRITERIA I have addedfollowed Adrian's code : I use the CONTEXTUAL FILTER with a field "Global PHP" butcondition on "equal to nid", and I don't know howalter the condition to retrieve 11541relace "equal to" by "< to" (and compare.

it toworks when I do it for only one view on my page. For the rows nidsecond view :) (same view but with a different condition , it doesn't work. Below is my code. is it possible that the function cannot handle twice the query variable ?

`function fdrav_views_query_alter(&$view, &$query) {
  if($view->name == 'metier2') {
    /* 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'][]['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'][1]['field'] = str_replace('=', '>', $query->where[0]['conditions'][1]['field']);
  }

  if($view->name == 'metier3') {
    $query->where[0]['conditions'][1]['field'] = str_replace('=', '<', $query->where[0]['conditions'][1]['field']);
  }
}

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

In FILTER CRITERIA I have added a field "Global PHP" but I don't know how to retrieve 11541 (and compare it to the rows nid)

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

 

I have followed Adrian's code : I use the CONTEXTUAL FILTER with a condition on "equal to nid", and I alter the condition to relace "equal to" by "< to" .

it works when I do it for only one view on my page. For the second view :) (same view but with a different condition , it doesn't work. Below is my code. is it possible that the function cannot handle twice the query variable ?

`function fdrav_views_query_alter(&$view, &$query) {
  if($view->name == 'metier2') {
    /* 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'][]['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'][1]['field'] = str_replace('=', '>', $query->where[0]['conditions'][1]['field']);
  }

  if($view->name == 'metier3') {
    $query->where[0]['conditions'][1]['field'] = str_replace('=', '<', $query->where[0]['conditions'][1]['field']);
  }
}
edited tags; edited title
Link
Adrian Cid Almaguer
  • 18.5k
  • 14
  • 79
  • 133

Filter How to filter views rows whose nid is less than nid provided by url?

Source Link
Loading