How to display msg to the unauthorized that he have no access to this content .. or please login to read .... ?!

Comments

marvix’s picture

any one for this ?!

marvix’s picture

for anyone may need this ...

if(!field_access('view', field_info_field('FIELD_NAME'),'node')){
		drupal_set_message(" MMMM", 'warning',false);
}
mxwitkowski’s picture

This works quite well. Thanks.

Another idea: I have a use case where I want to show the field name but not the value to users without view permissions. The use case is for a site where premium content can be purchased. I want to set the view permission to block certain users, but let them know that a restricted field exists, then instead of showing the value display a message to the user providing additional information. Ideally, this message to the user would be managed via the admin UI.

Can this be done?

Zarevac’s picture

Marvix,

Where do you add this exactly?

jonathan_hunt’s picture

It would be good to be able to add a custom "access denied" message in the field settings added by field_permissions. In the meantime, you can implement hook_node_view_alter() with per-field logic similar to:

  if (!$build['field_video']['#access']) {
    $build['field_video']['#access'] = TRUE; // Grant access
    $build['field_video']['#formatter'] = 'text_default'; // Change from media to text output.
    $destination = array('destination' => 'node/' . $build['#node']->nid);
    foreach ($build['field_video']['#items'] as $key => $item) {
      $build['field_video'][$key] = array(
        '#markup' => t('<a href="@login">Log in</a> or <a href="@register">register</a> to view video.', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination)))),
      );
    }
  }
Chimos’s picture

if(!field_access('view', field_info_field('FIELD_NAME'),'node')){
		drupal_set_message(" MMMM", 'warning',false);
}

Sorry,
where and how should we use this code?
Thanks !

marvix’s picture

You can added in tempate.php under function THEME_preprocess_node , or you can just added some where in the tempate.php...

Chimos’s picture

Thank you!

liliplanet’s picture

Hi Jonathan, would you be so kind to guide me on where to add your hook_node_view_alter() at http://drupal.org/comment/reply/1429200/5835296 ?

Would most appreciate your help, thx

jonathan_hunt’s picture

The code I suggested for hook_view_alter() would go in a custom module. You need to adapt the function name hook_view_alter() to suit the name of your custom module. See https://drupal.org/node/361112 and the nodeapi_example module from https://drupal.org/project/examples

liliplanet’s picture

thank you Jonathan, that is above my head .. I had a look and have to create a new module .. perhaps you have created an example please?

jonathan_hunt’s picture

I don't have ready code to show because I don't recall which site I used this on, and in any case the code would be incorporated with other customisations for that site. The examples I cited should be sufficient if you are a module developer (or want to become one). Otherwise you may need to hire one.

Paulset’s picture

Issue summary: View changes

Hi,

I try the code of @jonathan_hunt (#5) for add a custom "access denied" message (in my template.php), but i have many errors...

Would there be another solution?

bluegeek9’s picture

Status: Active » Closed (outdated)

Field Permissions does not support Drupal 7.