4

I have created the view. It displays all comments from a certain time period. So I need filter created date and between option, isn't it?

But also I need to get these MIN and MAX options from node: MIN - created node time and MAX - node`s filed "to_date".

So, How can I pass these parameters to view? Is here some solution?

enter image description here

Edit:

Case: I should create contest module(contest for most popular comments).

Process: Admin creates a node of type Contest. There he must specify contest "To date" and add the description. So in order to determine contest winners, I have created view, that must sort all comments by total views and filter by range date(node created and "To date" field) from node on which page view is displaying. I need to display this view under node: I do it in this way:

$page['content']['winners'] = array('#markup' => views_embed_view('contest_winners_one', 'block'));

But how to change view filter for every view instance?

4
  • Why do you to filter between min and max? What's the goal? To display all comments by default?
    – Shawn Conn
    Jun 10, 2016 at 15:47
  • @ShawnConn i need to display comments from the time period. For example, all comments created from 10.06.2016 to 13.06.2016
    – Q-bart
    Jun 10, 2016 at 20:57
  • It might help to state your whole use case. Where is the date range coming from? Is it supplied by your users? If so, ticking the Expose this filter... checkbox will enable a form and query parameters (for View pages) from which to supply a date range.
    – Shawn Conn
    Jun 10, 2016 at 21:11
  • Edit question. More text about use case
    – Q-bart
    Jun 11, 2016 at 9:54

3 Answers 3

1
+50

You should pass nid to views by contextual filter to restricts results to specified nodes, then for Comment Post Date filter I have two solution to pass your filter value programmatically to view filter

you can expose your Post comment ( created) filter and hide it by css( or in views template remove markups ) then in your code

Notice:this code is example please dsm $view to precises filter machine name

 $view = views_get_view($name);
  $view->set_display($display_id);

  $start = '2015-12-01 00:00:00';
  $end = '2015-12-31 00:00:00';
  $view->exposed_input['created'] = array('min' => $start, 'max' => $end);
  //$r = $view->get_exposed_input();
  //dsm($r);
  $view->pre_execute();
  $view->execute();
  $r = $view->result;

another solution is pass your values to filter (without expose them)

 $view = views_get_view($name);
  $view->set_display($display_id);
  $item = $view->get_item($display_id, 'filter', 'date_filter');
  //$item['value'] = array('value'=>'2015-01-01 00:00:00');
  $item['default_date'] = '2015-01-01 00:00:00';
  $item['default_to_date'] = '2015-12-01 00:00:00';
  $view->set_item($display_id, 'filter', 'date_filter', $item);
  $item = $view->get_item($display_id, 'filter', 'date_filter');
  $view->pre_execute();
  $view->execute();
  $r = $view->result;
  dsm($r); 

I had the same requirement after some research found this issue and I resolved my problem.maybe help you too.

3
  • The second variant seems good, Thanks. But, I have already done this task by changing task conditions... Thanks again.
    – Q-bart
    Jun 18, 2016 at 7:44
  • @Q-bart sorry I see the question late, sometimes I am very busy and forget come here and help Drualist :(
    – Yuseferi
    Jun 18, 2016 at 7:45
  • @Q-bart you're welcome, but sometimes late waste our time;) . good luck dude
    – Yuseferi
    Jun 18, 2016 at 7:48
1

If the comments starting from the publication of the node are valid, you do not need a start date. You only need to make sure you load all comments for that single node and an end date.

Since your view lists comments, I suggest you start by adding a contextual filter for the Comment: nid. This limits all the comments to those on that specific node only. For this to work in your use case, you need to update views_embed_view to also send the node id along (all parameters after the second parameter are sent as arguments (contextual filters)):

views_embed_view('contest_winners_one', 'block', $nid);

Now the results are limited to those on this node only, you need to compare the post date. If the To date is formatted properly (Drupal supports multiple date formats and out of my head I do not know the right one, but my guess is that the date needs to be stored as a timestamp), you can do a field comparison.

For this you have to add the fields to compare to the Fields section of the View (if there is no option to add fields, you can force enable in the dialog you get when clicking settings behind Format. These are the Comment: creation date and the Content: To date fields. Display format does not matter and you can select Exclude from display.

Then in the Filter section add the Field comparison filter. Then make sure the comment date is smaller or equal to the to date.

If you fail to get the contextual filter working, you can also use this approach for the content creation date and the comment creation date, but without node filtering, comments would get mixed up if two contests run at the same time.

Additionally you can probably use Rules and the Rules scheduler sub-module to disable comments on the node once the to date is reached. Refer to How do I create a rule that closes comments on node after 30 days? for reference on how to achieve this. If the comments are closed on the correct date, you can safely show all comments as they could only have been created in the defined period. (You wouldn't need any filtering)

0

Took me a while to figure it out, so for D8/9:

Assuming the field you want to filter on is the "created" one.

$view = \Drupal\views\Views::getView($view_machine_name);
$view->setDisplay($display_name);
// Contextual filters.
$view->setArguments([$arg1, $arg2]);
// Get values for filter handler.
$values = $view->getHandler($display_name, 'filter', 'created')['value'];
// Set new values for created filter.
$view->setHandlerOption($display_name, 'filter', 'created', 'value', array_merge($values, [
  'min' => 'YYYY-MM-DD 00:00:00',
  'max' => 'YYYY-MM-DD 00:00:00',
]));
$view->execute();
$renderable_array = $view->render();

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.