1

With field permissions, I restrict the access of a field to certain users.

I would like to display some text to inform the user that he has no access to the field.

I tried it in node.tpl.php :

<?php if (!field_access('view', field_info_field('field_name'),'node', $node)): ?>
    <p><?php print t('my text.'); ?></p>
<?php endif; ?>

It works, but I do not know how to restrict according to the type of content.

How can it be done? Also, is it the good method?

1 Answer 1

0

You can use drupal_set_message in hook_node_view to display the text as a message to the users.

function MODULE_NAME_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'your_node_type') {
    if (!field_access('view', field_info_field('field_name'),'node', $node)) {
      drupal_set_message(t('Your text.'), 'warning');
    }
  }
}
6
  • Thanks Suresh. I thought of the drupal message, but I prefer one <p>. You know how can it be done? I had also thought of using a field to show the message, but my tries did not work.
    – Hgfdsoiu
    Commented Feb 23, 2016 at 12:11
  • I think you can follow the normal way of adding markup in t(). Something like t('!para_start your text !para_end', array('!para_start' => '<p>', '!para_end' => '</p>'));, but am not so sure if it is the right way. Just give it try
    – Suresh R
    Commented Feb 23, 2016 at 12:34
  • i try the first code with drupal message and it does'nt work.
    – Hgfdsoiu
    Commented Feb 23, 2016 at 13:39
  • Did you clear the cache? replace MODULE_NAME with your module name. Try to put print_r to check if the condition passes.
    – Suresh R
    Commented Feb 24, 2016 at 4:33
  • I have clear the cache, but it does not work. I tried print_r(), but there also, nothing displays. Would not you rather use hook_node_view_alter()? For information, I put this function in my template.php, but I do not think the problem comes from there.
    – Hgfdsoiu
    Commented Feb 24, 2016 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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