You are here

function og_ui_field_settings in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og_ui/og_ui.admin.inc \og_ui_field_settings()

Allow site admin to add or remove group fields from fieldable entities.

1 string reference to 'og_ui_field_settings'
og_ui_menu in og_ui/og_ui.module
Implements hook_menu().

File

og_ui/og_ui.admin.inc, line 923
Admin settings for Organic groups module.

Code

function og_ui_field_settings($form, &$form_state) {
  $form = array();
  $options = array();
  $options = array();
  foreach (entity_get_info() as $entity_type => $entity_info) {
    if (empty($entity_info['fieldable'])) {
      continue;
    }
    foreach ($entity_info['bundles'] as $bundle_name => $bundle) {

      // Prefix the bundle name with the entity type.
      $entity_name = check_plain("{$entity_info['label']} ({$entity_type})");
      $options[$entity_name][$entity_type . ':' . $bundle_name] = filter_xss($bundle['label']);
    }
  }
  $form['bundle'] = array(
    '#title' => t('Bundles'),
    '#type' => 'select',
    '#options' => $options,
  );
  $options = array();
  foreach (og_fields_info() as $field_name => $field) {
    foreach ($field['type'] as $type) {
      $type_name = $type == 'group' ? t('Group') : t('Group content');
      $options[$type_name][$field_name] = filter_xss($field['instance']['label']);
    }
  }
  $selected_field_name = !empty($form_state['values']['field_type']) ? $form_state['values']['field_type'] : OG_AUDIENCE_FIELD;
  $selected_og_info = og_fields_info($selected_field_name);
  $form['field_info_wrapper'] = array(
    '#prefix' => '<div id="field-info-wrapper">',
    '#suffix' => '</div>',
    '#parents' => array(
      'field_info_wrapper',
    ),
    '#type' => 'fieldset',
  );
  $form['field_info_wrapper']['field_type'] = array(
    '#title' => t('Fields'),
    '#type' => 'select',
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $selected_field_name,
    '#ajax' => array(
      'callback' => 'og_ui_admin_fields_ajax_callback',
      'wrapper' => 'field-info-wrapper',
    ),
  );
  $form['field_info_wrapper']['description'] = array(
    '#markup' => $selected_og_info['description'],
  );
  if (!empty($selected_og_info['multiple'])) {
    $form['field_info_wrapper']['field_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Field name'),
      '#description' => t('This field type supports adding multiple instances on the same bundle (i.e. the field name is not hardcoded).'),
      '#required' => TRUE,
      '#maxlength' => 32,
      '#default_value' => $selected_field_name,
    );
  }
  else {

    // Pass the field name as a value.
    $form['field_name_wrapper']['field_name'] = array(
      '#type' => 'value',
      '#value' => $selected_field_name,
    );
  }
  $field_enabled = array();
  $og_fields = og_fields_info();
  $og_fields_name = array_keys($og_fields);
  $entity_info = entity_get_info();

  // Get the fields that exist in the bundle.
  foreach (field_info_fields() as $field_name => $field) {
    if (in_array($field_name, $og_fields_name) && !empty($field['bundles'])) {
      foreach ($field['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {
          $field_enabled[$entity_type][$bundle][] = $field_name;
        }
      }
    }
  }
  if ($field_enabled) {
    $form['group_fields'] = array(
      '#type' => 'vertical_tabs',
      '#weight' => 99,
    );

    // Show all the group fields of each bundle.
    foreach ($field_enabled as $entity_type => $bundles) {
      foreach ($bundles as $bundle => $fields) {
        $options = array();
        $bundles = field_info_bundles($entity_type);
        $form['group_fields_' . $entity_type . '_' . $bundle] = array(
          '#type' => 'fieldset',
          '#title' => t('@bundle - @entity entity', array(
            '@bundle' => $bundles[$bundle]['label'],
            '@entity' => $entity_info[$entity_type]['label'],
          )),
          '#collapsible' => TRUE,
          '#group' => 'group_fields',
        );
        foreach ($fields as $field_name) {
          $options[] = array(
            check_plain($og_fields[$field_name]['instance']['label']),
            filter_xss($og_fields[$field_name]['description']),
            l(t('Delete'), "admin/config/group/fields/{$entity_type}/{$bundle}/{$field_name}/delete"),
          );
        }
        $header = array(
          t('Field'),
          t('Description'),
          t('Operations'),
        );
        $form['group_fields_' . $entity_type . '_' . $bundle]['fields'] = array(
          '#markup' => theme('table', array(
            'header' => $header,
            'rows' => $options,
          )),
        );
      }
    }
  }
  else {
    $form['group_fields'] = array(
      '#markup' => t('There are no Group fields attached to any bundle yet.'),
    );
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add field'),
  );
  return $form;
}