You are here

function acquia_lift_element_variation_details_form in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 acquia_lift.admin.unibar.inc \acquia_lift_element_variation_details_form()

Form handler for an element variation details form.

Parameters

string $variation_type: The variation type that defines the type of details to include. This is a key defined as a personalize_elements variation.

string $osid: (Optional) The option set id when adding variations to an existing option set. If null, then create a new option set with the control and variation.

string $option_id: (Optional) The option id for the specific variation to be edited.

See also

hook_personalize_elements_variation_types

1 string reference to 'acquia_lift_element_variation_details_form'
acquia_lift_menu in ./acquia_lift.module
Implements hook_menu().

File

./acquia_lift.admin.unibar.inc, line 29
acquia_lift.admin.unibar.inc

Code

function acquia_lift_element_variation_details_form($form, &$form_state, $variation_type, $osid = NULL, $option_id = NULL) {
  module_load_include('inc', 'personalize_elements', 'personalize_elements.admin');
  $types = module_invoke_all('personalize_elements_variation_types');
  if (!isset($types[$variation_type]['contextual']['formitem'])) {
    $form['error'] = array(
      '#markup' => t('Invalid element variation type. Please close this message and create a new variation.'),
    );
    return $form;
  }
  $default_option_label = '';
  if (!empty($osid)) {
    $osid = personalize_resolve_stringified_osid($osid);
    $option_set = personalize_option_set_load($osid);
    $default_set_name = $option_set->label;
    $edit_option = NULL;
    if (!empty($option_id)) {
      if ($option_id === PERSONALIZE_CONTROL_OPTION_ID) {
        $form['error'] = array(
          '#markup' => t('The control variation cannot be edited.'),
        );
        return $form;
      }
      foreach ($option_set->options as $option) {
        if ($option['option_id'] === $option_id) {
          $edit_option = $option;
          $form['option'] = array(
            '#type' => 'value',
            '#value' => $edit_option,
          );
          break;
        }
      }
    }
    $default_option_label = !empty($edit_option['option_label']) ? $edit_option['option_label'] : '';
    $form['osid'] = array(
      '#type' => 'value',
      '#value' => $osid,
    );
  }
  $form['variation_type'] = array(
    '#type' => 'value',
    '#value' => $variation_type,
  );

  // These form item will be populated by the front-end code.
  $form['selector'] = array(
    '#type' => 'textfield',
    '#title' => t('Selector'),
    '#required' => TRUE,
    '#maxlength' => 500,
    '#access' => empty($osid),
  );
  $form['pages'] = array(
    '#type' => 'hidden',
  );
  $form['agent'] = array(
    '#type' => 'hidden',
  );

  // Option set title applicable to element variations.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Variation set name'),
    '#description' => t('Descriptive name for the variation set, used by Acquia Lift for administration and reporting.'),
    '#default_value' => isset($default_set_name) ? $default_set_name : '',
    '#required' => TRUE,
    '#maxlength' => 255,
    '#attributes' => array(
      'placeholder' => t('Example: Front page offers'),
    ),
  );

  // Option label
  $form['option_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Variation name'),
    '#required' => TRUE,
    '#maxlength' => PERSONALIZE_MACHINE_NAME_MAXLENGTH,
    '#default_value' => personalize_sanitize_string($default_option_label),
    '#attributes' => array(
      'placeholder' => t('Example: Winter vacation'),
    ),
  );

  // Now include the details specific to this variation type.
  $form['personalize_elements_content'] = $types[$variation_type]['contextual']['formitem'];
  if (isset($edit_option)) {
    $form['personalize_elements_content']['#default_value'] = personalize_elements_filter_personalize_element_option($variation_type, $option['personalize_elements_content']);
    $form['variation_number']['#type'] = 'value';
    $form['variation_number']['#value'] = $edit_option['option_id'];
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );

  // Form actions.
  // TRICKY: These must have ids that are different from our CTools loaded
  // modal forms or else their submit buttons will be processed via Drupal ajax
  // rather than the main URL callbacks leading to a 200 response with an
  // empty response body.
  $form['actions']['variation_type_submit_form_add'] = array(
    '#type' => 'submit',
    '#value' => t('Add another'),
    '#attributes' => array(
      'class' => array(
        'action-item-primary-active',
        'acquia-lift-submit-button',
      ),
    ),
    '#ajax' => array(
      'callback' => 'acquia_lift_element_variation_details_form_ajax_callback',
      'wrapper' => 'acquia-lift-element-variation-details-form',
      'progress' => array(
        'message' => NULL,
        'type' => 'throbber',
      ),
    ),
  );
  $form['actions']['variation_type_submit_form'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#attributes' => array(
      'class' => array(
        'acquia-lift-submit-button',
      ),
    ),
    '#ajax' => array(
      'callback' => 'acquia_lift_element_variation_details_form_ajax_callback',
      'wrapper' => 'acquia-lift-element-variation-details-form',
      'progress' => array(
        'message' => NULL,
        'type' => 'throbber',
      ),
    ),
  );

  // Add a cancel button for AJAX-loaded forms.
  $form['actions']['variation_type_cancel_form'] = array(
    '#type' => 'button',
    '#button_type' => 'cancel',
    '#value' => t('Cancel'),
    '#attributes' => array(
      'class' => array(
        'acquia-lift-cancel-button',
      ),
    ),
    '#ajax' => array(
      'callback' => 'acquia_lift_element_variation_details_form_ajax_callback',
      'wrapper' => 'acquia-lift-element-variation-details-form',
      'progress' => array(
        'message' => NULL,
        'type' => 'throbber',
      ),
    ),
    // Turn off validation.
    '#limit_validation_errors' => array(),
    '#submit' => array(),
  );
  return $form;
}