2

I'm writing a small module for learning purpose, where I try to create a module settings form using hook_menu() and form_api.
Everything look good in my code but I still getting this error message :

Warning: require_once(/var/www/html/drupal/modules/system/demo_usernames_form.inc): failed to open stream: No such file or directory in menu_execute_active_handler() (line 515 of /var/www/html/drupal/includes/menu.inc). Warning: require_once(/var/www/html/drupal/modules/system/demo_usernames_form.inc): failed to open stream: No such file or directory in menu_execute_active_handler() (line 515 of /var/www/html/drupal/includes/menu.inc).

function demo_menu(){
    $items[]=array();
    $items['admin/config/people/usernames']=array(
        'title'=>'bad usernames list',
        'description'=>t('configure not allowed usernames'),
        'page_callback'=>'drupal_get_form',
        'page arguments'=>array('demo_usernames_form'),
        'access arguments'=>array('administer site configuration'),
        'file'=>'demo_usernames_form.inc',
        );
    return $items;


function demo_usernames_form($form,&$form_state){
    $form['bad_usernames']=array(
        '#type'=>'textfield',
        '#title'=>'Bad usernames',
        '#default_value'=>variable_get('bad_usernames'),
        '#description'=>t('Enter a list of "bad usernames" separated by commas.  If a user tries to enter one of these usernames either on account creation or when editing their account, the form will display an error message.'),
        );
    return(system_settings_form($form));

}
  • If you've recently changed anything in hook_menu, make sure you clear Drupal's caches, the changes won't get picked up otherwise – Clive Aug 21 '15 at 17:25
  • I cleared the cache many times, the menu titles appears in the configuration page, but it's look like my module can't access to demo_usernames_form.inc file or it failing to passing the argument to drupal_get_form() function. – learner123 Aug 21 '15 at 17:35
5

If you look a bit closer at the path in the error message:

/var/www/html/drupal/modules/system/demo_usernames_form.inc

You'll notice it's trying to find your include file in the system module.

It's happening because the user module's implementation of hook_menu() has this in it:

$items ['admin/config/people'] = array(
  'title' => 'People',
  'description' => 'Configure user accounts.',
  'position' => 'left',
  'weight' => -20,
  'page callback' => 'system_admin_menu_block_page',
  'access arguments' => array('access administration pages'),
  'file' => 'system.admin.inc',
  'file path' => drupal_get_path('module', 'system'),
);

Notice the file path at the bottom, explicitly setting the path Drupal should look for the included file in.

Since properties are inherited from parent paths, and the path of your item sits nicely under that one, you get an implicit file path that you weren't expecting. The result: your module path is no longer consulted when the system tries to find the include file.

To fix, just add the correct file path to your hook_menu() code:

$items['admin/config/people/usernames']=array(
  'title'=>'bad usernames list',
  'description'=>t('configure not allowed usernames'),
  'page_callback'=>'drupal_get_form',
  'page arguments'=>array('demo_usernames_form'),
  'access arguments'=>array('administer site configuration'),
  'file'=>'demo_usernames_form.inc',
  'file path' => drupal_get_path('module', 'demo'),
);

And flush them caches.

  • Yes this fixed it, but I get WSOD in admin/config/people/usernames – learner123 Aug 21 '15 at 18:35
  • $form;&$form_state doesn't look right... – Clive Aug 21 '15 at 18:36
  • I already corrected that and cleared the cache before I commented your answer, so this is not the problem. – learner123 Aug 21 '15 at 18:41
  • I get already that Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'system_admin_menu_block_page' not found or invalid function name in menu_execute_active_handler() (line 517 of /var/www/html/drupal/includes/menu.inc). – learner123 Aug 21 '15 at 18:53
  • page_callback needs to be page callback – Clive Aug 21 '15 at 19:00
1

it's weird because it's calling "demo_usernames_form.inc" from drupal/modules/system/ not from your module directory in your case demo/. make sure you clear cache. if did't work try to add "file path" and see how it goes, this just for debugging but it should pick the file from your module by default. see more here hook_menu: "file path": The path to the directory containing the file specified in "file". This defaults to the path to the module implementing the hook."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.