1

I want to add a custom page for creating "blog post". Ive got a node blog. So I write something like this:

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

    $items['user/%/edit/blog/add'] = array(
            'title' => 'Add blog post',
            'page callback' => 'drupal_get_form',
            'page arguments' => array('module_add_blog',1),
            'access arguments' => array('createBlog'),
            'type' => MENU_CALLBACK,

    );
return $items;
}

And then in module_add_blog callback:

function module_add_blog(&$form_state, $arg) {
    $form = array();

    $id = intval($arg);
    if ($id == 0){
        drupal_set_message('ID must be an integer.' , '$error');
        return;
    }

    $user = user_load($id);
    module_load_include('inc', 'node', 'node.pages');
    $node = array('uid' => $user->uid, 'name' => $user->name, 'type' => 'blog');


    $form = drupal_retrieve_form('blog_node_form', $form_state, $node);
    drupal_prepare_form('blog_node_form', $form, $form_state);
    return $form;
}

Form is rendered in path user/1/edit/blog/add but when I send data it is NOT SAVED.

Any ideas how fix it? Thanks

0

I think you're confusing Drupal by having a form within a form so-to-speak. I think it would be a lot easier to define a proxy function for the node form so you don't need to worry about doing so:

function module_menu() {
  $items['user/%/edit/blog/add'] = array(
    'title' => 'Add blog post',
    'page callback' => 'module_blog_form_proxy',
    'page arguments' => array(1),
    'access arguments' => array('createBlog'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function module_blog_form_proxy($uid) {
  // Validitiy checks etc...

  $user = user_load($uid);
  $node = array('uid' => $user->uid, 'name' => $user->name, 'type' => 'blog');

  module_load_include('inc', 'node', 'node.pages');
  return drupal_get_form('blog_node_form', $node);
}
1
  • it works. But Ive got another question. How can I do custom EDIT form for fe. user/%/edit/blog/edit/% path? – Michał Wiatr Jun 24 '12 at 11:50

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.