Hello i am using views 7.x-3.2 and the References module. -user reference-

I am trying to create a page for users on the site that will list their appointments. Added by themselves or another role -receptionist-

I initially tried to do this by content author but i realized their was no good way to fake the author.

So the better way would be to make a user reference field and let the receptionist select the user from a list. also the user would be forced to select from the same list.

As I cannot let them see other users names etc. I tried making a new content type that uses the same field but a different references view. this didn't work because it is a global setting for all content types with that field.

So I backed up a little and thought what if i could have a unique field.

Then i would encounter the exact same issue that i am presently faced with.
The contextual filter excludes all types without that particular field. And And vs And OR
the regular filter is static.

I have created a view with a page that is called my appointments and it is using a contextual filter that is filtering by the logged in user, for the user reference field.

contextual filter: Content: Patient Name (field_patientselect)

I have tried using one content type -appointment- but there is no way to limit the user reference list despite the users permission to view there profile or even field. when i limited there field permission it returned rows of users without the fields they were not supposed to see. but they got the whole list of users as blank lines they could still select.

So i though two content types, with the same field. the same field because couldn't have the -my appointments- view contextual filter, the And And vs And OR thing again.

one more idea i had was to have two views one that limits by logged in user and the other a list of all. as i couldn't use multiple views references for one field -global- i tried the no results behavior to pull up the all users view when the logged in user view failed.

this had a multitude of issues ie. they are both logged so there is always a result. so i did the same no results backwards where there would be no result. but i still got an empty list -maybe due to it being a references view?-

I have looked around for days,

http://drupal.org/project/nodeaccess_userreference this looks similar but is not
http://drupal.org/project/entityreference This is supposed to be better but it would just be trowing a new mod in when i am trying to limit the views reference by permission

Question: Can i use contextual filters in the same AND/OR way as the regular filter. -ideal-

Question: do i just not know how to use multiple contextual filters together and maybe the no results behavior

Question: Am i just missing a better way of doing this whole appointment scheduling thing? -not a views issue-

At the end of the day i am hoping someone can point me in the right direction, and every way i can think of requires contextual filters to have And/Or

Thanks in advance!

Comments

John Morahan’s picture

I'm not sure I fully understand what you're trying to do. But there's a "User: Current" filter which filters by the current logged in user's uid, and is a normal filter and not a contextual filter, so this might be of some use. Your view needs to have a relationship to a User (or be a User view) for this filter to be available.

srlawr’s picture

Hi John.

I can't seem to get this "User: Current" filter to appear as an option in my view... can you possibly elaborate a little for me?

I have a view, of "Assignments" - which has a user reference to a user in it called "governor" - I have added a Relationship to the user object via this governor field - and I know that works, because in the fields I can add fields through this relationship, and, say, include the DoB of the governer on any given assignment. HOWEVER I can't (through filters) make the view JUST show the assignments for which the logged in user is the governor... I CAN do this through contextual filters, but unfortunately that's no good, because I actually need to list assignments you are a governor of OR ones for which there is no governor... which contextual filters can't provide for as there is no "or" functionality that can be tied to an "is empty" condition.

That's probably terribly confusing, sorry! The original question stands though.. how did you get "User: Current" in your Views filter list?

