You are here

function flexiform_manage_form_fields_form in Flexiform 7

flexiform_manage_form_fields_form

This form configures the fields on an flexiform. You can choose them rearrange them and override verious instance settings through this form.

1 string reference to 'flexiform_manage_form_fields_form'
FlexiformUIController::hook_menu in ./flexiform.admin.inc
Overrides hook_menu() defaults.

File

./flexiform.admin.inc, line 765
Model type editing UI.

Code

function flexiform_manage_form_fields_form($form, &$form_state, $flexiform) {

  // Get list of entities
  $entities = $flexiform->entities;

  // Get the fields in the flexiform deff
  $elements = array();
  foreach ($flexiform->elements as $element_namespace => $settings) {
    $element = FlexiformElement::getElement($flexiform, $element_namespace);
    $elements[$element
      ->getElementNamespace()] = $element;
  }
  $form += array(
    '#flexiform' => $flexiform,
    '#elements' => array_keys($elements),
  );
  $form_state += array(
    '#flexiform' => $flexiform,
    '#elements' => $elements,
  );
  $table = array(
    '#type' => 'flexiform_field_table',
    '#tree' => TRUE,
    '#header' => array(
      t('Label'),
      t('Weight'),
      t('Parent'),
      t('Type'),
      t('Entity'),
      t('Name'),
      t('Widget'),
      t('Operations'),
    ),
    '#parent_options' => array(),
    '#regions' => array(
      'main' => array(
        'message' => t('No fields are present yet'),
      ),
      'add_new' => array(
        'title' => ' ',
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'field-ui-overview',
      ),
      'id' => 'field-overview',
    ),
  );
  foreach ($elements as $element_namespace => $element) {
    $element_path = 'admin/structure/flexiforms/manage/' . $flexiform->form . '/form-fields/' . $element_namespace;
    if ($element instanceof FlexiformElementField) {
      $widget = array(
        '#type' => 'link',
        '#title' => t($element
          ->getWidgetLabel()),
        '#href' => $element_path . '/widget-type',
        '#options' => array(
          'attributes' => array(
            'title' => t('Change widget type.'),
          ),
        ),
      );
    }
    else {
      $widget = array(
        '#type' => 'markup',
        '#markup' => '',
      );
    }
    $table[$element_namespace] = array(
      '#attributes' => array(
        'class' => array(
          'draggable',
          'tabledrag-leaf',
        ),
      ),
      '#row_type' => 'field',
      '#flexiform_element' => $element,
      '#region_callback' => 'flexiform_field_form_row_region',
      'label' => array(
        '#markup' => $element
          ->label(),
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#title' => t('Weight for @title', array(
          '@title' => $element
            ->label(),
        )),
        '#title_display' => 'invisible',
        '#default_value' => $element
          ->getWeight(),
        '#size' => 3,
        '#attributes' => array(
          'class' => array(
            'field-weight',
          ),
        ),
      ),
      'parent_wrapper' => array(
        'parent' => array(
          '#type' => 'select',
          '#title' => t('Parent for @title', array(
            '@title' => $element
              ->label(),
          )),
          '#title_display' => 'invisible',
          '#options' => $table['#parent_options'],
          '#empty_value' => '',
          '#attributes' => array(
            'class' => array(
              'field-parent',
            ),
          ),
          '#parents' => array(
            'fields',
            $element_namespace,
            'parent',
          ),
        ),
        'hidden_name' => array(
          '#type' => 'hidden',
          '#default_value' => $element_namespace,
          '#attributes' => array(
            'class' => array(
              'field-name',
            ),
          ),
        ),
      ),
      'type' => array(
        '#markup' => $element
          ->type(),
      ),
      'entity' => array(
        '#markup' => $element
          ->getEntityNamespace(),
      ),
      'field' => array(
        '#markup' => $element
          ->name(),
      ),
      'widget_type' => $widget,
      'operations' => array(
        '#theme' => 'item_list',
        '#items' => array(
          'configure' => l(t('configure'), $element_path, array(
            'attributes' => array(
              'title' => t('Configure settings for this form'),
            ),
          )),
          'remove' => l(t('remove'), $element_path . '/remove', array(
            'attributes' => array(
              'title' => t('Remove this element.'),
            ),
          )),
        ),
      ),
    );
  }

  // Additional row: add new field.
  $max_weight = flexiform_field_max_weight($flexiform);
  $entity_options = array();
  foreach ($entities as $entity_namespace => $info) {
    $entity_options[$entity_namespace] = $info['label'];
  }
  if (isset($form_state['values']['fields']['_add_element']['entity'])) {
    $element_entity_namespace = $form_state['values']['fields']['_add_element']['entity'];
  }
  else {
    $element_entity_namespace = NULL;
  }
  $element_options = flexiform_field_element_options($flexiform, $element_entity_namespace);
  if ($entity_options) {
    $name = '_add_element';
    $table[$name] = array(
      '#attributes' => array(
        'class' => array(
          'draggable',
          'tabledrag-leaf',
          'add-new',
        ),
      ),
      '#row_type' => 'add_field',
      '#region_callback' => 'flexiform_field_form_row_region',
      'label' => array(
        '#type' => 'textfield',
        '#title' => 'Lable',
        '#title_display' => 'invisible',
        '#size' => 15,
        '#description' => t('Label'),
        '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add element') . '</div>',
        '#suffix' => '</div>',
      ),
      'weight' => array(
        '#type' => 'textfield',
        '#default_value' => $max_weight + 1,
        '#size' => 3,
        '#title_display' => 'invisible',
        '#title' => t('Weight for element'),
        '#attributes' => array(
          'class' => array(
            'field-weight',
          ),
        ),
        '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
      ),
      'parent_wrapper' => array(
        'parent' => array(
          '#type' => 'select',
          '#title' => t('Parent for element'),
          '#title_display' => 'invisible',
          '#options' => $table['#parent_options'],
          '#empty_value' => '',
          '#attributes' => array(
            'class' => array(
              'field-parent',
            ),
          ),
          '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
          '#parents' => array(
            'fields',
            $name,
            'parent',
          ),
        ),
        'hidden_name' => array(
          '#type' => 'hidden',
          '#default_value' => $name,
          '#attributes' => array(
            'class' => array(
              'field-name',
            ),
          ),
        ),
      ),
      'type' => array(
        '#markup' => 'Field',
      ),
      'entity' => array(
        '#type' => 'select',
        '#title' => t('The Enitity'),
        '#title_display' => 'invisible',
        '#options' => $entity_options,
        '#empty_option' => t('- Select an Entity -'),
        '#description' => t('Entity from which to take the field'),
        '#attributes' => array(
          'class' => array(
            'entity-select',
          ),
        ),
        '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
        '#ajax' => array(
          'event' => 'change',
          'method' => 'replace',
          'wrapper' => 'flexiform-element-selector',
          'callback' => 'flexiform_manage_form_fields_elements',
        ),
      ),
      'field' => array(
        '#type' => 'select',
        '#title' => t('The Field'),
        '#title_display' => 'invisible',
        '#options' => $element_options,
        '#empty_option' => t('- Select an Element -'),
        '#description' => t('Pick an Element'),
        '#attributes' => array(
          'class' => array(
            'entity-select',
          ),
        ),
        '#prefix' => '<div id="flexiform-element-selector"><div class="add-new-placeholder">&nbsp;</div>',
        '#suffix' => '</div>',
      ),
      'widget' => array(),
      'operations' => array(),
    );
  }
  $form['fields'] = $table;
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#theme_wrappers' => array(
      'vertical_tabs',
    ),
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );
  $redirect_settings = !empty($flexiform->settings['redirect']) ? $flexiform->settings['redirect'] : array();
  $form['additional_settings']['redirect'] = array(
    '#type' => 'fieldset',
    '#title' => t('Redirect'),
    '#description' => t('Configure redirect options for this form. For ajax forms and modals see the ajax section.'),
  );
  $form['additional_settings']['redirect']['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Redirect Path'),
    '#description' => t('Where to redirect to when the form is submitted.'),
    '#default_value' => !empty($redirect_settings['path']) ? $redirect_settings['path'] : '',
  );
  $form['additional_settings']['redirect']['contexts'] = array(
    '#title' => t('Substitutions'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['additional_settings']['redirect']['contexts']['contexts'] = $flexiform
    ->getBuilder()
    ->getCtoolsSubstitutionsList();
  $buttons_settings = !empty($flexiform->settings['buttons']) ? $flexiform->settings['buttons'] : array();
  $form['additional_settings']['buttons'] = array(
    '#type' => 'fieldset',
    '#title' => t('Buttons'),
    '#description' => t('Configure the buttons that appear on this form.'),
  );
  $form['additional_settings']['buttons']['submit_text'] = array(
    '#title' => t('Submit Button Text'),
    '#description' => t('What the submit button should say.'),
    '#type' => 'textfield',
    '#default_value' => !empty($buttons_settings['submit_text']) ? $buttons_settings['submit_text'] : t('Save'),
  );
  $ajax_settings = !empty($flexiform->settings['ajax']) ? $flexiform->settings['ajax'] : array();
  $form['additional_settings']['ajax'] = array(
    '#type' => 'fieldset',
    '#title' => t('AJAX'),
    '#description' => t('Configure settings for submitting a form with ajax.'),
  );
  $form['additional_settings']['ajax']['submit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Submit with AJAX'),
    '#description' => t('Submit this form without reloading the page.'),
    '#default_value' => !empty($ajax_settings['submit']),
  );
  $form['additional_settings']['ajax']['response'] = array(
    '#type' => 'select',
    '#title' => t('Response'),
    '#description' => t('What should happen after the form has been submitted?'),
    '#options' => array(
      'refresh' => t('Refresh Form'),
      'reload' => t('Reload Page'),
      'redirect' => t('Redirect'),
    ),
    '#default_value' => !empty($ajax_settings['response']) ? $ajax_settings['response'] : 'refresh',
    '#states' => array(
      'visible' => array(
        ':input[name="additional_settings[ajax][submit]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['additional_settings']['ajax']['redirect'] = array(
    '#type' => 'textfield',
    '#title' => t('Redirect Path'),
    '#description' => t('The path to redirect to.'),
    '#default_value' => !empty($ajax_settings['redirect']) ? $ajax_settings['redirect'] : '',
    '#states' => array(
      'visible' => array(
        ':input[name="additional_settings[ajax][submit]"]' => array(
          'checked' => TRUE,
        ),
        ':input[name="additional_settings[ajax][response]"]' => array(
          'value' => 'redirect',
        ),
      ),
    ),
  );
  if (module_exists('honeypot')) {
    $honeypot_settings = !empty($flexiform->settings['honeypot']) ? $flexiform->settings['honeypot'] : array();
    $form['additional_settings']['honeypot'] = array(
      '#type' => 'fieldset',
      '#title' => t('Honeypot'),
      '#description' => t('Configure settings for honeypot form protection. There are two forms of protection; a honeypot text field and a time limit.'),
    );
    $form['additional_settings']['honeypot']['honeypot'] = array(
      '#type' => 'checkbox',
      '#title' => t('Protect with Honeypot Text Field'),
      '#default_value' => !empty($honeypot_settings['honeypot']),
    );
    $form['additional_settings']['honeypot']['time_restriction'] = array(
      '#type' => 'checkbox',
      '#title' => t('Protext with Time Restriction'),
      '#default_value' => !empty($honeypot_settings['time_restriction']),
    );
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['#attached']['css'][] = drupal_get_path('module', 'flexiform') . '/flexiform_ui.css';
  $form['#attached']['js'][] = drupal_get_path('module', 'flexiform') . '/flexiform_ui.js';
  $form['#attached']['js'][] = 'misc/form.js';
  $form['#attached']['js'][] = 'misc/collapse.js';

  // Add tabledrag behavior.
  $form['#attached']['drupal_add_tabledrag'][] = array(
    'field-overview',
    'order',
    'sibling',
    'field-weight',
  );
  $form['#attached']['drupal_add_tabledrag'][] = array(
    'field-overview',
    'match',
    'parent',
    'field-parent',
    'field-parent',
    'field-name',
  );
  return $form;
}