Hi, I want to have an edit this link on my node pages.
Is there a better way to do it then something like this?:

global $user;
if($user->uid) {
echo 'edit';
}

I can't believe there's no built-in check to see if the logged in user is allowed to edit the node or not.

Comments

ceejayoz’s picture

You can use hook_link() to do this:


function module_name_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();

  if ($type == 'node') {
    if (node_access('update', $node)) {
      $links['node_edit'] = array(
        'title' => t('Edit'),
        'href' => "node/$node->nid/edit",
        'attributes' => array('title' => t('Edit this node.'))
      );
    }
  }

  return $links;
}

Placed in a module, this will add a link to the $links variable (where the "read more" link goes).

The key bit is node_access('update', $node). That's the built-in check you're looking for.

Nda’s picture

massive, thanks I'm gonna try that

Zoologico’s picture

There was enough info here to get what I needed done.
Thanks.

In my node template, I just used:

<?php
	if (node_access('update', $node)) {
      print l("Edit", "node/$node->nid/edit");
    }
	?>
Nda’s picture

For me it worked to just put {$tabs} above the {$content} in my template... silly me.

Zoologico’s picture

Could you add a code sample to illustrate?

ceejayoz’s picture

It's just print $tabs; - themes should already do this by default, in most cases. They're the tabs that display up the top of nodes.

Nda’s picture

Ah ye sorry, I use smarty so.. :)

ceejayoz’s picture

Isn't it just {$tabs} or {tabs} or something, then?

Nda’s picture

yea

{$tabs}
{$title}
{$help}
{$messages}
{$content}

stuff like that