2

I get the error in below code, it is strange, why it occurred?

In hook_menu

$items['share/%'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array('ym_alter_sharing_content'),
    'access callback' => 'user_is_logged_in',
    'type' => MENU_CALLBACK,
);

and:

function ym_alter_sharing_content($form, $form_state) {
  module_load_include('inc', 'node', 'node.pages');
  $nid=arg(1);
  $form = node_add('share');

  //$form['field_nid']['und'][0]['value']['#value']=$nid;
  //$form['#submit'][]='ym_alter_sharing_submit';
  //form_load_include($form_state, 'inc', 'node', 'node.pages');
  return $form;
}

but it return me:

Fatal error: Call to undefined function node_form_validate()

Where is the problem?

7
  • Maybe you forgot to include node.pages.inc from node module? that's where node_form_validate() is defined.
    – Mołot
    Jan 8 '14 at 13:39
  • @Mołot I include it module_load_include('inc', 'node', 'node.pages');
    – Yuseferi
    Jan 8 '14 at 13:41
  • 1
    It's because you're trying to include another form into your existing form. You need to use hook_form_alter(). I'm sure you know that already though
    – Clive
    Jan 8 '14 at 13:41
  • 3
    You haven't declared a callback function, you've declared a form and are using drupal_get_form() as your callback. Change your callback to ym_alter_sharing_content() and return node_add('share'); or something like that
    – Clive
    Jan 8 '14 at 13:45
  • 1
    Glad you got it sorted :)
    – Clive
    Jan 8 '14 at 13:54
1

Change page callback form drupal_get_form to function, because if you use drupal_get_form in call back it return form and in your function you create another form, it means form in form and make conflict.

$items['share/%'] = array(
    'page callback' => 'ym_alter_sharing_content',
    'access callback' => 'user_is_logged_in',
    'type' => MENU_CALLBACK,
);
1
  • Always use form_load_include() in forms. Otherwise you will not load your files when form is cached.
    – milkovsky
    Nov 7 '14 at 9:46
0

You got the error, because of missing &.

When you're using form_load_include() in hook_form_alter(), make sure you're following the right API syntax which is:

hook_form_alter(&$form, &$form_state, $form_id)

otherwise form_load_include() can't perform alterations of your current state of the form ($form_state).

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.