Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#70 | 996606-70.patch | 3.45 KB | paulocs |
| |||
#32 | context-n996606-32.patch | 1.78 KB | DamienMcKenna |
Comments
Comment #1
YesCT CreditAttribution: YesCT commented#580324-22: Option to negate conditions postponed that issue, but for paths. this request if for roles.
Comment #2
ressa CreditAttribution: ressa commentedThis 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:
...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
Comment #3
YesCT CreditAttribution: YesCT commentedyou mean ==
not !=
right?
Comment #4
ressa CreditAttribution: ressa commentedI actually did mean !=
But I lifted the code from somewhere else, so the "help" text was false 8o)
This makes more sense:
Comment #5
dwkitchen CreditAttribution: dwkitchen commentedThis is exactly what I want to do as well, show a membership ad to the users who do not have the subscriber role
Comment #6
YesCT CreditAttribution: YesCT commentedNeeded 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.
Comment #7
upasaka CreditAttribution: upasaka commented+1. Would love to see this feature in context. Need to show a block to everyone who is NOT of "subscriber" role.
Comment #8
El Bandito CreditAttribution: El Bandito commentedYes. A generalised "NOT" applied to all conditions would be super useful.
+1
El B
Comment #9
El Bandito CreditAttribution: El Bandito commentedRessa
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
Comment #10
ressa CreditAttribution: ressa commentedElB: 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'?
Comment #11
YesCT CreditAttribution: YesCT commentedtrying 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
Comment #12
Mark Nielsen CreditAttribution: Mark Nielsen commentedYou 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 :)
Comment #13
Mark Nielsen CreditAttribution: Mark Nielsen commentedI 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 :)
Comment #14
rodmarasi CreditAttribution: rodmarasi commentedsubscribe
Comment #15
wbobeirne CreditAttribution: wbobeirne commentedAttaching 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.
Comment #16
dwkitchen CreditAttribution: dwkitchen commentedLooks good @wbobeirne will try it out and report back
Comment #17
kbasarab CreditAttribution: kbasarab commentedI 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.
Comment #18
Chappo CreditAttribution: Chappo commentedJust 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!
Comment #19
mrfelton CreditAttribution: mrfelton commentedPatch in #18 updated to apply cleanly against latest code. Also bumping version to 7.x, since thats where new dev work should go.
Comment #20
mrfelton CreditAttribution: mrfelton commentedLast patch had a mistake (duplicate execute() function).
Comment #21
Liliplanet CreditAttribution: Liliplanet commentedThe patch is so perfect, thank you Chappo and mrfelton!
Now can easily negate a role for example user is 'authenticated' but not 'premium'.
Comment #22
nigelw CreditAttribution: nigelw commentedPatch 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.
Comment #23
ressa CreditAttribution: ressa commentedI just applied the patch in #20 manually to the latest official release (7.x-3.0-beta6), and it works great, thanks!
Comment #24
ressa CreditAttribution: ressa commentedHere 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.
Comment #25
YesCT CreditAttribution: YesCT commentedThe 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
Comment #26
YesCT CreditAttribution: YesCT commentedWell 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
Comment #27
ressa CreditAttribution: ressa commentedYou'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 :-)
Comment #28
YesCT CreditAttribution: YesCT commentedAh, 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.
Comment #29
ressa CreditAttribution: ressa commentedThat 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 :-)
Comment #30
YesCT CreditAttribution: YesCT commentedhmm. 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! :)
Comment #31
ressa CreditAttribution: ressa commentedYeah, 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.
Comment #32
DamienMcKennaThe patch above works but needed a few small fixes:
This patch fixes both issues.
Comment #33
petermallett CreditAttribution: petermallett commentedI 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
Comment #34
brunomolica CreditAttribution: brunomolica commented#32 working
Comment #35
capfive CreditAttribution: capfive commentedI 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 :)
Comment #36
markdcThank you for this! #32 works like a charm.
Comment #37
mr.york CreditAttribution: mr.york at Agence Inovae commentedThank you for this! #32 works for me.
Comment #38
Anybody#32 works great! RTBC!
Is there an active maintainer who includes this in the next dev release?
Comment #63
hass CreditAttribution: hass commentedCan we have tests for this, please.
Comment #64
hass CreditAttribution: hass commentedComment #65
criscomPatch in #32 works for me. Thanks! Please commit to dev version!
Comment #66
delacosta456 CreditAttribution: delacosta456 commentedhi
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
Comment #67
cobenash#32 works on 7.64.
Comment #68
cobenashPlease commit to dev version.
Comment #69
DamienMcKennaIn #63 a maintainer requested that test coverage be written, so putting this back to "needs work".
Comment #70
paulocsAdding tests
Comment #72
paulocsComment #73
paulocsComment #74
DamienMcKenna