requesting a condition that would be true if a user did NOT have a certain role

for example: show the membership ad block if not already a member

CommentFileSizeAuthor
#70 996606-70.patch3.45 KBpaulocs
#32 context-n996606-32.patch1.78 KBDamienMcKenna
#24 996606.24-context-negate-user_-roles.patch1.84 KBressa
#27 context-negate_user_roles-996606-27.patch1.84 KBressa
#20 996606.20-context-negate-user_-roles.patch1.79 KBmrfelton
#19 context-Negate_user_roles-996606-19.patch1.77 KBmrfelton
#18 context-Negate_user_roles-996606-18.patch3.62 KBChappo
#15 negative-user-condition-996606-15.patch1.51 KBwbobeirne
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

YesCT’s picture

#580324-22: Option to negate conditions postponed that issue, but for paths. this request if for roles.

ressa’s picture

This would be a great addition to the module, to be able to hide blocks for for example the administrator. For now I will just hack some php together for the olde blocks system:

global $user;
if ($user->uid != 1) {
    return "This block is hidden for the user with the user-ID 1.";
} else {
    return;
}

...and to change the order of the blocks, which is "0" by default, by updating the weight of the block in the database, to for example "3", and then drag and drop the order with Context UI + Admin

YesCT’s picture

you mean ==
not !=
right?

ressa’s picture

I actually did mean !=

But I lifted the code from somewhere else, so the "help" text was false 8o)

This makes more sense:

global $user;
if ($user->uid == 1) {
  // This block is hidden for the user with the user-ID 1.
  return FALSE;
} else {
  return TRUE;
}
dwkitchen’s picture

This is exactly what I want to do as well, show a membership ad to the users who do not have the subscriber role

YesCT’s picture

Needed this (again) and found my old feature request. :)
Has anyone gotten anywhere with this, or knows someone who might want to write a patch for context to add this in?

Now I want to be able to test:

user does not have role "has applied"
and user has role "is a member"

So I can show a "apply now" block to people who are members but have not applied yet for an internal position only available to members.

upasaka’s picture

+1. Would love to see this feature in context. Need to show a block to everyone who is NOT of "subscriber" role.

El Bandito’s picture

Yes. A generalised "NOT" applied to all conditions would be super useful.

+1

El B

El Bandito’s picture

Ressa

Ref. Your code. Sorry. I've not quite got this. Is this code added to the Page Specific Visibility Section using the standard /admin/build/block interface, even though the block is not assigned to a region via the Blocks system ?

Cheers

El B

ressa’s picture

ElB: I did move the block to the desired region via the standard /admin/build/block interface, updated the weight of the block in the database, and dragged and dropped the order with Context UI + Admin. Probably as the last thing I added the php code, to hide it from admin

I just can't seem to reproduce it now... Perhaps a better solution would have been to auto assign a specific role to new users ('new user'), and use the Context interface to only show the block to the roles 'anonymous' and 'new user'?

YesCT’s picture

trying to deal with this situation: keeping track of subscriptions and expired subscriptions.

when a user buys a subscription:
they get the role (ubercart role feature, or just admin editing account):
subscriber ever
current subscriber

triggered rule on account change checks if
presaved user had current subscriber role and post save they did not (to see if role is being removed), then rule adds role: expired subscriber

triggered rule on account change checks if
user has current subscriber role and expired subscriber
and then: removes the expired subscriber role

still thinking this through, seems to work ok, but the removal of the expired role is being missed by role watch dog module.

seems like it would be so much less error prone to have a context that is: NOT role: current subscriber

Mark Nielsen’s picture

You can already achieve a negated context. It's a little counter-intuitive, but it's totally possible, as originally pointed out here: http://drupal.org/node/580324#comment-4401174

You create a context based on a positive selection. Let's say a context called "is_member" which uses a Role Condition to see if a user is in the "Community Member" role). Then you create a second context that has a Context Condition which is true if the first context is negative - in this case, "~is_member".

Job done :)

Mark Nielsen’s picture

Component: Code » User interface
Priority: Normal » Minor

I thought I'd be bold and change the component to UI (implementing this feature would just be an improvement to the interface/workflow for something that's already possible), and change the priority to minor (if it's already possible, then this can't be a high priority).

Apologies if that was too bold -- just trying to help reduce pressure on the issue queues :)

rodmarasi’s picture

subscribe

wbobeirne’s picture

