You are here

function conditional_fields_dependency_add_form in Conditional Fields 7.3

Dependency add form.

See also

conditional_fields_dependency_add_form_submit()

conditional_fields_dependency_add_form_validate()

1 string reference to 'conditional_fields_dependency_add_form'
conditional_fields_forms in ./conditional_fields.module
Implements hook_forms().

File

includes/conditional_fields.admin.inc, line 106
Administration of dependencies.

Code

function conditional_fields_dependency_add_form($form, &$form_state, $entity_type, $bundle_name) {
  $form = array();
  $instances = field_info_instances($entity_type, $bundle_name);
  if (count($instances) < 2) {
    $form['no_fields'] = array(
      '#markup' => t('Add at least two fields to enable dependencies.'),
    );
    return $form;
  }
  $dependencies = conditional_fields_load_dependencies($entity_type, $bundle_name);
  $form['table'] = array(
    '#type' => 'conditional_fields_table',
    '#entity_type' => $entity_type,
    '#bundle_name' => $bundle_name,
    '#header' => array(
      t('Dependent'),
      t('Dependees'),
      array(
        'data' => t('Description'),
        'colspan' => 2,
      ),
      array(
        'data' => t('Operations'),
        'colspan' => 2,
      ),
    ),
    '#attributes' => array(
      'class' => array(
        'conditional-fields-overview',
      ),
    ),
    'dependencies' => array(),
  );
  $form['table']['#attached']['css'][] = drupal_get_path('module', 'conditional_fields') . '/conditional_fields.css';
  if ($dependencies) {
    $destination = drupal_get_destination();
    $grouping_count = array();
    foreach ($dependencies['dependents'] as $dependent => $dependees) {
      uasort($dependees, '_conditional_fields_sort_dependees');

      // Enable row grouping by operator.
      $first_row = $show_AND = $show_OR = $show_XOR = TRUE;
      if (!isset($grouping_count[$dependent])) {
        $grouping_count[$dependent] = array(
          'AND' => 0,
          'OR' => 0,
          'XOR' => 0,
        );
        foreach ($dependees as $dependency) {
          $grouping_count[$dependent][$dependency['options']['grouping']]++;
        }
      }
      foreach ($dependees as $id => $dependency) {
        $form['table']['dependencies'][$id] = array();
        $dependee_count = count($dependees);

        // Dependencies come ordered by dependent, so by adding it only to the
        // first row they will appear grouped.
        if ($first_row == TRUE) {
          $form['table']['dependencies'][$id]['dependent'] = array(
            '#markup' => check_plain($instances[$dependent]['label']) . ' (' . $dependent . ')',
            '#rowspan' => $dependee_count,
          );
          $first_row = FALSE;
        }
        $row = array(
          'dependee' => array(
            '#markup' => check_plain($instances[$dependency['dependee']]['label']) . ' (' . $dependency['dependee'] . ')',
          ),
        );

        // To avoid clutter, collect information about groupings so we can show each
        // operator once per dependent.
        if ($dependee_count > 1) {
          if (${'show_' . $dependency['options']['grouping']}) {
            $row['group'] = array(
              '#markup' => $dependency['options']['grouping'],
              '#rowspan' => $grouping_count[$dependent][$dependency['options']['grouping']],
            );
            ${'show_' . $dependency['options']['grouping']} = FALSE;
          }
        }
        else {
          $row['description']['#colspan'] = 2;
        }
        $row['description']['#markup'] = conditional_fields_dependency_description($instances[$dependency['dependee']]['label'], $instances[$dependent]['label'], $dependency['options']);
        $row['edit'] = array(
          '#type' => 'link',
          '#title' => t('edit'),
          '#href' => 'admin/structure/dependencies/edit/' . $id,
          '#options' => array(
            'query' => $destination,
            'attributes' => array(
              'title' => t('Edit dependency settings.'),
            ),
          ),
          '#query' => drupal_get_destination(),
        );
        $row['delete'] = array(
          '#type' => 'link',
          '#title' => t('delete'),
          '#href' => 'admin/structure/dependencies/delete/' . $id,
          '#options' => array(
            'query' => $destination,
            'attributes' => array(
              'title' => t('Delete dependency.'),
            ),
          ),
        );
        $form['table']['dependencies'][$id] += $row;
      }
    }
  }

  // Build list of available fields.
  $fields = array();
  foreach ($instances as $field) {
    $fields[$field['id']] = check_plain($field['label'] . ' (' . $field['field_name'] . ')');
  }
  asort($fields);

  // Build list of states.
  $states = array_map('drupal_strtolower', conditional_fields_states());

  // Build list of conditions.
  foreach (conditional_fields_conditions() as $condition => $label) {
    $conditions[$condition] = $condition == 'value' ? t('has value...') : t('is !label', array(
      '!label' => drupal_strtolower($label),
    ));
  }

  // Add new dependency row.
  $form['table']['add_new_dependency'] = array(
    'dependent' => array(
      '#type' => 'select',
      '#title' => t('Dependent'),
      '#title_display' => 'invisible',
      '#description' => t('Dependent'),
      '#options' => $fields,
      '#prefix' => '<div class="add-new-placeholder">' . t('Add new dependency') . '</div>',
    ),
    'dependee' => array(
      '#type' => 'select',
      '#title' => t('Dependee'),
      '#title_display' => 'invisible',
      '#description' => t('Dependee'),
      '#options' => $fields,
      '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
    ),
    'state' => array(
      '#type' => 'select',
      '#title' => t('State'),
      '#title_display' => 'invisible',
      '#options' => $states,
      '#default_value' => 'visible',
      '#prefix' => t('The dependent field is') . '&nbsp;<span class="description-select">',
      '#suffix' => '</span>&nbsp;' . t('when the dependee'),
    ),
    'condition' => array(
      '#type' => 'select',
      '#title' => t('Condition'),
      '#title_display' => 'invisible',
      '#options' => $conditions,
      '#default_value' => 'value',
      '#prefix' => '&nbsp;<span class="description-select">',
      '#suffix' => '</span>',
    ),
    'actions' => array(
      'submit' => array(
        '#type' => 'submit',
        '#value' => t('Add dependency'),
      ),
    ),
  );
  return $form;
}