Drupal - Showing a node edit page with drupal_get_form() and hook_menu()

hook_menu() is only evaluated when the caches are rebuilt, so the call to module_load_include() won't be called when the path is actually visited.

That hook is only meant for declaring menu items, if there's any logic that needs to be added for a path it should happen in the access callback/page callback like so:

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

  $items['test/test'] = array(
    'title' => 'Edit this node',
    'description' => 'Edit this node.',
    'page callback' => 'control_center_test_callback',
    'page arguments' => array('page_node_form', $node),
    'access callback' => TRUE,
  );
  return $items;
}

function control_center_test_callback() {
  module_load_include('inc', 'node', 'node.pages');
  $nid = '4';
  $node = node_load($nid);

  return drupal_get_form('page_node_form', $node);
}

Since you're forwarding to a static node edit page though, it might be easier just to set the page callback to drupal_goto() and hardcode the URL:

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

  $items['test/test'] = array(
    'title' => 'Edit this node',
    'description' => 'Edit this node.',
    'page callback' => 'drupal_goto',
    'page arguments' => array('node/4/edit'),
    'access callback' => TRUE,
  );
  return $items;
}

You should define the menu callback as in the following code.

  $items['test/test'] = array(
    'title' => 'Edit this node',
    'description' => 'Edit this node.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('page_node_form', $node),
    'file path' => drupal_get_path('module', 'node'),
    'file' => 'node.pages.inc',
    'access callback' => TRUE,
  );

Remember that an implementation of hook_menu() is being invoked only when menus are rebuild, not each times a new page is requested to Drupal. As consequence of this, the form would get an updated node object only when your module is installed, or when the menus are being rebuilt; in other cases, the form will get a static copy of the node object, which doesn't reflect the actual node object.

It would be better to make test/test a path alias for node/4/edit. In that way, the form would get an updated node object every time.

Tags:

Forms

Routes

7