You are here

function formassembly_form in FormAssembly 7

Form definition for editing a fa_form entity.

1 string reference to 'formassembly_form'
formassembly_menu in ./formassembly.module
Implements hook_menu().

File

./formassembly.module, line 397
Contains hooks implementations and callbacks to non-admin pages.

Code

function formassembly_form($form, &$form_state, $fa_form = NULL) {

  // attach fa_query_param field
  field_attach_form('fa_form', $fa_form, $form, $form_state);
  $form['fa_query_params']['#weight'] = 10;

  // Baseline token replacement functionality is available in core, but display
  // the token insert list from the token module if installed.
  if (module_exists('token')) {
    $form['token_tree'] = array(
      '#type' => 'fieldset',
      '#title' => t('Replacement tokens'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#theme' => 'token_tree',
    );
  }
  else {
    $form['token_tree'] = array(
      '#markup' => '<p>' . t('Enable the <a href="@drupal-token">Token module</a> to view the available token browser.', array(
        '@drupal-token' => 'http://drupal.org/project/token',
      )) . '</p>',
    );
  }
  $form['token_tree']['#weight'] = 20;
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 50,
  );
  $form['fa_form'] = array(
    '#type' => 'value',
    '#value' => $fa_form,
    '#weight' => 70,
  );

  // get the form html
  $form_array = formassembly_view($fa_form, 'markup');

  //there should only be one form
  $form_array = array_pop($form_array['fa_form']);
  $form_html = $form_array['fa_markup']['#markup'];
  $rows = array();
  $header = array(
    t('Element ID'),
    t('Associated Label'),
    t('HTML tag'),
    t('Attribute: type'),
    t('Attribute: value'),
  );
  $dom = new DOMDocument();
  libxml_use_internal_errors(TRUE);
  if (!$dom
    ->loadHTML($form_html)) {
    foreach (libxml_get_errors() as $error) {
      watchdog('FormAssembly', 'Form HTML failed to load on edit tab. LibXML error message is: ' . $error->message, WATCHDOG_ERROR, NULL);
    }
    libxml_clear_errors();
  }
  $label_elements = $dom
    ->getElementsByTagName('label');
  foreach ($label_elements as $label) {

    //FormAssembly label ids are of the form tfa_N-L where tfa_N is the id of the element
    $associated_id = rtrim($label
      ->getAttribute('id'), '-L');
    $associated_element = $dom
      ->getElementById($associated_id);
    $rows[]['data'] = array(
      check_plain($associated_id),
      check_plain($label->nodeValue),
      check_plain($associated_element->tagName),
      check_plain($associated_element
        ->getAttribute('type')),
      check_plain($associated_element
        ->getAttribute('value')),
    );
  }
  $form['structure'] = array(
    '#type' => 'item',
    '#markup' => theme('table', array(
      'header' => $header,
      'rows' => $rows,
    )),
    '#weight' => 80,
  );

  // format serialized query parameters array as "key|value" string for editing
  $value =& $form['fa_query_params'][LANGUAGE_NONE][0]['value']['#default_value'];
  if ($value) {
    $value = list_allowed_values_string(unserialize($value));
  }
  return $form;
}