You are here

function ajax_form_entity_form_alter in Ajax form entity 7.x

Same name and namespace in other branches
  1. 8 ajax_form_entity.module \ajax_form_entity_form_alter()
  2. 7 ajax_form_entity.module \ajax_form_entity_form_alter()

Form builder; the entity wall message add form.

File

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

Code

function ajax_form_entity_form_alter(&$form, $form_state) {

  // Check if we have an entityform (but no administration form).
  // Do not use $form['#id'] because it may change.
  // TODO : improve detection of admin forms.
  if (isset($form['#entity_type']) && isset($form['#bundle']) && !(arg(0) == 'admin')) {
    $settings = variable_get('ajax_form_entity_' . $form['#entity_type'] . '_' . $form['#bundle'], array());
    if (isset($settings['activate']) && $settings['activate']) {

      // Adds containers for messages & entity view, and form reload wrapper (as form ID in $form may be different from the HTML ID whith AJAX issues).
      if (!isset($form['#prefix']) || !strpos($form['#prefix'], 'form-message-wrapper') && $settings['view_mode_region'] != 0 && $settings['view_mode_region'] != 'id') {
        $form['#prefix'] = '<div id="form-message-wrapper-' . $form['#build_id'] . '"></div><div id="preview-wrapper-top-' . $form['#build_id'] . '"></div><div id="form-reload-' . $form['#build_id'] . '">';
        $form['#suffix'] = '</div><div id="preview-wrapper-bottom-' . $form['#build_id'] . '"></div>';
      }

      // Indicate if it is a creation or modification for the AJAX callback.
      // Special case for Field collection entity  (no ID in the form).
      $id = $settings['id'];

      // Check if it is the first form (there are some settings if it is).
      if (!isset($form_state['build_info']['args'][0]->{$id}) || !$form_state['build_info']['args'][0]->{$id}) {
        $form['new'] = array(
          '#type' => 'value',
          '#value' => TRUE,
        );
      }

      // Compatibility with field collection if on the field collection adding page.
      // TODO : mettre dans ajax_form_entity_field_collection.
      if ($form['#entity_type'] == 'field_collection_item') {
        global $_ajax_form_entity_field_collection;
        if (arg(0) == 'field-collection') {
          $host_entity_type = arg(3);
          $host_entity_id = arg(4);
        }
        elseif ($_ajax_form_entity_field_collection['host_entity_id']) {
          $host_entity_type = $_ajax_form_entity_field_collection['host_entity_type'];
          $host_entity_id = $_ajax_form_entity_field_collection['host_entity_id'];
        }
        $form['host_entity_type'] = array(
          '#type' => 'value',
          '#value' => $host_entity_type,
        );
        $form['host_entity_id'] = array(
          '#type' => 'value',
          '#value' => $host_entity_id,
        );
        $form['#attributes']['id'][] = $form['form_build_id']['#value'];
        if (isset($form['new'])) {

          //$new_form_build['#suffix'] .= l(t('Cancel'), 'ajax-form-entity-collections-cancel/nojs/' . $form_build_id, array('attributes' => array('class' => array('use-ajax button-cancel'))));
          if (arg(1) != 'ajax') {
            $form['#attributes']['style'][] = 'display:none';

            // Add cancel button to close the form
            $form['#suffix'] .= l(t('Cancel'), 'ajax-form-entity-field-collection-cancel/nojs/' . $form['form_build_id']['#value'], array(
              'attributes' => array(
                'class' => array(
                  'use-ajax button-collection-cancel cancel-' . $form['form_build_id']['#value'],
                ),
                'style' => array(
                  'display:none',
                ),
              ),
            ));
            $form['#prefix'] .= l(t('Add an item'), 'javascript:void(0)', array(
              'attributes' => array(
                'id' => 'open-form' . $form['form_build_id']['#value'],
                'class' => 'open-form ' . $form['form_build_id']['#value'],
              ),
              'fragment' => 'refresh',
              'external' => true,
            ));
          }
          else {

            // Add cancel button to close the form
            $form['#suffix'] .= l(t('Cancel'), 'ajax-form-entity-field-collection-cancel/nojs/' . $form['form_build_id']['#value'], array(
              'attributes' => array(
                'class' => array(
                  'use-ajax button-collection-cancel cancel-' . $form['form_build_id']['#value'],
                ),
              ),
            ));
            $form['#prefix'] .= l(t('Add an item'), 'javascript:void(0)', array(
              'attributes' => array(
                'id' => 'open-form' . $form['form_build_id']['#value'],
                'class' => 'open-form ' . $form['form_build_id']['#value'],
              ),
              'fragment' => 'refresh',
              'external' => true,
              'style' => 'display:none',
            ));
          }
        }
      }

      // Add ajax callback to the submit button.
      $form['actions']['submit']['#ajax'] = array(
        'callback' => 'ajax_form_entity_callback',
        'effect' => 'fade',
      );

      // Load js to autosubmit files (else they are not loaded).
      drupal_add_js(drupal_get_path('module', 'ajax_form_entity') . '/ajax_form_entity.js');
    }
  }
}