(I'm using Views 2 on D7.12 if thats relevant).
Cheers.

John Morahan’s picture

Assuming you mean Views 3.2, as Views 2 doesn't exist for D7, I don't believe I did anything other than what you described. I don't know why the filter wouldn't be appearing for you.

srlawr’s picture

I've found it! Thank you. Your response gave me the resolve to persist with this as the solution, cheers.
(for the record, I've been on holiday, it didn't take me 4 more days!)

For anyone else who stumbles upon this; the User: Current filter is located in what I call the "field list" - the first stage of adding a filter.

I was expecting it to turn up on the second page, so I would add a user relationship, and then add a filter, selecting Content: Governor (field_assignment_govenor), and under "conditions" I would see "Current user", or I would select "Is equal to" and then see "Current user"... rather than User: Current being its own entirely encapsulated filter.

Horses for courses though, I'm just really glad this works. Cheers again.

stonewalker’s picture

so i was trying to use contextual filters in the AND OR fashion. use this contextual filter OR a different one. like the static filters. i looked alot more and if you click the ? button to the left of contextual filters at the bottom there is a answer to this exact problem.

Grouping of contextual filters
----------------
Even though contextual filters do not appear in the "and/or" user interface for sorting and grouping regular filters, contextual filters are always added to the first group of filters. Thus the order of the groups can cause the contextual filter to have entirely different effects on the results of a view that has contextual filters. Even though differences might not be apparent through the user interface.

Multiple contextual filters are therefore always in the same "and/or" group of filters, and can not be placed in different groups. There is an effort to add this feature.

----------------

So from what i gather is that if i had two contextual filters i could switch the first group of the static filter to AND/OR and it would give me what i need. even though they do not show up?

i have tried this and i cannot seem to get the correct behavior. besides me not being able to get it to work, i could not place only the contextual filters in the the First static group and the other static filters in a higher group. when i did this the first group disappeared as if it was empty. this would make sense as they do not show up?

does anyone know how to use multiple contextual filters in a way that is this one OR that one?? the help button seems to indicate that it is currently a feature of views.

i have been able to workaround this with rules. and renaming the author but i would love to know how to do this with views

srlawr’s picture

I made some attempts at doing what you describe during my investigations... I too read that the contextual filters are "added" to the first group of filters; and so I tried the following:

I added my contextual filters that I wanted to be ORed.

I then added a fairly redundant filter to my view, and used the filters groupings to make this the first "group" and that it was an "OR" group. This first group was then ANDed to the second grouping, that was also itself an AND group containing the rest of my filters.

(anyone still with me?!)

I ran the view, but did not get the expected behaviour. I used Devel to output the query, and what Views had done was to add the contextual filters to the first group, but appears to still have ANDed them to each other, and the group - despite it supposedly being an OR group.

I fiddled about with some other options to no avail, until finally solving my problem in the fashion described above anyway. I'm sure the Views module will catch up a more powerful feature for contextual filters soon ;)

Anonymous’s picture

I had a problem getting an OR for contextual filters as well. I ended up creating an Attachment that was attached to the page view, and had the attachment use the other contextual filter. Worked for me, but would be nice to have an OR option for contextual filters.

torotil’s picture

Priority: Normal » Major

It seems that the behavior changed with 3.1.

views-7.x-3.0 behaves exactly as described in:
----------------
Even though contextual filters do not appear in the "and/or" user interface for sorting and grouping regular filters, contextual filters are always added to the first group of filters. Thus the order of the groups can cause the contextual filter to have entirely different effects on the results of a view that has contextual filters. Even though differences might not be apparent through the user interface.

Multiple contextual filters are therefore always in the same "and/or" group of filters, and can not be placed in different groups. There is an effort to add this feature.
----------------

beginning with views-7.x-3.1 the contextual filters are still put in the first group - but the first group is always AND regardless of the settings.

I'm setting this to major as this breaks existing sites, and leaves them without an upgrade path.

torotil’s picture

The commit that broke the behavior is http://drupalcode.org/project/views.git/commit/e433b6519035357aab83a8d51...

It even says so in the commit message.

rvarkonyi’s picture

I've been in a similar situation, I ended up using hook_views_query_alter(&$view, &$query) to modify 'AND' to 'OR'. It's not a long term solution, but for now it will work...

damiankloip’s picture

Priority: Major » Normal
mbouchard58’s picture

My use-case is very similar to the one described in #2 above except with nodes instead of users. I needed to OR a contextual filter with my first static filter group, but, like everyone else couldn't get it to work. Ultimately, I used the same contextual twice on the view to get the where clause structure correct. I have a contextual filter on Internship_ID which defaults to a content ID in the URL. I added Internship_ID again as a contextual filter this time with a fixed value. I had to use hook_views_query_alter because I can't figure out a way to have the Fixed Value test for nullity and of course I had to override the type (i.e., the logical operator) to OR ($query->where[0]['type']). The nice thing about this approach though is the WHERE clause structure is setup properly. I changed the Value and the Operator (I had to re-write the field name too; it came through mangled otherwise; I don't know why).

Having the SQL displayed in the view was a *huge* time saver also (go to Views|Settings to turn it on).

Props (do people still say that?) to Drupalista extraordinaire TedBow for his help and advice.

John Pitcairn’s picture

Another way to deal with this is to setup the filter group structure entirely using standard filters, using appropriate dummy values where you would use contextual filters, and add no contextual filters.

Then hook_views_query_alter() and insert the appropriate arguments - either pulled directly from the URL with arg(), or if you're using views_embed_view(), you can still pass the arguments there and grab those from $view->args.

Turns out this is easier to get my head around than the way views manages contextual filters vs OR groups anyway (when that was working). I might keep doing it this way even when the broken behaviour is fixed, unless we get real contextual filter groups in the UI.

odegard’s picture

I did like #10 suggested.

My view is called 'statuser' and my module is called eb_mine

eb_mine.module:

function eb_mine_views_api() {
  return array(
    'api' => 3,
  );
}

eb_mine.views.inc:

function eb_mine_views_query_alter(&$view, &$query) {
  if ($view->name == 'statuser') {
    dsm($query, 'before');
    $query->where[0]['type'] = 'OR';
    dsm($query, 'after');
  }
}

You need to implement hook_views_api in your module file, then it'll find query_alter in the inc-file. The naming convention is strong, it's documented here:
http://drupalcontrib.org/api/drupal/contributions!views!views.api.php/fu...
and here:
http://drupalcontrib.org/api/drupal/contributions!views!views.api.php/fu...

The dsms are from the devel module and just to see what happens in the query.

mbouchard58’s picture

drupalvino,

I'm not clear what you're asking. Are you trying to "OR" a contextual filter with static filters which is what this thread is about? What is the application of your view? Do you need a block display, page display (with a path), a content pane or something else? What makes you think you need a contextual filter or even a view? For example if node/1 is a book with the taxonomy term "Technical books," drupal will show the taxonomy term as a link when you view the book. If you click on the link, you'll get a list of all other books which are categorized with "Technical books. Is that what you want? I could be way off, sorry, but, as I say, I'm not sure what is being asked or if it has anything to do with the main topic of this discussion thread.

Mb

drupalvino’s picture

@Mb

Thanks for your reply. In my case node/1 has term 1, node/2 has term 2 and node/3 also has term 1. If the path is node/1, I need to display the node/1's title and node/3's title. If the path is node/3 means, I need to display the node/1's title and node/3's title. So I need to filter both node id and term id. But I dont know how to filter that.

Do you have any idea?

mbouchard58’s picture

If I were doing it, I would create a view content pane with two contextual filters, one is the node id, the other is the term id. A view content pane gives you lots of choices on grabbing fields from context. View content panes are part of CTools. You'll want to use page manager (also part of CTools) and Panels. See the excellent videos on page manager with panels (which also covers View Content Panes) at NodeOne (see http://dev.nodeone.se/en/learn-page-manager). Johan Falk rocks!

I know this works but there are likely other ways to accomplish this (I can think of a way to do it using hook_views_query_alter but I don't know if you want to write any glue code).

If you need more help than this, you should start a new thread since your request doesn't have anything to do with the main topic. People who are following this thread probably don't want to read about my solutions to this problem :-)

You can also try IRC chat, drupal.stackoverflow.com, or the Views group (I think there is a VIews group).

HTH

Mb

krishnasdb’s picture

I have tried it, but no success. any help please.

Thanks in advance.

krishnasdb’s picture

I have tried it, but no success. Any help please.

Thanks in advance.

kingswoodute’s picture

Subscribe - this would be great functionality. If anyone comes up with a good way (or sees a patch get committed) please post.

kingswoodute’s picture

Hi odegard, thanks so much for posting this #14 I just implemented your solution and it works like a dream! I really appreciate it.

And thanks rvarkonyi for supplying the hook & idea.

mvdve’s picture

Version: 7.x-3.2 » 7.x-3.7

I also need to set the contextual filters to OR instead of AND.
All works fine until I create a view based on indexed nodes (for the faced search blocks).
The hook_views_query_alter function does not recognize the view, based on indexed nodes.

Does anybody has an suggestion why the alter function is not working on indexed nodes?

coredumperror’s picture

I'm very thankful for this issue thread, because without it I would have had no idea at all how to set up a view that shows all the events which belong to any of several different groupings on my site (the user specifies which groupings to filter on via contextual filter).

The solution in #14 did the trick, though I had to make one significant tweak. Apparently, the "Content: Published (Yes)" filter that comes with every View is considered to be a part of the same filter group as the contextual filters (group 0). So in order to make my view display only published nodes which belong to any of the groups specified by contextual filters (published AND (arg1 OR arg2 OR ...)), I had to move the "Content: Published (Yes)" into a new filter group (using the "And/Or, Rearrange" dialog). Note that the UI didn't display the "Content: Published (Yes)" filter any differently after I moved it into a new group (there weren't any other groups), but in the query object that gets passed into hook_views_query_alter() it did move it from group 0 into group 1, causing the code in #14 to no longer affect it.

I would very much appreciate it if Views would make OR-grouped Contextual Filters possible though the UI. It seems like it should be possible to essentially copy the "And/Or, Rearrange" dialog from Filters.

John Pitcairn’s picture

See #357082: Pull filter value from an argument?, which would mean contextual filters are no longer required.

liquidcms’s picture

re #24:

I would very much appreciate it if Views would make OR-grouped Contextual Filters possible though the UI. It seems like it should be possible to essentially copy the "And/Or, Rearrange" dialog from Filters.

yes, i think this would be very useful (and i think this is also the very short, simple explanation of what this issue is)

liquidcms’s picture

Title: Views AND/OR Contextual filters two ways » Support OR groups with contextual filters

sorry.. had to change the title to something that made sense.. :)