Attaching a patch that adds a checkbox to the User Role condition which allows you to make the user condition a negative one. I did some testing and it seemed to work fine, but I didn't have any complicated cases to test it with. Hope it works for y'all.

dwkitchen’s picture

Looks good @wbobeirne will try it out and report back

kbasarab’s picture

I actually went about this how @Mark Nielson mentioned in http://drupal.org/node/996606#comment-5299900 before finding this thread. I did go ahead and test the patch though on D6 and it worked as expected. For sites with lots of contexts keeping it in one option like this makes things a little bit easier to deal with from a UX perspective.

Chappo’s picture

Just in case anyone needs the same functionality that I did, I've updated the functionality so you can negate multiple roles rather then just one. Its on the 7.x version but the code is the same. Thanks to wbobeirne for the original patch!

mrfelton’s picture

Patch in #18 updated to apply cleanly against latest code. Also bumping version to 7.x, since thats where new dev work should go.

mrfelton’s picture

Liliplanet’s picture

The patch is so perfect, thank you Chappo and mrfelton!

Now can easily negate a role for example user is 'authenticated' but not 'premium'.

nigelw’s picture

Status: Needs review » Needs work

Patch works(ish). Doesn't apply cleanly to the latest Dec 7 dev or stable. Probably needs to be rerolled using the latest version. Otherwise the actual functionality works great.

ressa’s picture

I just applied the patch in #20 manually to the latest official release (7.x-3.0-beta6), and it works great, thanks!

ressa’s picture

Here is an updated patch, and instructions if others find patching daunting.

I used to think I couldn't possibly patch a Drupal module, but actually three commands from the command line is all it takes. You need to have Git installed on your computer:

The first command you find by clicking 'Version control', right under the title on the Context module main page. Select '7.x-3.x', click 'Show' and copy the lines below and run them, one after the other. Do the updates in the module code and execute the third command, which will create the patch. In this example 996606 is the issue number and 24 is the issue page post number, which will be your post, including the patch attached. That's it!

EDIT: The format of the attached patch has got the wrong syntax, but I can't update it. The example above should be correct though. Sorry about the confusion.

YesCT’s picture

Status: Needs work » Needs review

The bot did not send I to test... I'm not used to working on contrib modules. Some have tests. Does this one?

You could try naming the patch like
project-99999-description_with_underscores-99.patch
So context-996606-negate_user_roles-24.patch

YesCT’s picture

Well the doc recommends [project_name]-[short_description]-[issue-number]-[comment-number].patch
But I've seen people have other variations, so the testbot is not uber picky.

That was from http://drupal.org/node/707484

One of my other favorites is http://xjm.drupalgardens.com/blog/interdiffs-how-make-them-and-why-they-...
Which talks about interdiffs. That will be good when someone posts the next try at this patch, they will include a diff to the previous patch. That helps make it easier to get reviews.

The other thing I'm thinking of is there are certain attachments that the testbot ignores.
http://drupal.org/node/332678

ressa’s picture

You're right, I just assumed the name syntax in the patch by mrfelton was correct, thanks for the heads up. I am resubmitting the renamed patch, let's see it if it's picked up this time. I will update my erroneous tips above when (if) it works :-)

YesCT’s picture

Ah, ha. Sorry for the traffic about stuff so general...
We can use the automated testing tab on the project page.. http://drupal.org/node/64885/testing-status
And see context isn't using automated testing right now. (To compare: http://drupal.org/project/media )
So that means ... :) that the file format was ok afaik and we should manually test it.

In case someone wants to test it and is a bit unsure:
Contributor tasks doc: http://drupal.org/node/1489010
might help.

ressa’s picture

That makes sense, the patch in post #19 doesn't seem to have been tested by the automated test bot, even though the name of the patch looks correct.
I get an "Access Denied" when I try to access that automated testing page (also, I can't see the tab), but I believe you :-)

YesCT’s picture

hmm. I wonder if it was just because I was logged in, or because I'm logged in and a maintainer (of another project).

Which leads me to wonder how other folk can tell. .. I guess they submit a patch, mark the issue needs review and see if it went to the testbot *ha* ... since that is what happened here to us! :)

ressa’s picture

Yeah, you probably have an elevated overall access level due to that. And it does explain why patches sometimes are test-botted, seemingly erratically, and others aren't :-)

It is kinda odd though, that auto-testing hasn't been activated for all contributed modules automatically, but there is probably a good explanation for that.

