1

I am creating a custom module that allows for users to create blog posts that will be private and related to a project. So I created a new content_type called "tasker_blog" that just has a title and body. The user goes to view their projects and clicks a link which takes them to 'tasker_project/%/blog' % being the id of the project. I have the form being displayed correctly but when I submit the form I have two problems, 1) the title is lost on submit 2) getting this error: EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids().

Here is the code I have written (some non-relevant parts taken out).

function tasker_project_menu() {
    $items = array();

    $items['tasker_project/%/blog'] = array(
        'title' => 'Private Blog Post',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('tasker_project_blog_form',1),
        'access arguments' => array('view tasker projects'),
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function tasker_project_blog_form($form, &$form_state) {
    drupal_add_css(drupal_get_path('module', 'tasker_project') . '/tasker_project.css'); 
    global $user;
    module_load_include('inc', 'node', 'node.pages');
    $node = (object) array(
        'uid' => $user->uid,
        'name' => (isset($user->name) ? $user->name : ''),
        'type' => 'tasker_blog',
        'language' => 'und',
    );
    node_object_prepare($node);

    $form = drupal_get_form('tasker_blog_node_form',$node);
    $form['hidden_project_id'] = array(
        '#type' => 'hidden',
        '#value' => arg(1),
    );

    return $form;
}

If I submit with nothing else added I get the error I mentioned above. If I add this (using Kint to display variables):

function tasker_project_node_validate($node, $form, &$form_state) {
    s($_REQUEST);
    s($form_state['values']);
    die();
}

$_REQUEST array output:

array (9) (
    'title' => string (4) "test"
    'body' => array (1) (
        'und' => array (1) (
             array (2) (
                'format' => string (13) "filtered_html"
                'value' => string (13) "<p>test</p>
"
            )
        )
    )
    'changed' => string (0) ""
    'form_build_id' => string (48) "form-eKWwyFlBOzi4LsajaAiEZBG7J0uOSI1UDVIhiDomeJE"
    'form_token' => string (43) "N421-IiWecixBJGbxTHGcJAIrd6ZutzAW0LAtVSsrJ4"
    'form_id' => string (24) "tasker_project_blog_form"
    'hidden_project_id' => string (2) "10"
    'additional_settings__active_tab' => string (0) ""
    'op' => string (4) "Save"
)

$form_state['values'] array output:

array (27) (
    'nid' => NULL
    'vid' => NULL
    'uid' => string (1) "5"
    'created' => integer 1347038959
    'type' => string (11) "tasker_blog"
    'language' => string (3) "und"
    'changed' => string (0) ""
    'title' => string (0) ""
    'additional_settings__active_tab' => string (0) ""
    'revision' => bool FALSE
    'log' => string (0) ""
    'name' => string (10) "ndenlinger"
    'date' => string (0) ""
    'status' => integer 0
    'promote' => integer 0
    'sticky' => integer 0
    'submit' => string (4) "Save"
    'preview' => string (7) "Preview"
    'body' => array (1) (
        'und' => array (1) (
             array (3) (
                'summary' => string (0) ""
                'format' => string (13) "filtered_html"
                'value' => string (13) "<p>test</p>
"
            )
        )
    )
    //...MORE HERE REMOVED SINCE NOT RELEVANT
)

So I'm not sure if the two problems are related or not. Once the node is saved I am going to add a row to a custom table that store the nid and the project_id.

  • I suggest you print_r or s() $node after node_object_prepare($node);, and before return $form look at $form['title'] ,check it ,maybe problem be in form title field – Yusef Sep 8 '12 at 4:32
  • Tried that everything looks correct before submitting. I tried setting my own '#validate' and the form_state variable has the title. It is the hook_node_validate() function that is stripping the title for some reason. – Pitchinnate Sep 10 '12 at 12:29
1

Found the solution in case anyone else ever has this issue:

    drupal_add_css(drupal_get_path('module', 'tasker_project') . '/tasker_project.css'); 
    global $user;
    module_load_include('inc', 'node', 'node.pages');
    $node = (object) array(
        'uid' => $user->uid,
        'name' => (isset($user->name) ? $user->name : ''),
        'type' => 'tasker_blog',
        'language' => 'und',
    );
    node_object_prepare($node);
    $form_state['build_info']['args'] = array($node);

    $form = drupal_retrieve_form('tasker_blog_node_form', $form_state);
    drupal_prepare_form('tasker_blog_node_form', $form, $form_state);
    $form['hidden_project_id'] = array(
        '#type' => 'hidden',
        '#value' => arg(1),
    );

I used drupal_retrieve_form and drupal_prepare_form and everything seems to work correctly now. Not sure why drupal_build_form or drupal_get_form don't work correctly.

0

Test this

function tasker_project_blog_form($form, &$form_state) {
drupal_add_css(drupal_get_path('module', 'tasker_project') . '/tasker_project.css'); 
global $user;
module_load_include('inc', 'node', 'node.pages');
$node = new stdClass();
$node = (object) array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => 'tasker_blog',
    'language' => 'und',
);
node_object_prepare($node);

$form = drupal_get_form('tasker_blog_node_form',$node);
$form['hidden_project_id'] = array(
    '#type' => 'hidden',
    '#value' => arg(1),
);

return $form;

}

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.