You are here

modal_node_edit.module in Modal operations 7

Allows node editing, adding and deleting in modal window.

Reference: http://drupal.stackexchange.com/questions/21053/how-to-load-a-node-edit-...

@example Create links with the following href and class attributes (NID is the identifier of the node to edit, TYPE is the type of node to add): <a href="/modal/node/add/TYPE/nojs" class="ctools-use-modal">Create node and reload the page</a> or <a href="/modal/node/add/TYPE/nojs/0" class="ctools-use-modal">Create node without page reloading</a> or <a href="/modal/node/NID/edit/nojs" class="ctools-use-modal">Edit node and reload the page</a> or <a href="/modal/node/NID/edit/nojs/0" class="ctools-use-modal">Edit node without page reloading</a>. or <a href="/modal/node/NID/delete/nojs" class="ctools-use-modal">Delete node and reload the page</a> or <a href="/modal/node/NID/delete/nojs/0" class="ctools-use-modal">Delete node without page reloading</a>.

Ensure the page with such links executes the following functions (this is done in init code of modal.module): ctools_include('modal'); ctools_modal_add_js();

File

modal_node_edit/modal_node_edit.module
View source
<?php

/**
 * @file
 * Allows node editing, adding and deleting in modal window.
 *
 * Reference:
 * http://drupal.stackexchange.com/questions/21053/how-to-load-a-node-edit-form-using-ctools-modal-in-drupal-7
 *
 * @example
 * Create links with the following href and class attributes (NID is the identifier of the node to edit, TYPE is the type of node to add):
 * <a href="/modal/node/add/TYPE/nojs" class="ctools-use-modal">Create node and reload the page</a>
 * or
 * <a href="/modal/node/add/TYPE/nojs/0" class="ctools-use-modal">Create node without page reloading</a>
 * or
 * <a href="/modal/node/NID/edit/nojs" class="ctools-use-modal">Edit node and reload the page</a>
 * or
 * <a href="/modal/node/NID/edit/nojs/0" class="ctools-use-modal">Edit node without page reloading</a>.
 * or
 * <a href="/modal/node/NID/delete/nojs" class="ctools-use-modal">Delete node and reload the page</a>
 * or
 * <a href="/modal/node/NID/delete/nojs/0" class="ctools-use-modal">Delete node without page reloading</a>.
 *
 * Ensure the page with such links executes the following functions (this is done in init code of modal.module):
 * ctools_include('modal');
 * ctools_modal_add_js();
 */

/**
 * Implements hook_menu().
 */
function modal_node_edit_menu() {
  $items = array();
  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' => 'Create ' . $type->name . ' node',
      'title callback' => 'check_plain',
      'page callback' => 'modal_node_edit_page_add',
      'page arguments' => array(
        $type->type,
        4,
      ),
      'access callback' => TRUE,
      'delivery callback' => 'ajax_deliver',
      'type' => MENU_CALLBACK,
    );
  }
  $items['modal/node/%node/edit/%ctools_js'] = array(
    'title' => 'Edit node',
    'page callback' => 'modal_node_edit_page_edit',
    'page arguments' => array(
      2,
      4,
    ),
    'access callback' => TRUE,
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );
  $items['modal/node/%node/delete/%ctools_js'] = array(
    'title' => 'Delete node',
    'page callback' => 'modal_node_edit_page_delete',
    'page arguments' => array(
      2,
      4,
    ),
    'access callback' => TRUE,
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Page callback - modal: add node.
 */
function modal_node_edit_page_add($type, $js, $force_page_reload = TRUE) {

  // Fall back if $js is not set.
  if (!$js) {
    $parameters = drupal_get_query_parameters();
    unset($_GET['destination']);
    drupal_goto('node/add/' . str_replace('_', '-', $type), array(
      'query' => $parameters,
    ));
    return NULL;
  }
  $types = node_type_get_types();
  if (!isset($types[$type])) {
    $parameters = drupal_get_query_parameters();
    unset($_GET['destination']);
    drupal_goto('node/add/' . str_replace('_', '-', $type), array(
      'query' => $parameters,
    ));
    return NULL;
  }
  $type_name = $types[$type]->name;

  // Fix superglobals (such as $_GET) in order to make arg() work properly.
  modal_set_path_data('node/add/' . str_replace('_', '-', $type));
  ctools_include('modal');
  ctools_include('ajax');
  if (!node_access('create', $type)) {
    $commands = array(
      ctools_modal_command_display(t('Access denied'), t('You are not authorized to access this page.')),
    );
    $commands[] = ajax_command_invoke('#modalContent', 'addClass', array(
      'modal-denied-node-add modal-denied-node-add-' . $type,
    ));
    drupal_alter('modal_node_edit_page_add_access_denied', $commands, $type, $type_name);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  global $user;

  // Node object skeleton..
  $node = (object) array(
    'uid' => $user->uid,
    'name' => isset($user->name) ? $user->name : '',
    'type' => $type,
    'language' => LANGUAGE_NONE,
  );
  $title = t('Create @name', array(
    '@name' => $type_name,
  ));
  drupal_alter('modal_node_add_title', $title, $type, $type_name);
  return modal_node_edit_node_form($node, $title, $force_page_reload);
}

/**
 * Page callback - modal: edit node.
 */
function modal_node_edit_page_edit($node, $js, $force_page_reload = TRUE) {

  // Fall back if $js is not set.
  if (!$js) {
    $parameters = drupal_get_query_parameters();
    unset($_GET['destination']);
    drupal_goto('node/' . $node->nid . '/edit', array(
      'query' => $parameters,
    ));
    return NULL;
  }

  // Fix superglobals (such as $_GET) in order to make arg() work properly.
  modal_set_path_data('node/' . $node->nid . '/edit');
  ctools_include('modal');
  ctools_include('ajax');
  if (!node_access('update', $node)) {
    $commands = array(
      ctools_modal_command_display(t('Access denied'), t('You are not authorized to access this page.')),
    );
    $commands[] = ajax_command_invoke('#modalContent', 'addClass', array(
      'modal-denied-node-edit modal-denied-node-edit-' . $node->type,
    ));
    drupal_alter('modal_node_edit_page_edit_access_denied', $commands, $node);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  $type_name = node_type_get_name($node);
  $title = t('<em>Edit @type</em> @title', array(
    '@type' => $type_name,
    '@title' => $node->title,
  ));
  drupal_alter('modal_node_edit_title', $title, $node);
  return modal_node_edit_node_form($node, $title, $force_page_reload);
}

/**
 * Page callback - modal: delete node.
 */
function modal_node_edit_page_delete($node, $js, $force_page_reload = TRUE) {

  // Fall back if $js is not set.
  if (!$js) {
    $parameters = drupal_get_query_parameters();
    unset($_GET['destination']);
    drupal_goto('node/' . $node->nid . '/delete', array(
      'query' => $parameters,
    ));
    return NULL;
  }

  // Fix superglobals (such as $_GET) in order to make arg() work properly.
  modal_set_path_data('node/' . $node->nid . '/delete');
  ctools_include('modal');
  ctools_include('ajax');
  if (!node_access('delete', $node)) {
    $commands = array(
      ctools_modal_command_display(t('Access denied'), t('You are not authorized to access this page.')),
    );
    $commands[] = ajax_command_invoke('#modalContent', 'addClass', array(
      'modal-denied-node-delete modal-denied-node-delete-' . $node->type,
    ));
    drupal_alter('modal_node_edit_page_delete_access_denied', $commands, $node);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  $type_name = node_type_get_name($node);
  $title = t('<em>Delete @type</em> @title', array(
    '@type' => $type_name,
    '@title' => $node->title,
  ));
  drupal_alter('modal_node_delete_title', $title, $node);
  return modal_node_edit_node_form($node, $title, $force_page_reload, TRUE);
}

/**
 * Builds the Modal Form.
 *
 * @param object $node
 *   The node object in editing or deleting case. In adding 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.
 * @param boolean $deletion
 *   TRUE if the node deleting form must be shown instead of node editing form.
 */
function modal_node_edit_node_form($node, $title, $force_page_reload = FALSE, $deletion = FALSE) {
  ctools_include('node.pages', 'node', '');
  $form_state = array(
    'title' => $title,
    'ajax' => TRUE,
    // This property can be used in hook_form_alter() to separate modal and full-page forms.
    'modal_edit' => TRUE,
    'build_info' => array(
      'args' => array(
        $node,
      ),
    ),
  );
  if (isset($_GET['destination'])) {
    $form_state['modal_destination'] = $_GET['destination'];
    unset($_GET['destination']);
  }
  $form_wrapper = ctools_modal_form_wrapper(!$deletion ? $node->type . '_node_form' : 'node_delete_confirm', $form_state);
  if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) {
    $commands = array();
    $commands[] = ajax_command_invoke('#modalContent', 'addClass', array(
      (!$deletion ? 'modal-node-edit modal-node-edit-' : 'modal-node-delete modal-node-delete-') . $node->type,
    ));
    drupal_alter(!$deletion ? 'modal_node_edit' : 'modal_node_delete', $commands, $node);
    $commands = array_merge($form_wrapper, $commands);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }

  // Window is to be closed.
  ctools_add_js('ajax-responder');
  $commands = array();
  $commands[] = ctools_modal_command_dismiss();
  drupal_alter(!$deletion ? 'modal_node_edit_close' : 'modal_node_delete_close', $commands, $node);
  $message_commands = array();
  if (module_exists('modal_message')) {
    $message_commands = modal_message_get_as_ajax_commands($force_page_reload);
  }
  if (!empty($message_commands)) {
    $commands = array_merge($commands, $message_commands);
  }
  elseif ($force_page_reload) {
    if (isset($form_state['modal_destination'])) {
      $commands[] = ctools_ajax_command_redirect($form_state['modal_destination']);
    }
    else {
      $commands[] = ctools_ajax_command_reload();
    }
  }
  elseif (isset($form_state['modal_destination'])) {
    $commands[] = ctools_ajax_command_redirect($form_state['modal_destination']);
  }
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}

Functions

Namesort descending Description
modal_node_edit_menu Implements hook_menu().
modal_node_edit_node_form Builds the Modal Form.
modal_node_edit_page_add Page callback - modal: add node.
modal_node_edit_page_delete Page callback - modal: delete node.
modal_node_edit_page_edit Page callback - modal: edit node.