‘Add node’ link in Drupal Views’ header

Fremantle

Very often one wants to add a link to a View’s header or footer that allows users with permission to quickly jump to the relevant content creation form.

It’s easy to create a Global: Text area with the link, but this would then be seen by all users, regardless of authorization.

A few people have had similar thoughts.

I don’t know if there’s a better way (i.e. probably one that doesn’t involve PHP, not because this is a particularly complicated way of doing things but just that I always figure that Drupal should be usable by non-coders), but here’s how I’ve got it working:—

Required modules: views, views_ui, and php.

Add a Global: Text area header to a view, and select the PHP code text format for it. The code is as follows:

<?php if (user_access('create article content')): ?>

<a href="<?=url('node/add/article')?>" class="btn btn-default">Add new article</a>
<?php endif ?>