I want to set a block to visible only for a certain path, with the logged in user id, like my-path/[user:uid]. I feel like I should be able to just do that.

How can I do this?

  • Or, if anyone can show me how to do this in preprocess or a hook, that would be great too. – vintorg Jul 23 '13 at 0:26
  • First thing, you did not mention how you created the block or want to create the block. Second, by enabling php filter, you can set the path explicitly! Make sure the path 'my-path/[User:id]' exist on your site! – Bhavin Joshi Jul 29 '13 at 5:48
  • The block is a Superfish menu block, so I can't enable a code filter. Also, the my-path/[User:id] does exist, but when the user is logged in, the path becomes my-path, so I need to cruch some logic to figure out if I can display my block. – vintorg Jul 29 '13 at 20:04
  • detail blog: goo.gl/77zEds – Suresh Kamrushi Jul 14 '15 at 11:36

You can do that with using hook_block_info_alter().

For Example:

function YOURMODULE_block_info_alter(&$blocks, $theme, $code_blocks) {
  global $user;
  $blocks['THE_MODULE']['THE_BLOCK']['pages'] = my-path/$user->uid;

Where THE_MODULE is the module defining the block you want to alter, and THE_BLOCK is the 'delta' of this block.

| improve this answer | |
  1. Add contextual filter: Global: Null

    provide default value: raw value from URL

    Path component: 1

  2. Add contextual filter: User: Uid

    When the filter value IS available or a default is provided
    Specify validation criteria -> Validator -> user

Path validation (contextual filter: Global: Null)

You can validate the first path component in two ways:


use contextual filter settings:

When the filter value IS available or a default is provided:
Specify validation criteria -> Validator -> PHP Code

return ($argument == 'my-path') ? TRUE : FALSE;


Alternatively, if you don't want to use PHP code validation in the contextual filter settings, you can use preprocess function in your template.php file. Not sure whether it's the right way or not, but works:

 * Preprocess the primary theme implementation for a view.
function YOURTHEME_preprocess_views_view(&$vars) {
  $view = $vars['view'];
  switch ($view->name) {
    case 'YOUR-VIEW-NAME':
      if ($view->args[0] != 'my-path') $view->result = array();

Assuming 'my-path' it's the View or some other module custom path, in this case it has to be validated with the php. Of course if it would be an alias of some content type, then php validation wouldn't be needed.

| improve this answer | |
  • Thanks, but it's not a view. – vintorg Jul 29 '13 at 20:05

1) Enable the core PHP filter module

2) Use php in the visibility setting of your block. Notice that the path 'mypath' must be registered somehow before using this code. I mean that if the path 'mypath' does not exist at all any code here will not work.

global $user;
$uid = $user->uid; // Get the logged in user id
// If the path is 'mypath/myarg' then arg(0) is 'mypath' and arg(1) is 'myarg'.
$show = (arg(0) == 'mypath') && (arg(1) == $uid); // It must return both TRUE

return $show;

Alternative, use the Context module.

| improve this answer | |
  • I'm trying not to use the PHP filter. Any ideas through a module or preprocess? – vintorg Jul 24 '13 at 1:26
  • Why not use php? Template preprocess are using php also. If this block is made with views module you should use a user id argument for the block and then just use a path permission of 'mypath/*'. – TheodorosPloumis Jul 24 '13 at 14:08
  • PHP isn't the problem. I shouldn't have to grab args off the URL for something that should be simple. I feel like there is a more appropriate way to do this. – vintorg Jul 24 '13 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.