Skip to content

Instantly share code, notes, and snippets.

@thirdender
Last active December 28, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thirdender/7533792 to your computer and use it in GitHub Desktop.
Save thirdender/7533792 to your computer and use it in GitHub Desktop.
Build a Drupal 7 node form using the FAPI
<?php
/**
* This leverages the node and form APIs to output a node add form. It can be
* used as part of a hook_menu page callback, or output in any part of the
* page.
*/
// Create the empty $node object (replace CONTENT_TYPE with the machine name
// of your node type)
$node = new StdClass();
$node->type = 'CONTENT_TYPE';
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->name = isset($user->name) ? $user->name : 'anonymous';
node_object_prepare($node);
// Build a dummy $form_state
$form_state = array();
$form_state['build_info']['args'] = array($node);
// Ensure that node.pages.inc is included every time $form is processed
form_load_include($form_state, 'inc', 'node', 'node.pages');
// Voilà! Il est sous $form!
$form = drupal_build_form("{$node->type}_node_form", $form_state);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment