Drupal 7: Creating custom Permissions hook_permission() with Example

Permissions control access in Drupal system. We can able to specify the permissions based on User Roles. We can programmatically able to create custom permissions by implementing the hook_permission in Drupal 7. The below is the syntax and example of how to use custom permissions.

Once permissions have defined the need to clear cache. So, that permission available on admin/people/permissions page.

Syntax:

/**
 * Implements hook_permission().
 */
function MYMODULE_permission() {
  return array(
    'PERMISSION_UNIQUE_NAME' => array(
      'title' => PERMISSION_READABLE_NAME,
      'description' => PERMISSION_DESCRIPTION,
    ),
  );
}

Example: 

/**
 * Implements hook_permission().
 */
function boost_cache_clear_permission() {
  return array(
    'config_boost_cache_clear' => array(
      'title' => 'Configuration for Boost cache clear',
      'description' => t('Configuration page for Content type level boost cache management. Warning: Give to trusted roles only; this permission has security implications.'),
    ),
  );
}

 

Category: