1

I added node ADD form in ctools modal window and ctools autosubmit functionality . So far so good, but my goal is to return node EDIT form when autosubmit triggered. Now modal callback function looks like this:

function my_module_node_add_modal_callback($js = FALSE) {
  global $user;
  if (!$js) {
    return "Javascript required";
  }
  ctools_include('node.pages', 'node', '');
  ctools_include('modal');
  ctools_include('ajax');
  ctools_add_js('auto-submit');


  // Create a blank node object.
  $node = (object) array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => 'node_type',
    'language' => LANGUAGE_NONE,
    );


  $form_state = array(
    'title' => t('Add node'),
    'ajax' => TRUE,
  );


  $form_state['build_info']['args'] = array($node);
  $output = ctools_modal_form_wrapper('node_type_node_form', $form_state);



  if (!empty($form_state['executed'])) {

  }


  print ajax_render($output);
  exit;
}

I tryed to add different varians of this:

    if (!empty($form_state['executed'])) {
  $output = array();
  if ($form_state['nid']) {
    $node = node_load($form_state['nid']);


     $form_state = array(
        'title' => t('Add node'),
        'ajax' => TRUE,
      );


     $form_state['build_info']['args'] = array($node);
     $output = ctools_modal_form_wrapper('node_type_node_form', $form_state);
  }
}

but it doesn`t work(

Any advice?

  • what means doesn't work ..... ??? how do you launch this modal ? – rémy Jun 5 '15 at 11:38
0

I use node edit in a ctools modal. The page callback fallbacks to the regular node edit page if JS is disabled, and is compatible if there is a node field widget that uses ajax itself. The page callback looks like this:

/**
 * Page callback to deliver node edit form via CTools AJAX/modal.
 *
 * @param boolean|null $js
 *   TRUE if Javascript is enabled, NULL otherwise.
 * @param object $node
 *   The node to edit.
 *
 * @return array
 *   Array of Drupal Ajax Commands.
 */
function pads_secure_file_edit_ajax($js, $node) {
  // Fallback if $js is not set.
  if (!$js) {
    module_load_include('inc', 'node', 'node.pages');
    return node_page_edit($node);
  }

  ctools_include('modal');
  ctools_include('ajax');

  $output = array();
  $type_name = node_type_get_name($node);
  $title = t('<em>Edit @type</em> @title', array(
    '@type' => $type_name,
    '@title' => $node->title,
  ));
  $form_state = array(
    'title' => $title,
    'ajax' => TRUE,
    'build_info' => array(
      'args' => array($node),
    ),
  );
  ctools_form_include_file($form_state, drupal_get_path('module', 'node') . '/node.pages.inc');
  $form_state['build_info']['args'] = array($node);

  $output = ctools_modal_form_wrapper($node->type . '_node_form', $form_state);

  if (!empty($form_state['executed'])) {
    $output = array();
    // If node submit, reload page.
    $triggering_element_id = array_key_exists('#id', $form_state['triggering_element']) ?
      $form_state['triggering_element']['#id'] : '';
    if (strpos($triggering_element_id, 'edit-submit') === 0) {
      $output[] = ctools_ajax_command_reload();
    }
    else {
      $output = array();
    }
  }

  print ajax_render($output);
  drupal_exit();
}
  • Sorry, I guess I described my problem not clear enough. When user click a link "Add node" he must see a ctools modal window with node add form. This part works fine with a piece of code I put in a guestion. But this form has autosubmit fields(it works with ctools ajax so user dont see any submitions), here become problem, cause every time user check any field - creates a new node. What I need is after first ajax submit and node creation, form in modal window change to node edit form, so the next time autosubmit works it will just edit a node wich was created the first. time – tarasikarius Jun 7 '15 at 12:36

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.