You are here

modal_entityform.module in Modal operations 7

Allows entityform submitting and editing in modal window.

@example Create links with the following href and class attributes (SUBMISSION_ID is the identifier of entityform submission to edit, FORM_TYPE is the type of the entityform to submit): <a href="/modal/entityform/FORM_TYPE/nojs" class="ctools-use-modal">Submit entityform and reload the page</a> or <a href="/modal/entityform/FORM_TYPE/nojs/0" class="ctools-use-modal">Submit entityform without page reloading</a>. or <a href="/modal/entityform/SUBMISSION_ID/edit/nojs" class="ctools-use-modal">Edit entityform submission and reload the page</a> or <a href="/modal/entityform/SUBMISSION_ID/edit/nojs/0" class="ctools-use-modal">Edit entityform submission 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_entityform/modal_entityform.module
View source
<?php

/**
 * @file
 * Allows entityform submitting and editing in modal window.
 *
 * @example
 * Create links with the following href and class attributes
 *   (SUBMISSION_ID is the identifier of entityform submission to edit, FORM_TYPE is the type of the entityform to submit):
 * <a href="/modal/entityform/FORM_TYPE/nojs" class="ctools-use-modal">Submit entityform and reload the page</a>
 * or
 * <a href="/modal/entityform/FORM_TYPE/nojs/0" class="ctools-use-modal">Submit entityform without page reloading</a>.
 * or
 * <a href="/modal/entityform/SUBMISSION_ID/edit/nojs" class="ctools-use-modal">Edit entityform submission and reload the page</a>
 * or
 * <a href="/modal/entityform/SUBMISSION_ID/edit/nojs/0" class="ctools-use-modal">Edit entityform submission 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_entityform_menu() {
  $items['modal/entityform/%entityform_empty/%ctools_js'] = array(
    'title' => 'Submit',
    'page callback' => 'modal_entityform_page_submit',
    'page arguments' => array(
      2,
      3,
    ),
    'access callback' => TRUE,
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );
  $items['modal/entityform/%entityform/edit/%ctools_js'] = array(
    'title' => 'Edit',
    'page callback' => 'modal_entityform_page_edit',
    'page arguments' => array(
      2,
      4,
    ),
    'access callback' => TRUE,
    'delivery callback' => 'ajax_deliver',
    'type' => MENU_CALLBACK,
  );
  return $items;
}
function modal_entityform_access($op, $entityform) {
  if (!entityform_access($op, $entityform)) {
    return FALSE;
  }

  // Check entityform access rules.
  // Entityform 7.x-1.x.
  if (function_exists('entityform_invoke_rules')) {
    return _entityform_rules_all_pass(entityform_invoke_rules($entityform, 'access_rules'));
  }

  // Entityform 7.x-2.x.
  if (function_exists('_entity_rules_invoke_rules')) {
    return _entity_rules_all_pass(_entity_rules_invoke_rules($entityform, 'entityform', 'form_access'));
  }

  // Allow operations for unknown version of Entityform module.
  return TRUE;
}

/**
 * Modal Entityform add page callback.
 */
function modal_entityform_page_submit($entityform, $js, $force_page_reload = TRUE) {

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

  // Fix superglobals (such as $_GET) in order to make arg() work properly.
  modal_set_path_data('eform/submit/' . $entityform->type);
  ctools_include('modal');
  ctools_include('ajax');
  $entityform_type_entity = entityform_type_load($entityform->type);
  if (!modal_entityform_access('submit', $entityform)) {
    $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-entityform modal-denied-entityform-' . $entityform->type,
    ));
    drupal_alter('modal_entityform_submit_access_denied', $commands, $entityform_type_entity, $entityform);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  $title = $entityform_type_entity->label;
  drupal_alter('modal_entityform_submit_title', $title, $entityform_type_entity, $entityform);
  return modal_entityform_form($entityform, $title, $entityform_type_entity, $force_page_reload);
}

/**
 * Modal Entityform edit page callback.
 */
function modal_entityform_page_edit($entityform, $js, $force_page_reload = TRUE) {

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

  // Fix superglobals (such as $_GET) in order to make arg() work properly.
  modal_set_path_data('entityform/' . $entityform->entityform_id . '/edit');
  ctools_include('modal');
  ctools_include('ajax');
  $entityform_type_entity = entityform_type_load($entityform->type);
  if (!modal_entityform_access('edit', $entityform)) {
    $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-entityform-edit modal-denied-entityform-edit-' . $entityform->type,
    ));
    drupal_alter('modal_entityform_edit_access_denied', $commands, $entityform_type_entity, $entityform);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  $title = $entityform_type_entity->label;
  drupal_alter('modal_entityform_edit_title', $title, $entityform_type_entity, $entityform);
  return modal_entityform_form($entityform, $title, $entityform_type_entity, $force_page_reload);
}

/**
 * Build the Modal Entityform Form.
 *
 * @param string $entityform
 *   Entityform type machine name.
 * @param string $title
 *   The Modal Popup Title.
 * @param string $entityform_type_entity
 *   Entityform type entity.
 * @param boolean $force_page_reload
 *   Indicates if page should be reloaded after form submission.
 */
function modal_entityform_form($entityform, $title, $entityform_type_entity, $force_page_reload = FALSE) {
  ctools_include('entityform.admin', 'entityform', '');
  $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(
        $entityform,
        'submit',
      ),
    ),
  );
  if (isset($_GET['destination'])) {
    $form_state['modal_destination'] = $_GET['destination'];
    unset($_GET['destination']);
  }
  $form_wrapper = ctools_modal_form_wrapper('entityform_edit_form', $form_state);
  if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) {
    $commands = array();
    $commands[] = ajax_command_invoke('#modalContent', 'addClass', array(
      'modal-entityform modal-entityform-' . $entityform->type,
    ));
    drupal_alter('modal_entityform_edit', $commands, $entityform_type_entity, $entityform);
    $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('modal_entityform_edit_close', $commands, $entityform_type_entity, $entityform);
  $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_entityform_access
modal_entityform_form Build the Modal Entityform Form.
modal_entityform_menu Implements hook_menu().
modal_entityform_page_edit Modal Entityform edit page callback.
modal_entityform_page_submit Modal Entityform add page callback.