I am trying to create a link to edit nodes but right now it is showing on every page. I need it to show the link only if the current user can edit the node.

This is what i have right now.

<div><?php print l('Edit', 'node/' . $node->nid . '/edit', array('query' => drupal_get_destination())); ?></div>

After I get the link to show only to users that can edit the node I am going to make the link an icon next to the page title.

Thank you for your help.

Comments

DevJoshLopez’s picture

I tried to add

<?php if (user_access('administer nodes')): ?>
<div><?php print l('Edit', 'node/' . $node->nid . '/edit', array('query' => drupal_get_destination())); ?></div>
<?php endif; ?>

But it prints only for admin user.

DevJoshLopez’s picture

I also tried:

<?php
      $path = "node/".$node->nid."/edit";
      if (node_access('update',$node)){
      print l(t('Edit'), $path);
      }
 ?>

but thats giving me Notice: Undefined variable: node in include()

ayesh’s picture

Try this:

if (arg(0) == 'node' && is_numeric(arg(1))){
$node = node_load(arg(1));
$path = "node/".$node->nid."/edit"; 
if (node_access('update',$node)){
 print l(t('Edit'), $path);
}
DevJoshLopez’s picture

I also need it to not print on view pages

samwillc’s picture

This prints on view pages:

if(function_exists('views_get_page_view') && views_get_page_view()) {
print l( STUFF_IN_HERE );
}

Maybe you could reverse the conditions?

Sam.

batigolix’s picture

Solution using hook_preprocess_node and node.tpl.php template: http://stackoverflow.com/a/33258230/1127583