You are here

function ajax_form_entity_callback in Ajax form entity 7.x

Same name and namespace in other branches
  1. 7 ajax_form_entity.module \ajax_form_entity_callback()

AJAX submit handler for entity message add form. Returns ajax commands to update the relevant message comments.

Return value

ajax commands (append to messages wrapper)

1 string reference to 'ajax_form_entity_callback'
ajax_form_entity_form_alter in ./ajax_form_entity.module
Form builder; the entity wall message add form.

File

./ajax_form_entity.module, line 185
Ajaxify entity forms.

Code

function ajax_form_entity_callback($form, $form_state) {
  $commands = array();

  // Return just error messages if there is an error.
  if ($errors = form_get_errors()) {

    // Change classes of the forms to display them as error.
    foreach ($errors as $name => $message) {
      $commands[] = ajax_command_invoke('#edit-' . str_replace(array(
        '_',
        '][',
      ), '-', $name), 'addClass', array(
        'error',
      ));
    }
    $commands[] = ajax_command_replace('#form-message-wrapper-' . $form['#build_id'], theme('status_messages'));
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $settings = variable_get('ajax_form_entity_' . $entity_type . '_' . $bundle, NULL);

  // Display result of form submission.
  if ($settings['view_mode_region']) {

    // Entity should be loaded again to handle processing certain elements (images for example).
    $ids = array();
    $entities = array();
    $ids[] = $form_state[$entity_type]->{$settings['id']};
    $entities[] = entity_load($form['#entity_type'], $ids, $conditions = array(), FALSE);
    $entities_view = entity_view($form['#entity_type'], $entities[0], $settings['view_mode'], NULL, TRUE);
    $entity_show = drupal_render($entities_view);

    // View entity at the top.
    if ($settings['view_mode_region'] == 'top') {
      $id = 'preview-wrapper-top-' . $form['#build_id'];
      $commands[] = ajax_command_append('#' . $id, $entity_show);
    }
    elseif ($settings['view_mode_region'] == 'id') {
      $id = str_replace('_', '-', $entity_type . '-' . $bundle);
      $commands[] = ajax_command_prepend('#' . $id, $entity_show);
    }
    else {
      $id = 'preview-wrapper-bottom-' . $form['#build_id'];
      $commands[] = ajax_command_prepend('#' . $id, $entity_show);
    }
  }

  // Return confirmation messages if any or empty session.
  if ($settings['message']) {
    $commands[] = ajax_command_replace('#form-message-wrapper-' . $form['#build_id'], theme('status_messages'));
  }
  else {
    unset($_SESSION['messages']['status']);
  }

  // Case of creation : respect the settings and reload the form.
  if (isset($form['new']['#value']) && $settings['reload']) {

    // If field collection ,delete former Cancel link of the former form.
    if ($entity_type == 'field_collection_item') {
      $commands[] = ajax_command_remove('a.cancel-' . $form['form_build_id']['#value']);
    }

    // Rebuilds the form.
    $form_state['reloaded'] = 1;
    $new_form_build = _ajax_form_entity_build_entity_forms($entity_type, $form, $form_state);
    $commands[] = ajax_command_replace('#form-reload-' . $form['#build_id'], drupal_render($new_form_build));

    //$new_link = l(t('Fermer'), 'javascript:void(0)', array('attributes' => array('id' => 'open-form' . $new_form_build['form_build_id']['#value'], 'class' => 'open-form ' . $new_form_build['form_build_id']['#value']), 'fragment' => 'refresh', 'external'=>true));
    $commands[] = ajax_command_css('#open-form' . $new_form_build['#build_id'], array(
      'display' => 'none',
    ));
  }
  else {
    $commands[] = ajax_command_remove('#form-reload-' . $form['#build_id']);
  }
  $id = str_replace('_', '-', $entity_type . '-' . $bundle);
  $commands[] = ajax_command_css('.remove-' . $id, array(
    'display' => 'none',
  ));
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}