javascriptdrupaldrupal-hookshook-menu

Print a 'node/add' form in a lightbox with Drupal


I'm having some troubles printing a 'node/add' form in a lightbox.

I have in my custom.module a hook_menu like this:

  $items['get-form/%'] = array(
    'title' => t('Get a form'),
      'description' => t('Get form'),
      'page callback' => '_get_form',
      'page arguments' => array(1),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
    );

... where % is the id, like "story_node_form".

Then, I have the callback function like this:

function _get_form($form_id){
  module_load_include('inc', 'node', 'node.pages');

  if (strpos($form_string, "_node_form")){
    //Test if the form is a <type>_node_form. Is the node/add/<type>
    $content_type = explode("_node_form", $form_id)[0];
    print drupal_render(node_add($content_type));
  }

The forms shows right, in the lightbox. The problem is that the javascript of the form (wysiwyg, node references, term references, ...) doesn't work.

I tried to execute Drupal.attachBehaviors(), Drupal.attachBehaviors(document) and Drupal.attachBehaviors("#story-node-form") but nothing seems to work.

Anyone can help?


Solution

  • This code should do the trick:

    function _get_form($form_id){
        global $user;
        $node = (object) array(
            'uid' => $user->uid, 
            'name' => (isset($user->name) ? $user->name : ''), 
            'type' => 'ENTER NODE TYPE HERE', 
            'language' => LANGUAGE_NONE
        );
        $form_state['build_info']['args'] = array($node);
        form_load_include($form_state, 'inc', 'node', 'node.pages');
        echo drupal_render(drupal_build_form($form_id, $form_state));
    }
    

    You will need to get the node type as well as the form_id for this to work but that shouldn't be to difficult.