0

I've created a view listing a content type named foo, it has a custom field 'start_date' which can be left blank.

When a node's start_date field is left blank, it should always show in the listing. When it's populated, and the date is less than the current date - then the node should also be listed.

I've tried to use hook_views_query_alter to do this, but it didn't do what I wanted as it's for changing the query itself.

Which hook should I use?

1

You can use the Views UI to accomplish this.

Add two date filters: one of them should filter date fields for "is empty" and the other should filter date fields for "is less than" and choose "Enter relative date" and type "now".

Then create an "OR" group and drag the two date filters to it. It should look like this:

enter image description here

  • Hi, @argiepiano. Thans for your kindly comment. It's a great answer! But i have a another question complementary.Is it possible only worked for a special user role. Example if current user's role is 'trainee', then the filter is efficient and if role is admin the filter was bypassed. – shi jack Dec 29 '15 at 2:59
  • I don't think it's possible to do that with Views UI. You could try creating a custom filter programmatically that returns true for admin and false for everyone else, and include that in two OR groups. A tutorial on creating a custom filter: metaltoad.com/blog/…. Otherwise, Views PHP is a module that is despised by many Drupal programmers, but it may work for something like (keeping in mind that it poses many serious security risks.) – argiepiano Dec 29 '15 at 3:31
  • One more thing, since you are new to Drupal Answers - if you find answers useful you can vote them up or mark them as correct. That encourages people to provide other answers. – argiepiano Dec 29 '15 at 3:32
  • The tutorial above may not be that helpful in your case. – argiepiano Dec 29 '15 at 3:44
  • Take a look at this post. This may have what you need: drupal.stackexchange.com/questions/98551/… (look at the answer marked as correct) – argiepiano Dec 29 '15 at 3:58

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.