Domanda

I have created a custom Drupal7 form module , but I want to show the form to the authenticated users only . I want to create the module likewise that it could have check-box options in the people->permission section from where I could set the permission for this module for all type of users .Here is the menu_hook

function form_example_menu() {
  $items = array();
  $items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Example Form', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'), //put the name of the form here
    'access arguments' => array('access administration pages'),
    'access callback' => TRUE
  );
  return $items;
}

I am new in drupal so any help regarding this would be appreciable.If someone could write down the hook_permission rather than giving examples then it would be a great help.

È stato utile?

Soluzione

Here is the implementation of hook_permission

/**
 * Implements hook_permission().
 */
function form_example_permission() {
  return array(
    'administer your module' => array(
      'title' => t('Administer permission for your module'),
      'description' => t('Some description that would appear on the permission page..'),
    ),
  );
}

And you have to give the key of the returned array (administer your module) to access arguments in the implementation of hook_menu So, your hook_menu implementation would become:

function form_example_menu() {
  $items = array();
  $items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Example Form', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'), //put the name of the form here
    'access arguments' => array('administer your module'),
  );
  return $items;
}

Note that you'll have to flush the cache after you change anything in hook_menu. You can do it from admin/config/development/performace/

Altri suggerimenti

Try this. After adding this clear your cache and go to the people->permission and from there you could set permission for users .

function form_example_permission() {
  return array(
    'administer my module' => array(
      'title' => t('Administer my module'),
      'description' => t('Perform administration tasks for my module.'),
    ),
  );
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top