You are here

function flexiform_form in Flexiform 7

Generates the model type editing form.

1 string reference to 'flexiform_form'
flexiform_ui_get_clone_form in ./flexiform.admin.inc
Clone form page callback.

File

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

Code

function flexiform_form($form, &$form_state, $flexiform, $op = 'edit') {
  $form_state['op'] = $op;
  $form['#flexiform'] = $flexiform;
  if ($op == 'clone') {
    $form['#cloned_from'] = $flexiform->cloned_from;
    $flexiform->label .= ' (cloned)';
    $flexiform->form = '';
  }
  $form['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#default_value' => $flexiform->label,
    '#description' => t('The human-readable name of this flexiform.'),
    '#required' => TRUE,
    '#size' => 30,
  );

  // Machine-readable type name.
  $form['form'] = array(
    '#type' => 'machine_name',
    '#default_value' => isset($flexiform->form) ? $flexiform->form : '',
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'flexiform_get_flexiforms',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for this flexiform. It must only contain lowercase letters, numbers, and underscores.'),
    '#disabled' => empty($flexiform->is_new),
  );
  $entity_info = entity_get_info();
  $entity_type_options = array();
  foreach ($entity_info as $type => $info) {
    $entity_type_options[$type] = $info['label'];
  }
  $form['base_entity'] = array(
    '#type' => 'select',
    '#title' => t('Base Entity'),
    '#description' => t('The base entity type for this form. Other entities can be added based on relationships to this entity.'),
    '#options' => $entity_type_options,
    '#empty_option' => t('- Select an Entity Type -'),
    '#default_value' => !empty($flexiform->base_entity) ? $flexiform->base_entity : FALSE,
    '#required' => TRUE,
    '#ajax' => array(
      'event' => 'change',
      'method' => 'replace',
      'wrapper' => 'flexiform-base-entity-bundle-selector',
      'callback' => 'flexiform_form_base_entity_bundle_selector',
    ),
    '#disabled' => empty($flexiform->is_new) || !empty($form_state['fix_entity_bundle']) || $op == 'clone',
  );
  if (isset($form_state['values']['base_entity'])) {
    $entity_type = $form_state['values']['base_entity'];
  }
  else {
    if (!empty($flexiform->base_entity)) {
      $entity_type = $flexiform->base_entity;
    }
    else {
      $entity_type = NULL;
    }
  }
  $bundle_options = array();
  if ($entity_type) {
    $entity_info = entity_get_info($entity_type);
    foreach ($entity_info['bundles'] as $bundle => $info) {
      $bundle_options[$bundle] = $info['label'];
    }
  }
  $default_bundle = FALSE;
  if (!empty($flexiform->base_entity) && !empty($flexiform->base_entity_bundle) && $flexiform->base_entity == $entity_type) {
    $default_bundle = $flexiform->base_entity_bundle;
  }
  $form['base_entity_bundle'] = array(
    '#type' => 'select',
    '#title' => t('Base Entity Bundle'),
    '#description' => t('The base entity bundle for this form.'),
    '#options' => $bundle_options,
    '#empty_option' => t('- Select a Bundle -'),
    '#default_value' => $default_bundle,
    '#required' => TRUE,
    '#prefix' => '<div id="flexiform-base-entity-bundle-selector">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'event' => 'change',
      'method' => 'replace',
      'wrapper' => 'flexiform-builder-selector',
      'callback' => 'flexiform_form_builder_selector',
    ),
    '#disabled' => empty($flexiform->is_new) || !empty($form_state['fix_entity_bundle']) || $op == 'clone',
  );
  if (isset($form_state['values']['base_entity_bundle'])) {
    $bundle = $form_state['values']['base_entity_bundle'];
  }
  else {
    if (!empty($flexiform->base_entity_bundle)) {
      $bundle = $flexiform->base_entity_bundle;
    }
    else {
      $bundle = NULL;
    }
  }
  $builder_options = array();
  $builder_description = '';
  if ($bundle && $entity_type) {
    $builder_info = flexiform_entity_type_get_builders($entity_type);
    foreach ($builder_info as $builder => $info) {
      $builder_options[$builder] = $info['label'];
      $builder_description .= t('%label: @desc<br />', array(
        '%label' => $info['label'],
        '@desc' => $info['description'],
      ));
    }
  }
  $default_builder = FALSE;
  $default_builder_desc = '';
  if (!empty($flexiform->base_entity) && !empty($flexiform->builder) && $flexiform->base_entity == $entity_type) {
    $default_builder = $flexiform->builder;
  }
  $form['builder'] = array(
    '#type' => 'select',
    '#title' => t('Builder'),
    '#description' => t('The builder to use to render this form.<br />!builder_desc', array(
      '!builder_desc' => $builder_description,
    )),
    '#options' => $builder_options,
    '#empty_option' => t('- Select a Builder -'),
    '#default_value' => $default_builder,
    '#required' => TRUE,
    '#prefix' => '<div id="flexiform-builder-selector">',
    '#suffix' => '</div>',
    '#disabled' => $op == 'clone',
  );
  $form['form_group'] = array(
    '#title' => t('Group'),
    '#description' => t('Select a group for this flexiform. Some groups do special things with forms.'),
  );
  $groups = flexiform_get_groups();
  if (!empty($flexiform->is_new) && count($groups) > 1) {
    $form['form_group']['#type'] = 'select';
    $form['form_group']['#default_value'] = isset($flexiform->form_group) ? $flexiform->form_group : FALSE;
    $form['form_group']['#required'] = TRUE;
    foreach ($groups as $group => $info) {
      $form['form_group']['#options'][$group] = $info['label'];
    }
  }
  else {
    $group = !empty($flexiform->form_group) ? $flexiform->form_group : key($groups);
    $form['form_group_display'] = $form['form_group'];
    $form['form_group_display']['#type'] = 'item';
    $form['form_group_display']['#markup'] = $groups[$group]['label'];
    $form['form_group']['#type'] = 'value';
    $form['form_group']['#value'] = $group;
  }

  // Add tags element
  $form['tags'] = array(
    '#type' => 'textfield',
    '#title' => t('Tags'),
    '#default_value' => isset($flexiform->tags) ? drupal_implode_tags($flexiform->tags) : '',
    '#autocomplete_path' => 'admin/structure/flexiforms/autocomplete_tags',
    '#description' => t('Tags associated with this flexible form. Separate multiple tags with commas.'),
  );

  // Displays Section of the form.
  $form['displays'] = array(
    '#type' => 'fieldset',
    '#title' => t('Displays'),
    '#description' => t('Configure places this form appears on your site.'),
    '#tree' => TRUE,
  );
  foreach (flexiform_display_info() as $display => $info) {
    $controller = $flexiform
      ->getDisplay($display);
    if (get_class($controller) == 'FlexiformDisplayNull') {
      $controller = $flexiform
        ->createDisplay($display);
    }
    $displayForm = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#title' => $info['label'],
      '#collapsed' => !$controller
        ->isEnabled(),
      '#description' => $info['description'],
    );
    $form['displays'][$display] = $controller
      ->configForm($displayForm, $form_state);
  }
  $form['access'] = array(
    '#type' => 'fieldset',
    '#title' => t('Access Control'),
    '#description' => t('Configure access control for this flexiform.'),
  );
  if (!empty($flexiform->is_new)) {
    $form['access']['message']['#markup'] = t('You must save the flexiform before configuring access control');
  }
  else {
    ctools_include('context');
    ctools_include('context-access-admin');
    $form_state['contexts'] = $flexiform
      ->getAccessController()
      ->prepareContexts();
    $form_state['access'] = $flexiform
      ->getAccessController()
      ->getCtoolsSettings();
    $form_state['no buttons'] = TRUE;
    $form_state['module'] = 'flexiform';
    $form_state['callback argument'] = $flexiform->form;
    $form['access'] += ctools_access_admin_form($form['access'], $form_state);
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 40,
  );
  if ($op == 'add') {
    $form['actions']['submit_entity'] = array(
      '#type' => 'submit',
      '#value' => t('Save & Add Entities'),
      '#weight' => 41,
      '#submit' => array(
        'flexiform_form_submit',
        'flexiform_form_submit_entity_redirect',
      ),
    );
  }

  // Submit Rule Actions
  // @todo: Remove dependency on rules admin by implement our own form for
  //        adding submit rules.
  if (module_exists('rules_admin') && !empty($flexiform->form)) {
    $form['rules']['#type'] = 'fieldset';
    $form['rules']['#title'] = t('Submit Actions');
    $form['rules']['#collapsible'] = TRUE;
    $form['rules']['#collapsed'] = TRUE;
    $form['rules']['title']['#markup'] = '<h3>' . t('Submit Actions') . '</h3>';
    $conditions = array(
      'plugin' => 'reaction rule',
      'active' => TRUE,
      'event' => 'flexiform_submit_' . $flexiform->form,
    );
    $options = array(
      'show plugin' => FALSE,
      'base path' => 'admin/config/workflow/rules/reaction',
    );
    $form['rules']['active'] = rules_ui()
      ->overviewTable($conditions, $options);
    $form['rules']['active']['#caption'] = t('Active Actions');
    $form['rules']['active']['#empty'] = t('There are no active rules. <a href="!url">Add new Action</a>.', array(
      '!url' => url('admin/config/workflow/rules/reaction/add'),
    ));
    $conditions['active'] = FALSE;
    $form['rules']['inactive'] = rules_ui()
      ->overviewTable($conditions, $options);
    $form['rules']['inactive']['#caption'] = t('Inactive Actions');
    $form['rules']['inactive']['#empty'] = t('There are no inactive rules.');
  }
  return $form;
}