0

I am using Drupal 7. and i am trying to achieve a user have multiple roles and if can able to login with particular role. I saved role id in session. and now i want to use that user only for that role. now i have different block. i want to show block on certain pages with session role id.

Does anybody have idea how to do this ?

Thanks

  • Why are you saving the role in the session? User roles in Drupal are accessible via the user object and don't need to be stored anywhere else. You can make a block accessible to certain roles only using the core block administration interface. – Alfred Armstrong Mar 1 '18 at 14:12
0

Option 1
Enable the core PHP Filter module. This will give you a third option in the Block Visibility settings: "Display is PHP Code returns true." The you could use something link the following in the text area that it provides:

<?php
if (isset($_SESSION['myval'])) {
  return TRUE;
}
return FALSE;
?>

The problem with this method is it:

  1. stores code in the DB (no source control)
  2. relies on eval()
  3. opens up the possibility that an unintended user could execute PHP code on your server

Option 2
The Extended Block Visibility module's purpose is eliminate the three drawbacks in Option 1. From the module's README:

FEATURES

  • Allows PHP block visibility to be defined in code.
  • Allows theme specific block visibility code.
  • Removes reliance on core php module and eval() for defining block visibility.

I haven't used this module yet, but essentially you would put the same code above in a custom function in a custom module after enabling the Extended Block Visibility module. E.g.:

function _search_form_block_visibility($block) {
  if (isset($_SESSION['myval'])) {
    return TRUE;
  }
  return FALSE;
}

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.