2

I'm using the following code to show the Edit button to edit pages in my blog teasers and others content types.

<a href="<?php echo '[node:edit-url]';?>">Edit</a>

It works fine, but it shows for all users, even the users with no permission to edit. How can I hide it for users that don't have permission to edit?

3 Answers 3

5

Assuming that you are talking of the blog content type, you should need to use code that checks:

  • If the user who is currently viewing the blog node is the author of the node and has the "edit own blog content" permission
  • If the user who is currently viewing the blog node is not the author of the node and has the "edit any blog content" permission

    <?php
      if ('[node:author:nid]' == $GLOBALS['user']->uid) {
        $permission = user_access('edit own blog content');
      }
      else {
        $permission = user_access('edit any blog content');
      } 
    ?>
    <?php if ($permission): ?>
      <a href="<?php print '[node:edit-url]';?>">Edit</a>
    <?php endif; ?>
    

A more generic code would be the following one.

<?php $item = menu_get_item('[node:edit-url]'); ?>
<?php if (!empty($item['access'])): ?>
  <a href="<?php print '[node:edit-url]';?>">Edit</a>
<?php endif; ?>

This code is more generic because it shows the link whenever the current user has access to the edit link, whatever permissions are required to see that link. It could be a module alters which users can see the node edit page, and this code would take that in consideration.
The code is assuming [node:edit-url] is a URL like node/10/edit; if the URL is /node/10/edit or http://example.com/node/10/edit, then the code needs to be changed to extract the node/10/edit part, since that is the string menu_get_item() is expecting. See for example the code used by blog_menu_local_tasks_alter().

  // Add action link to 'node/add/blog' on 'blog' page.
  if ($root_path == 'blog') {
    $item = menu_get_item('node/add/blog');
    if ($item['access']) {
      $item['title'] = t('Create new blog entry');
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }

That code is part of the Blog module, and it uses menu_get_item() to check if the currently logged-in user has access to the page to create a new blog. It doesn't merely verify the user has the permission to create a blog node, since that would not be enough if a module changes when a user can access the node create form.
Also note that the argument passed to menu_get_item() doesn't have a / at the beginning.

Notice that the Token module defines a URL token type, for which it makes available the following sub-tokens:

  • path, the path component of the URL
  • relative, the relative URL
  • absolute, the absolute URL
  • brief, the URL without the protocol and trailing backslash
  • unaliased, the unaliased URL
  • arg, the specific argument of the current page (e.g. 'arg:1' on the page 'node/1' returns '1')

Unfortunately, at the moment the type for node:edit-url is not set to url; when it is, the following code will work.

<?php $item = menu_get_item('[node:edit-url:path]'); ?>
<?php if (!empty($item['access'])): ?>
  <a href="<?php print '[node:edit-url]';?>">Edit</a>
<?php endif; ?>
2

You can use the node_access function to see whether the current user can perform operations on a given node:

<?php if (node_access('update', $node)): ?>
  <!-- edit link here -->
<?php endif; ?>

https://api.drupal.org/api/drupal/modules%21node%21node.module/function/node_access

1

Use user_access function. Pass target permission as parameter. For node edit access you can use 'administer nodes'.

<?php if(user_access('administer nodes')): ?>
  <a href="<?php print '[node:edit-url]';?>">Edit</a>
<?php endif; ?>

Update

You can work with permission for editing own content. Permissions are set in drupal per each content type. For example for article content type permission will be edit own article content.

<?php if(user_access('edit any article content')): ?>
   <a href="<?php print '[node:edit-url]';?>">Edit</a>
<?php endif; ?>
2
  • Hello milkovsky, now it's only showing to administrator. I'm trying to make this visible also to authenticated user with permission to edit their own content. Do you know how can I do this? Thank you a lot
    – user19627
    Jul 30, 2013 at 22:53
  • I updated the answer. But kiamlaluno's answer describes solution better
    – milkovsky
    Jul 31, 2013 at 11:20

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.