1

I am trying to make a button for people logged in to be able to upload an image with a title. It would be great to have it open in a modal window. I have this inside my _preprocess_node() function in the template.php.

  global $user;
  $vars['user'] = $user;
  $node = $vars; 

  if (!$vars['page']) {
    $vars['theme_hook_suggestions'][] = 'node__'.$vars['type'].'__'.$vars['view_mode'];
  }

  switch ($node['type']) {

     case 'my_page-node_type':

       // Code I'm having issue with
       $vars['aviation_upload'] = '<div class="btn-wrapp">' . ctools_modal_text_button('Upload Image', 'modal_forms/nojs/view/node/459/edit', 'Upload Image', 'btn link-upload ctools-modal-modal-popup-large') . '</div>';

     break; 

When I call this variable onto my page and I click the button, it shoots back this error.

An AJAX HTTP error occurred.
HTTP Result Code: 404
Debugging information follows.
Path: /smo/trunk/modal_forms/ajax/view/node/459/edit

Shows the path is wrong. What path do I use for a node edit? I'm not understanding how to properly load a node edit link (just 1, not all).

Thanks for any help.


Update:

I am going down the route of setting up a custom module instead and trying to control it there with using Modal for node edit/add links.

My current custom module settup:

custom_module.info

name = "Node edit modal"
description = "How to load a node edit form using Ctools module in Drupal 7."
package = "Modal Gallery"
core = 7.x
files[] = custom_module.module

dependencies[] = "ctools"

custom_module.module

<?php 
/**
 * Implementation of hook_menu().
 */
function custom_module_menu() {
  $items = array();

  // @see node_menu in node.module:
  // @todo Remove this loop when we have a 'description callback' property.
  // Reset internal static cache of _node_types_build(), forces to rebuild the
  // node type information.
  node_type_cache_reset();
  foreach (node_type_get_types() as $type) {
    $type_url_str = str_replace('_', '-', $type->type);
    $items['modal/node/add/' . $type_url_str . '/%ctools_js/%'] = array(
      'title' => $type->name,
      'title callback' => 'check_plain',
      'page callback' => 'custom_module_node_page_add',
      'page arguments' => array($type->type, 4, 5),
      'access callback' => 'node_access',
      'access arguments' => array('create', $type->type),
      'description' => $type->description,
      'file' => 'custom_module.pages.inc',
    );
  }
  $items['modal/node/%node/edit/%ctools_js/%'] = array(
    'title' => 'Edit',
    'page callback' => 'custom_module_node_page_edit',
    'page arguments' => array(2, 4, 5),
    'access callback' => 'node_access',
    'access arguments' => array('update', 2),
    'weight' => 0,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'file' => 'custom_module.pages.inc',
  );


  return $items;
}

custom_module.pages.inc

<?php 
/**
  * Page Callback - Modal: Add Node
  */
function custom_module_node_page_add($type, $js, $force_page_reload = FALSE) {
  // Fall back if $js is not set.
  if (!$js) {
    return drupal_goto('node/add/' . str_replace('_', '-', $type));
  }

  // Get node form defaults.
  global $user;
  $types = node_type_get_types();
  $node = (object) array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $type,
    'language' => LANGUAGE_NONE
  );

  $title = t('Create @name', array('@name' => $types[$type]->name));

  custom_module_node_form($node, $title, $force_page_reload);
}

/**
 * Page Callback - Modal: Edit Node
 */
function custom_module_node_page_edit($node, $js, $force_page_reload = FALSE) {
  // Fall back if $js is not set.
  if (!$js) {
    return drupal_goto('node/' . $node->nid . '/edit/');
  }

  $type_name = node_type_get_name($node);
  $title = t('<em>Edit @type</em> @title', array('@type' => $type_name, '@title' => $node->title));

  custom_module_node_form($node, $title, $force_page_reload);
}


/**
 * Build the Modal Form.
 *
 * @param object $node
 *   The node Object in edit case. In Add case it's only a pseudo node object.
 * @param string $title
 *   The Modal Popup Title.
 * @param boolean $force_page_reload 
 *   Indicates if page should be reloaded after form submission.
 */
function custom_module_node_form($node, $title, $force_page_reload = FALSE) {
  $commands = array();

  ctools_include('node.pages', 'node', '');
  ctools_include('modal');
  ctools_include('ajax');

  $form_state = array(
    'title' => $title,
    'ajax'  => TRUE,
    'build_info' => array(
      'args'  => array($node),
    ),
  );

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

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

      // overwrite commands.
      $commands = array();

      ctools_add_js('ajax-responder');
      $commands[] = ctools_modal_command_dismiss();
      // if (t('Delete') == $form_state['values']['op']) {
      //   $commands[] = ctools_ajax_command_redirect('modal/node/' . $node->nid . '/delete/ajax/reload');
      // }
      // else 
      if ($force_page_reload) {
        $commands[] = ctools_ajax_command_reload();
      }
  }

  // add CSS class to Modal-Frame
  $commands[] = ajax_command_invoke('#modalContent', 'addClass', array('custom-module-node', 'custom-module-node-form'));

  print ajax_render($commands);
  exit;
}

And I'm using this on my page.tpl to try and open a node add in a modal window:

<a href="add/my-node-type/nojs/">Create new node</a>
  • I saw this post earlier, but is creating a module the only way to do this? – tdm Aug 19 '14 at 21:37
  • There are probably other ways, but I don't know of them. However, I think it is the most clean way to implement it. All you need is already in that question (as far as I can see). Did you try the solution to see whether it would solve your use case? – Sidney Gijzen Aug 20 '14 at 12:43
  • No, I never did. I was thinking of taking an easier approach and having this modal form open a 'Create New Node Content' instead. Same issue though when I try to load modal_forms/nojs/node/add/my-node-type-upload. I could go down the route of creating a webform to handle this and use modal_forms/nojs/webform/%node but really rather not. – tdm Aug 20 '14 at 21:46
  • Ok, I created a custom module using what was here - drupal.stackexchange.com/a/45150/20734. Adding/Editing/Viewing a node won't work. Just not quiet getting it... – tdm Aug 21 '14 at 15:39
  • Do you get any errors/messages? Could you also update your question with what you've tried? Then other people can chime in easier :) – Sidney Gijzen Aug 21 '14 at 16:03

Browse other questions tagged or ask your own question.