Add support for negating user role condition for block visibility

Created on 19 July 2018, over 6 years ago
Updated 4 June 2024, 6 months ago

Issue summary updated as of comment #32

Problem/Motivation

The block module does not make provision for negating the user role visibility condition. That makes it impossible (via the UI) to make a block visible only to authenticated users who do not also have a specified additional role, such as member.

Steps to reproduce

Go to /admin/structure/block/manage/BLOCKNAME and look at the "roles" tab; there is no "negate" option.

Proposed resolution

See patch: enable-block-visibility-user-role-negation-2986958-27.patch

Remaining tasks




Tested with themes

  • Bartik
  • Bootstrap
  • Radix
  • Umami

User interface changes

On /admin/structure/block/manage/BLOCKNAME ("roles" tab), add "negate" option.

API changes

None.

Data model changes

None.

Release notes snippet

Add a "negate" option to "roles" tab in block visibility settings.

โœจ Feature request
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Blockย  โ†’

Last updated 1 day ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States benjaminbradley Austin, TX

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Merge request !5649reroll patch #46 โ†’ (Open) created by ptmkenny
  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    I re-rolled patch #46 and opened an MR to allow this to apply to the latest version.

  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny
  • Pipeline finished with Canceled
    12 months ago
    Total: 475s
    #58234
  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    Here's a corrected reroll of #46.

  • Pipeline finished with Failed
    12 months ago
    Total: 583s
    #58236
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia gaurav.kapoor

    #53 would require some changes as it isn't removing these 2 lines and the radio buttons disappear as their type gets changed to 'value'.

    -      $form['user_role']['negate']['#type'] = 'value';
    -      $form['user_role']['negate']['#value'] = $form['user_role']['negate']['#default_value'];
    

    Also, the saved value of user role negation isn't reflected in the UI after saving the block. Should we make this change as well:

    -      $form['user_role']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value'];
    +     $form['user_role']['negate']['#default_value'] = (int) $form['user_role']['negate']['#default_value'];
    

    Thoughts?

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance liliplanet Cannes

    How I wish this negate role functionality could be committed, it is awesome! Been having to hack the block module since D8 ๐Ÿ˜‰

  • ๐Ÿ‡ธ๐Ÿ‡ชSweden edward.peters

    Me too, this would be such a great development to have this in core.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance liliplanet Cannes

    Thank you! But the patch in #53 does not save when you select 'hide for the selected roles'.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance liliplanet Cannes

    Oh dear, I have tried patch #43, #46, #54, still 'hide for selected roles' is not saving.

    Any progress most appreciated.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance liliplanet Cannes

    Thank you #@mohit! #46 is definitely the one.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shabana.navas

    Updating patch in #46 to fix default value problem mentioned in #54.

  • Pipeline finished with Failed
    7 months ago
    Total: 172s
    #145539
  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    @gaurav.kapoor I renamed request_path to user_role; that was a mistake on my part. Thank you for pointing it out.

    As for the first two lines, in Drupal 11 it seems those lines are already gone.

  • Pipeline finished with Failed
    7 months ago
    Total: 1495s
    #164026
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom iancawthorne

    Is there a patch for 10.2.x which works on this thread?

    I've tried #53, which will apply, but as mentioned in #58, saving the block does not update the negate value. The patches at #43, #46 and #60 will not apply to 10.2.6.

  • ๐Ÿ‡ฑ๐Ÿ‡ฐSri Lanka SajithAthukorala

    Patch for Issue #2986958: Invert visibility of chosen roles for blocks

    Here is the patch to invert the visibility of chosen roles for blocks as per Issue #2986958.

    This patch introduces a custom block permissions handler that allows inverting the visibility settings for blocks based on roles.
    @iancawthorne #62
    Please review and test the changes.
    This patch tested with Drupal 10.2.x

    https://www.drupal.org/files/issues/2024-06-03/2986958-invert-block-visibility-by-role.patch โ†’

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom iancawthorne

    The patch at #63 works great on 10.2.x.

  • Pipeline finished with Failed
    6 months ago
    Total: 182s
    #189694
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prabha1997

    The patch #63 works on 10.2.x.

  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    Warning: Patch #63 is a different modification than the MR 2986958-add-support-for and the rest of the patches in this thread. Patch #63 inverts the visibility of the roles in all cases, whereas the rest of the patches and the MR 2986958-add-support-for add a setting that allows the roles to be negated. Patch #63 is not a solution to this issue and it doesn't have any tests.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom iancawthorne

    @ptmkenny Can you clarify on the [quote]inverts the visibility of the roles in all cases[/quote] part?

    From my testing, I update a blocks visibility and did a config export. Only the user role visibility changed in that config export, as expected. No config changed for negating visibility was negated for any of the other conditions, such as by content type, by page or by vocabulary etc.

    Are you talking about a different scenario which I'm missing?

  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    @iancawthorne I meant all cases for user roles, not for the other conditions. Anyway, you can clearly see this by reading the code for the patch; patch #63 is a one-liner whereas the other patches in this thread are many more lines.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    Prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    6 months ago
    Total: 641s
    #190421
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    I noticed a few things, and I would like to request your inputs on the following considerations:

    • By default none of the radio boxes should be selected, currently by default "Show for the selected roles"is selected and it gives the impression that we have to select some role before proceeding
    • There should be some description under roles checkboxes like "If nothing is selected the block will be visible to all the listed roles"
    • The "Roles" label should be just above the roles checkboxes and not above the radioboxes.

    Please see the attached screenshot.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom iancawthorne

    @ptmkenny I can see the patch at #63 is a lot less lines of code than others. However, up until needing to upgrade from Drupal 10.1.x to 10.2.x, I've been using the patch at #21. If you take out the test lines for that patch, you are left with a two liner, which as far as I can tell does the same as patch #63 but in a very slightly different way.

    I'm still not sure what you mean by all cases for user roles though?

  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    @Prashant.c First, please note that this change has already undergone a usability review โœจ Add support for negating user role condition for block visibility Needs work .

    As for your points:

    By default none of the radio boxes should be selected, currently by default "Show for the selected roles"is selected and it gives the impression that we have to select some role before proceeding

    In Drupal, radios generally have something selected by default. For example, in "Submission form settings", "Preview before submitting" also has a default value selected. If no default value was selected, when creating a new entity, users would always have to fill out that part of the form. Anyway, this is the way other

    There should be some description under roles checkboxes like "If nothing is selected the block will be visible to all the listed roles"
    The "Roles" label should be just above the roles checkboxes and not above the radioboxes.

    This could be a helpful suggestion but I think it is out of scope for this issue, as the change has already undergone usability review. Please consider opening a new issue with this request.

  • ๐Ÿ‡ฏ๐Ÿ‡ตJapan ptmkenny

    For anyone coming to this issue and "just looking for a patch," please consider using the User Not Role module โ†’ instead, which does what this issue will eventually accomplish in core.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prashant.c Dharamshala

    @ptmkenny

    Thanks for your responses; I understand your points. Apologies for missing #41 โœจ Add support for negating user role condition for block visibility Needs work

Production build 0.71.5 2024