You are here

function globallink_field in GlobalLink Connect for Drupal 7.5

Same name and namespace in other branches
  1. 7.7 globallink_field_configuration.inc \globallink_field()
  2. 7.6 globallink_field_configuration.inc \globallink_field()

Renders form for globallink_field.

1 string reference to 'globallink_field'
globallink_field_page in ./globallink_field_configuration.inc
Returns form for globallink_field page.

File

./globallink_field_configuration.inc, line 19
globallink_field_configuration.inc is a file that contains most functions needed on the Field Configuration UI.

Code

function globallink_field() {
  module_load_include('inc', 'globallink', 'globallink');
  $selected_value = '';
  $entity_type = 'node';
  if (!empty($_SESSION['globallink_selected_content_type'])) {
    $selected_value = $_SESSION['globallink_selected_content_type'];
  }
  if (globallink_starts_with($selected_value, 'fpp:')) {
    $entity_type = 'fieldable_panels_pane';
  }
  else {
    if (globallink_starts_with($selected_value, 'tax:')) {
      $entity_type = 'taxonomy_term';
    }
  }
  $form = array();
  $n_arr = array(
    '' => t('--Select Content Type--'),
  );
  $t_arr = globallink_get_translatable_node_types_and_names();
  $node_types = $n_arr + $t_arr;
  if (module_exists('globallink_fieldable_panels')) {
    module_load_include('inc', 'globallink', 'globallink_fieldable_panels/globallink_fieldable_panels');
    $t_arr = globallink_get_all_translatable_fpp_types_and_names();
    if (!empty($t_arr)) {
      $node_types += array(
        '_fpp_' => t('--Select FPP Type--'),
      );
      $node_types += $t_arr;
    }
  }
  if (module_exists('globallink_taxonomy')) {
    module_load_include('inc', 'globallink', 'globallink_taxonomy/globallink_taxonomy');
    $t_arr = globallink_get_all_translatable_taxonomy_types_and_names();
    if (!empty($t_arr)) {
      $node_types += array(
        '_tax_' => t('--Select Vocabulary--'),
      );
      $node_types += $t_arr;
    }
  }
  $form['#attributes']['class'][] = 'globallink-field';
  $form['select_field'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
  );
  $form['select_field']['select_type'] = array(
    '#type' => 'select',
    '#title' => t('Content Type'),
    '#options' => $node_types,
    '#default_value' => $selected_value,
    '#attributes' => array(
      'class' => array(
        'globallink-field-select-type',
      ),
    ),
    '#field_suffix' => '    ',
  );
  $form['select_field']['select_entity_type'] = array(
    '#type' => 'hidden',
    '#value' => $entity_type,
  );
  $form['select_field']['go'] = array(
    '#type' => 'submit',
    '#value' => t('Go'),
    '#attributes' => array(
      'style' => 'display: none;',
    ),
  );
  if ($selected_value != '') {
    $disabled = FALSE;
    $f_arr = array(
      '[all]' => t('All'),
    );
    switch ($entity_type) {
      case 'node':
        $p_arr = globallink_get_pending_fields($selected_value, $entity_type);
        break;
      case 'fieldable_panels_pane':
        $fpp_type = str_replace('fpp:', '', $selected_value);
        $p_arr = globallink_get_pending_fields($fpp_type, $entity_type);
        break;
      case 'taxonomy_term':
        $tax_type = str_replace('tax:', '', $selected_value);
        $p_arr = globallink_get_pending_fields($tax_type, $entity_type);
        break;
    }
    $pending_fields = $f_arr + $p_arr;
    if (count($pending_fields) == 1) {
      $pending_fields = array(
        '' => '--Select--',
      );
      $disabled = TRUE;
    }
    $form['select_field']['pending_fields'] = array(
      '#type' => 'select',
      '#title' => '  Fields:  ',
      '#options' => $pending_fields,
      '#field_suffix' => '    ',
    );
    $form['select_field']['add_field'] = array(
      '#type' => 'submit',
      '#value' => t('Add'),
      '#disabled' => $disabled,
    );
    $form['select_field']['br_markup'] = array(
      '#type' => 'markup',
      '#markup' => '<BR/><BR/>',
    );
    $header = array(
      'field_label' => array(
        'field' => 'field_label',
        'data' => t('Field Label'),
      ),
      'field_name' => array(
        'field' => 'field_name',
        'data' => t('Field Name'),
      ),
      'field_type' => array(
        'field' => 'field_type',
        'data' => t('Field Type'),
      ),
    );
    $ignore_field_types = array(
      'list_boolean',
      'file',
      'taxonomy_term_reference',
      'entityreference',
    );
    $query = db_select('globallink_field_config', 'tf')
      ->fields('tf')
      ->condition('content_type', $selected_value, '=')
      ->condition('field_type', $ignore_field_types, 'NOT IN')
      ->extend('TableSort')
      ->orderByHeader($header);
    $results = $query
      ->execute();
    $count = 0;
    $rows = array();
    $default_values = array();
    $field_types = field_info_field_types();
    $field_bundles = field_info_bundles();
    foreach ($results as $item) {
      if ($item->field_type == 'field_collection') {
        continue;
      }
      $count++;
      $bundle_info = $field_bundles[$item->entity_type];
      $bundle_text = $item->bundle;
      switch ($item->entity_type) {
        case 'node':
          if (isset($bundle_info[$item->content_type])) {
            $bundle_text = $bundle_info[$item->content_type]['label'];
          }
          break;
        case 'fieldable_panels_pane':
          $exploded_bl = explode(':', $item->bundle);
          $bundle = $exploded_bl[1];
          if (isset($bundle_info[$bundle])) {
            $bundle_text = $bundle_info[$bundle]['label'];
          }
          break;
        case 'taxonomy_term':
          $exploded_bl = explode(':', $item->bundle);
          $bundle = $exploded_bl[1];
          if (isset($bundle_info[$bundle])) {
            $bundle_text = $bundle_info[$bundle]['label'];
          }
          break;
      }
      $rows[$item->fid] = array(
        'field_label' => $item->field_label,
        'bundle' => $bundle_text,
        'field_name' => $item->field_name,
        'field_type' => isset($field_types[$item->field_type]) ? $field_types[$item->field_type]['label'] : $item->field_type,
      );
      if ($item->translatable == 1) {
        $default_values[$item->fid] = 1;
      }
      else {
        $default_values[$item->fid] = 0;
      }
    }
    $form['field-heading'] = array(
      '#type' => 'container',
    );
    $form['field-heading'][] = array(
      '#markup' => '<em>' . t('Check all the fields that you wish to translate for this content type.') . '</em><br>',
    );
    $form['table'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $rows,
      '#empty' => t('No items available'),
      '#default_value' => $default_values,
    );
    if (globallink_content_type_workbench_enabled($selected_value)) {
      if ($states = workbench_moderation_state_labels()) {
        $mod_key = '';
        $current_value = variable_get('globallink_moderation_' . $selected_value, FALSE);
        if ($current_value) {
          $states_value = $states[$current_value];
          unset($states[$current_value]);
          $new_value = array(
            $mod_key => t('Current: @state', array(
              '@state' => $states_value,
            )),
          );
          $states = array_merge($new_value, $states);
        }
        $form['state'] = array(
          '#title' => t('Moderation state (after import)'),
          '#type' => 'select',
          '#options' => $states,
          '#default_value' => $mod_key,
        );
      }
    }
    if ($count > 0) {
      $form['submit_field_update'] = array(
        '#type' => 'submit',
        '#value' => t('Update'),
      );
    }
  }
  return $form;
}