2

I have a menu local task callback in hook_menu():

$items['leg/add'] = array(
    'title' => t('Create a new leg'),
    'page callback' => 'mymodule_leg_node_form',
    'access arguments' => array('create leg content'),      
    'file' => 'node.pages.inc',
    'file path'=> drupal_get_path('module', 'node'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
);  

This is the page callback function:

function mymodule_leg_node_form(){
    $form = drupal_get_form('leg_node_form');
    return $form;
}

When I visit that page, I get the following error:

Fatal error: Cannot access empty property in path\to\site\modules\field\field.attach.inc on line 197

I have a "Leg" content type, and I just want to show the form to create nodes of that type in a tab.

My content type has two fields: "field_from" and "field_to."
drupal_get_form() calls _field_invoke(), and the error is generated from that function.

I tried to change the menu definition as below, but I get the same error.

$items['leg/add'] = array(
    'title' => t('Create a new leg'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('leg_node_from'),   
    'access arguments' => array('create leg content'),      
    'file' => 'node.pages.inc',
    'file path'=> drupal_get_path('module', 'node'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
);

I'm implementing something similar to what reported in http://drupal.org/node/251526#comment-827860; it might work in Drupal 6 with the CCK module, but it doesn't seem to work on Drupal 7.

I found the same problem reported in Drupal bug issues, but I could not find any qualified answer.

4 Answers 4

2

Solved this issue when I upgraded to drupal 7.10, but I had to create a node object in the page callback function.

function mymodule_leg_node_form(){
    global $user;
    global $language;

    $node = new stdClass();
    $node->uid = $user->uid;
    $node->type = 'leg';
    $node->language = $language->language;

    $form = drupal_get_form('leg_node_form', $node);
    return $form;
}
3
  • The next problem I got is that although the node form is submitted correctly, node is not saved. When I printed $form_state['#values'] from my hook_node_submit, I see nid is blank. Do I need to call node_save() explicitly.
    – Sithu
    Jan 20, 2012 at 3:06
  • And also form validation does work. I got required error messages, but text boxes are not highlighted with red as in our node form.
    – Sithu
    Jan 20, 2012 at 5:54
  • you have forgotten $form and &$form_state in your form function definition.
    – reptilex
    Jan 20, 2012 at 10:26
0

My first guess is that your form is not being built correctly. Some field is not valid. If it's not that in your code 'leg/add' seems risky, I don't know, but maybe 'leg_add' would not throw the error? This is just guessing since there is a lot of code you did not post (like your form).

1
  • I created a content type with 2 new cck fields from admin panel. Its form id is leg_node_form. That's all. When I visit node/add/leg, the form is correctly rendered.
    – Sithu
    Jan 19, 2012 at 11:38
0

Do I get you right, that you have content type "let" with two (or more fields)? There's no need to code anything with forms or menu. Fields (or CCK in D6) does everything for you. Simply go to node/add/leg (after removing your hook_menu stuff) and you will see your form.

1
  • I know that. But I need to implement it into a local task. The nod/add/leg is just a menu normal or menu callback.
    – Sithu
    Jan 20, 2012 at 2:46
0

Node is not saved when the add node form is implemented in a menu local task.
Finally, I solved this by calling node_save() explicitly in my extra_submit function attached to $form['#submit'].

function mymodule_leg_node_submit($form, &$form_state){
    global $user;
    $values = $form_state['values'];
    $nid = $values['nid'];
    if(empty($nid)){
        # Create a node object, and add node properties.
        $node = new stdClass();
        $node->type = 'leg';
        $node->uid = $user->uid;
        $node->created = strtotime("now");
        $node->changed = strtotime("now");
        $node->status = 1;
        $node->comment = 0;
        $node->promote = 0;
        $node->moderate = 0;
        $node->sticky = 0;
        $node->language = LANGUAGE_NONE;
        $node->field_from[LANGUAGE_NONE][0]['value'] = $values['field_from'][LANGUAGE_NONE][0]['value'];
        $node->field_to[LANGUAGE_NONE][0]['value'] = $values['field_to'][LANGUAGE_NONE][0]['value'];

        node_object_prepare($node);
        // Save the node.
        // The node will also get published, if $node->status was set to 1.
        node_save($node);       
        $nid = $node->nid;
    }   
    $form_state['redirect'] = 'leg/' . $nid . '/transport';
    drupal_redirect_form($form_state);
}

I don't know why drupal did not save the node in this case, probably it is a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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