DamienMcKenna’s picture

The patch above works but needed a few small fixes:

  • The "if ($options['negate_role'] == 0) {" line gave an error for any context that had a role definition prior to the patch being applied.
  • Per the Drupal coding standards, booleans should be spelled in uppercase.

This patch fixes both issues.

petermallett’s picture

I took a slightly different approach to this for one of my sites. I needed a context that had both a Role condition and a Not Role condition. So I made a new condition for the not-role part: https://drupal.org/sandbox/petermallett/1962466

brunomolica’s picture

Issue summary: View changes

#32 working

capfive’s picture

I just used the option stated in https://www.drupal.org/node/580324#comment-4401174 and whilst it is not really standard it did work.

my use case was to exclude 1 taxonomy term, whilst still allowing the user to add more taxonomy terms without having to go add them to the context each time.

would be good if a negate button was able to be integrated :)

markdc’s picture

Thank you for this! #32 works like a charm.

mr.york’s picture

Thank you for this! #32 works for me.

Anybody’s picture

Status: Needs review » Reviewed & tested by the community

#32 works great! RTBC!
Is there an active maintainer who includes this in the next dev release?

The last submitted patch, 15: negative-user-condition-996606-15.patch, failed testing.

The last submitted patch, 15: negative-user-condition-996606-15.patch, failed testing.

The last submitted patch, 15: negative-user-condition-996606-15.patch, failed testing.

The last submitted patch, 15: negative-user-condition-996606-15.patch, failed testing.

The last submitted patch, 18: context-Negate_user_roles-996606-18.patch, failed testing.

The last submitted patch, 18: context-Negate_user_roles-996606-18.patch, failed testing.

The last submitted patch, 18: context-Negate_user_roles-996606-18.patch, failed testing.

The last submitted patch, 18: context-Negate_user_roles-996606-18.patch, failed testing.

The last submitted patch, 19: context-Negate_user_roles-996606-19.patch, failed testing.

The last submitted patch, 19: context-Negate_user_roles-996606-19.patch, failed testing.

The last submitted patch, 19: context-Negate_user_roles-996606-19.patch, failed testing.

The last submitted patch, 19: context-Negate_user_roles-996606-19.patch, failed testing.

The last submitted patch, 20: 996606.20-context-negate-user_-roles.patch, failed testing.

The last submitted patch, 20: 996606.20-context-negate-user_-roles.patch, failed testing.

The last submitted patch, 20: 996606.20-context-negate-user_-roles.patch, failed testing.

The last submitted patch, 20: 996606.20-context-negate-user_-roles.patch, failed testing.

The last submitted patch, 27: context-negate_user_roles-996606-27.patch, failed testing.

The last submitted patch, 27: context-negate_user_roles-996606-27.patch, failed testing.

The last submitted patch, 24: 996606.24-context-negate-user_-roles.patch, failed testing.

The last submitted patch, 27: context-negate_user_roles-996606-27.patch, failed testing.

The last submitted patch, 27: context-negate_user_roles-996606-27.patch, failed testing.

The last submitted patch, 24: 996606.24-context-negate-user_-roles.patch, failed testing.

The last submitted patch, 24: 996606.24-context-negate-user_-roles.patch, failed testing.

The last submitted patch, 24: 996606.24-context-negate-user_-roles.patch, failed testing.

hass’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Need tests

Can we have tests for this, please.

hass’s picture

criscom’s picture

Patch in #32 works for me. Thanks! Please commit to dev version!

delacosta456’s picture

hi

on drupal 7.53 #32 works for me too.. Just for infos (not in all cases) : it may be necessary to flush cache (drush cc all) to make appear the checkbox

thanks for the nice work

cobenash’s picture

#32 works on 7.64.

cobenash’s picture

Status: Needs work » Needs review

Please commit to dev version.

DamienMcKenna’s picture

Status: Needs review » Needs work

In #63 a maintainer requested that test coverage be written, so putting this back to "needs work".

paulocs’s picture

Status: Needs work » Needs review
FileSize
3.45 KB

Adding tests

  • paulocs committed a2d0595 on 7.x-3.x
    Issue #996606 by ressa, mrfelton, DamienMcKenna, paulocs, Chappo,...
paulocs’s picture

Status: Needs review » Fixed
paulocs’s picture

Status: Fixed » Closed (fixed)
DamienMcKenna’s picture