Skip to main content

Example of hook_permission()

Example of hook_permission()

hook_permission() is a handy way to allow certain roles to access certain pages, i.e. the 'Administrator' to change settings and say 'Comms Team' to view the results. As per a lot of Drupal building the return value must be a nested array, and that can be tricky to remember... So here is an example.

/**
 * Implementation of hook_permission().
 */
function MY_MODULE_NAME_permission() {
  return array(
    'view MY_MODULE_NAME' => array(
      'title' => t('View MY_MODULE_NAME'),
      'description' => t('Allow certain roles to view MY_MODULE_NAME without control of the settings.'),
    ),
    'administer MY_MODULE_NAME' => array(
      'title' => t('Administer MY_MODULE_NAME'),
      'description' => t('Add, delete and edit the settings for MY_MODULE_NAME.'),
    ),
  );
}

Simply replace MY_MODULE_NAME with the 'machine' name of your module.

Text that appears inside the t() function can be a 'real' name rather than the 'machine' name as this is just text to be displayed on the 'permissions' page.

Main Category
  • Module Building
  •  
Secondary Categories
  • Drupal
  • Drupal 7
  •  
 
Jon Moore

Search form

Jon Moore

Tips 'n' Snips

 
  • Home
  • General
  • Drupal
    • Drupal 6
    • Drupal 7
    • Module Building
  • HTML & CSS
  • Javascript
  • jQuery
  • PHP
    • CodeIgniter
    • Handy Functions
    • MySQL
  • WordPress
  • About

BBC Technology News

  • Heathrow: Man charged with flying drone near airport
  • Girl Scouts of America offer badge in cybersecurity
  • Rise in nude photos sent to phones on public transport
  • Are these the faces of the future?
  • Zimbabwe blocks Facebook, WhatsApp and Twitter amid crackdown
More

Sitepoint

  • How to Build Your First Static Site with Gatsby
  • Customer Journey Maps: A Day in the Life of a Customer
  • How to Take JavaScript Beyond the Web in 2019
  • Check out the 6 Best Productivity Apps for Designers
  • 7 Awesome Wireframing Tools You Should Be Using
More

Heart Internet

  • Did he really just say that? How to deal with an obnoxious client
  • Product management for designers
  • Talking to your clients about WordPress plugin security
  • The developer’s guide to native web animation
  • Are you seeing clearly? The role of visual thinking in creative work
More
 

Backend Coders

  • PHP
  • Handy PHP Functions
  • PHP MySQL

Frontend Coders

  • HTML & CSS
  • jQuery
  • Javascript

CMS/CMF Systems

  • Drupal
  • Drupal 6 Specific
  • Drupal 7 Specific
  • Drupal Module Building
  • WordPress

About

  • Home
  • About
 

© Jon Moore 2019

All stock images are from www.istockphoto.com