2

I want to have a views with this Filter criteria

Content: Nid (= 2410)‎
OR
Content: Type (= news) AND
Content: value (= high) AND
Content: published (Yes)
OR
Content: Type (= multimedia) AND
Content: category (<> cinema) AND
Content: piblished (Yes)

and Sort criteria :

Content: Post date ‪(desc)‬

and just need to display 10 items.

actually, I need to force views to bring one or more NIDs. But when it sorts the results by post date if the specified nid is older than the 10 first results it does not appear in the results.

How can I solve the problem?

11
  • What does that set of filters give you? Mar 12, 2017 at 12:34
  • Depending on your use case you could create two views and display them on the same page if your forced nid not being sorted is not a problem.
    – schlicki
    Mar 12, 2017 at 13:17
  • @NiallMurphy the specific nid may be an advertising content which I wana show in the middle of other contents
    – shekoufeh
    Mar 12, 2017 at 13:22
  • @schlicki as I say in the above comment. I want to show some advertising content in the middle of other content. The solution must be user friendly as I need to enable or disable ads every day.
    – shekoufeh
    Mar 12, 2017 at 13:25
  • Ah now I got it. Is writing custom code an option?
    – schlicki
    Mar 12, 2017 at 13:39

2 Answers 2

1
+50

I can think of two solutions. I will improve my solution a bit if you clarify exactly what the concept is behind the fixed node ID.

Solution 1: Custom field

  • Create a custom boolean field (working just like the node.sticky property).
  • Filter and sort your view by the boolean property.

The advantage of this approach is that the admin would be able to change the hard-coded default node you want to display by simply updating the custom field from the node form. Also, they would be able to specify more than one such forced-appearance nodes, which is good in certain cases.

Solution 2: Custom sort handler

  • You can write a custom views sort handler and add an ORDER BY n.nid = 2410 DESC.
  • You will have to ensure that this sort is placed before the other sorts though.

BTW, as an additional suggestion, if you are using a hard-coded nid in logic, then make sure nobody can delete that node, otherwise you will have to change the relevant code every time someone deletes that node by mistake.

Hope this helps! Good luck.

0

I suggest you remove that specified node form that display and add it as attached OR another solution divide the what do you did into two view display and with getting help of hook_views_pre_render combine them (helper question)

and Third solution is remove 10 item limit and with getting help of hook_views_pre_render you should manually limit it to 9 result ordered by date and the specified NID. try something like following snippet (I don't have drupal 7 to test it, But take the Idea )

function HOOK_views_pre_render(&$view) {

  if($view->name == 'VIEWS_MACHINENAME' && $view->current_display == 'DISPLAY_MACHINE_NAME') {

    if(empty($view->result)){
      return;
    }
    $other_items = array();

  foreach($view->result as $item){
      if($item->nid == 2410){
       $special_item = $item;
      }else{
       $index = $item->created . $item->nid; // maybe some nodes has the same created time
       $other_items[$index] = $item;  // set index of array to created for sorting
       }
    }
    krsort($other_items); // sort by key descending , key = publish date
    $final_items = array_slice($other_items, 0, 9);
    $index = $special_item->created . $special_item->nid;
    $final_items[index] = special_item;
    krsort($final_items); // sort again to have correct sorting with special Item
    $view->result =  $final_items;
  }
}

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.