tobyup’s picture

#14 worked for me with #24 taken into account. Thank you all, saved me a lot of time.

BeatnikDude’s picture

#14 worked for me noting #24 as well

Alcaparra’s picture

+1

vistree’s picture

Can someone explain step by step how to set the view up? And, is there something in #14 which depends on the contextual field?

$query->where[0]['type'] = 'OR';

Is " type" the type of combination, or comes the type from "node->type"??

I tried the following:
New view.

+ Filter: published = TRUE
+ Filter: content type = article

+contextual filter 1: author of article = UID of logged in user
+contextual filter 2: referencend user (userreference) in article (field_userlink) = UID of logged in user

If combine the 2 contextual filters I get only articles, where author AND referenced user are both the same.
If I use only one of the contextual filters, the result is as expected.

Can anyone help me?

crabpatty’s picture

Is this actually going to be fixed or is everyone just accepting it as broken and moving on?

Todd Young’s picture

Agreeing with most, seems like a gap in capability for Views, would be very helpful.

In the meantime, for select use cases I am having luck using a "Global: PHP" filter type in the normal filter section.

Anybody’s picture

#25 is a good and logical step I think. Anyway this is an important functionality and I hope a solution will be found :(

liquidcms’s picture

Component: user data » Code
Category: Support request » Feature request
Priority: Normal » Major

