Search code examples
drupal-7

URL hit showing output didn't want when I give permission to the module


On direct URL hit it showing the result I want not to done it but when I give permission to it it should be work fine. Help me regarding this I will be very grateful to you.

This is my module provide me the code for that module:

<?php
        // $Id: person.module

        /**
         * implements hook_menu()
         */
        function person_menu(){ 
            $items  = array();
            $items['person'] = array(
                'title' => "Person",
                'page callback' => "perso_personal_info", // after visit drupal6/person, person_personal_info() function is called
                'access callback' => true,  // must return true, otherwise it will not visible as menu item
                'type' => MENU_NORMAL_ITEM, // drupal's default menu type   
                'weight' => '10', // we want to display person link below in our nav menu
            );      
            return $items; // finally, do not forget to return $items array
        }
        function perso_personal_info(){
            $output = 'Name: Gaurav</br>';
            $output .= 'City: nanital </br>';
            $output .= 'Country: india </br>';
            return $output;
        }
        function person_permission(){ 
        return array(
         'administer my module' => array( 
        'title' => t('Administer my module'), 
        'description' => t('Perform administration tasks for my module.'),
         ), 
        ); }
        ?>

Please provide me the the code needed; it should work fine when I set the permission for my module.


Solution

  • You need to update "access callback" in your hook_menu in which users permission will be checked as below:

    /**
     * implements hook_menu()
     */
    function person_menu() {
      $items  = array();
      $items['person'] = array(
        'title' => "Person",
        'page callback' => "demo_custom_personal_info", // after visit drupal6/person, person_personal_info() function is called
        //'access callback' => true,  // must return true, otherwise it will not visible as menu item
        'access callback' => 'person_personal_info_check_access',
        'type' => MENU_NORMAL_ITEM, // drupal's default menu type   
        'weight' => '10', // we want to display person link below in our nav menu
      );
      return $items; // finally, do not forget to return $items array
    }
    

    Now you will need to add below function in you module file (this function will check user's access permission which will assign from permission page)

    /**
     * To check user's permission
     */
    function person_personal_info_check_access() {
      if (user_access('administer my module')) {
        return TRUE;
      }
      return FALSE;
    }