Sam Wilson

Wikimedia user link badge Mastodon user link badge GitHub user link badge

‘Add node’ link in Drupal Views’ header

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.

https://www.drupal.org/node/2006322 http://drupal.stackexchange.com/questions/131247/create-node-link-in-views-header-and-open-in-popup https://www.drupal.org/node/891142 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 ?>