3

I have added a block to a region and set it so that it shows on user profile pages.

However, I want the block to be displayed only on the profile pages, not on the profile edit pages.

Is that possible?

  • You can use PHP in block visibility settings page – niksmac Apr 21 '12 at 5:42
  • @NikhilMohan Is that enough for my need ? Can you show some example code ? please – user6930 Apr 21 '12 at 5:43
  • 1
    What is that block? your written module or an exists block? – Mohammad Ali Akbari Apr 21 '12 at 5:49
  • @MohammadAliAkbari i have written module. – user6930 Apr 21 '12 at 6:01
  • so you can return empty $block array in /user/%uid/edit, something like if(arg(0) == 'user' && is_number(arg(1)) && empty(arg(2))) ... – Mohammad Ali Akbari Apr 21 '12 at 6:09
3

First, enable the PHP filter module and give yourself permissions to use PHP in order to input PHP on the Block visibility settings page.

On the Block visibility settings page, you can check for an argument like this:

<?php 
if(arg(0) == 'user' && arg(2) != 'edit' )
return true;
else 
return false 
?> 

Also, see my answer on Stack Overflow.

0

These days there is another alternative available ... which doesn't require the PHP filter to be enabled (which you should try to avoid whenever possible) ... Just use the Rules block visibility module. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless.

Need to show a block only for users registered more than a month ago?

Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules "check the path of the current page" (so that the block is only shown on your pages with a path that starts with "user/", but not on those pages that have a path ending with "/edit").

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?". It includes a Rules Component (in Rules export format), which you should replace by this variation of it to make it match your specific question):

{ "rules_block_visibility_show_on_selected_profile_pages" : {
    "LABEL" : "Show block on selected profile pages",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "module" : { "label" : "Module", "type" : "text" },
      "delta" : { "label" : "Delta", "type" : "text" },
      "result" : { "label" : "Result", "type" : "boolean", "parameter" : false }
    },
    "IF" : [
      { "AND" : [
          { "text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "user\/",
              "operation" : "starts"
            }
          },
          { "NOT text_matches" : {
              "text" : [ "site:current-page:path" ],
              "match" : "\/edit",
              "operation" : "ends"
            }
          }
        ]
      }
    ],
    "DO" : [ { "data_set" : { "data" : [ "result" ], "value" : "1" } } ],
    "PROVIDES VARIABLES" : [ "result" ]
  }
}

Also, if you'd replace the 2nd Rules Condition in the above Rules Component with something using an appropriate regular expression, you could even further refine it, to check for something like "user/uid/edit", whereas uid corresponds with some user's uid, and so that the block would be shown anyway on pages with a path like "user/with/some/other/path/edit".

0

On Drupal 8, this is possible to be done with the Block Exclude Pages module. You would set it like this:

/user
/user/*
!/user/*/*

This would cause the block to be displayed on the regular user profile page (www.example.com/user, or www.example.com/user/5), but not on www.example.com/user/5/edit.

This module establishes the (!) sign as a mark for denying access on a given rule.

protected by kiamlaluno Jan 20 '16 at 9:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?