really frustrating Views still can't do this and now it seems with latest version that doing a query alter to modify the contextual filter operator also no longer works (i know it used to).

first, i tried solution John mentions here: #357082: Pull filter value from an argument?. had high hopes and seems like the start of a good solution; but it only works with filters which take a text input; so reference filters like terms or entity refs that have a list of checkboxes as filter options; do not work (maybe this is fixable; will take a look).

i also tried the query alter approach that i know i have used many times before:

- did what was mentioned in #24 to give me 2 where groups and then simply changed the type for the where group that holds my contextual filters to OR; but sadly this does not work either.

Chris Burge’s picture

Take a look at Views Contextual Filters OR:

The module provides a views plugin which modify query to support OR conditions for contextual filters.

bmango’s picture

I had a similar problem of grouping static and multiple contextual filters. I managed to solve it using a combination of the Views Contextual Filters OR module and the Views PHP filter module. I put a more detailed write up at the [SOLVED] Views - Filtering nodes by current user profile field thread.

It would be great if views could include contextual filters in the grouping functionality that is already there for static filters.

chaps2’s picture

Same problem in D8. Fortunately, the hook_views_alter_query() fix mentioned in #10 and #14 still works - though looks slightly different:

function hook_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == '<your_view_id>') {
    $query->setWhereGroup('OR', 0);
  }
}
Anybody’s picture

Well this should be a task in D8, if I may wish and hopefully be backported later on.

ARN__’s picture

With this code you can add a new OR-group. $query->add_table() is required if the table was not yet used in the query.

function hook_views_query_alter(&$view, &$query) {
	
	if ($view->name == 'my_view') {
		
			
		$conditions[0]['field'] = 'field_data_field_my_table.field_users_target_id = :val';
		$conditions[0]['value'] = Array(':val' => $view->args[1]); 
		$conditions[0]['operator'] = 'formula';
			
			
		$conditions[1]['field'] = 'node.uid';
		$conditions[1]['value'] = $view->args[2];
		$conditions[1]['operator'] = '=';
						
		$query->add_table('field_data_field_my_table');
		$query->where[] = Array("conditions" => $conditions, "args" => Array(), "type" => "OR");
						
	}
}
anthonys’s picture

Similar to the last post, but using the query constructors:

This goes in HOOK_views_query_alter

  if ($view->name == 'my_view') {
    // Get the first view contextual filter
    $filterval = $view->args[0];

    // Add a where with two options
    // 100 is an arbitrary id number for the OR group, which should not interfere
    // with any existing ID numbers in the view filters (views starts from 0).

    // This creates a where group
    $query->set_where_group($type = 'OR', 100, $where = 'where');

    // This adds conditions. Here we allow either the contextual filter value or null
    // Having a look at the $query object should help you find table alias/field name
    $query->add_where(100, 'tablealias.fieldname', $filterval);
    $query->add_where(100, 'tablealias.fieldname', NULL);
  }

You do also need to add HOOK_views_api() as in #14

MustangGB’s picture

Status: Active » Closed (won't fix)

Can close this now as there are other modules that provide this functionality.

torotil’s picture

You are referring to https://www.drupal.org/project/views_contextual_filters_or, right? Are there other modules that provide this functionality? Why does it make this a won’t fix?

MustangGB’s picture

As mentioned in #39 to keep feature parity it should go into D8 first, also in 7 years no-one has created a patch, and several modules/workaround have been mentioned that accomplish the goal, I guess you could close it as "works as designed" if you wish.

torotil’s picture

I could close as either if I were a views maintainer and decided that this feature is not desirable. Neither of us is a views maintainer.

Inactivity alone isn’t enough. Many major Drupal core issues would qualify for being closed as “works as designed” if we put them to the same test.