You are here

globallink_field_configuration.inc in GlobalLink Connect for Drupal 7.7

Globallink_field_configuration.inc is a file that contains most functions needed on the Field Configuration UI.

File

globallink_field_configuration.inc
View source
<?php

/**
 * @file
 * Globallink_field_configuration.inc is a file that contains most functions
 * needed on the Field Configuration UI.
 */

/**
 * Returns form for globallink_field page.
 */
function globallink_field_page() {
  return drupal_get_form('globallink_field');
}

/**
 * Renders form for globallink_field.
 */
function globallink_field() {
  module_load_include('inc', 'globallink', 'globallink');
  module_load_include('inc', 'globallink', 'globallink_taxonomy/globallink_taxonomy');
  $selected_value = '';
  $entity_type = GLOBALLINK_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';
  }
  elseif (globallink_starts_with($selected_value, 'tax:')) {
    $entity_type = 'taxonomy_term';
    $arr_avail = globallink_get_all_translatable_taxonomy_types_and_names();
    if (!array_key_exists($selected_value, $arr_avail)) {
      $selected_value = '';
    }
  }
  elseif (globallink_starts_with($selected_value, 'bean:')) {
    $entity_type = 'bean';
  }
  elseif (globallink_starts_with($selected_value, 'file:')) {
    $entity_type = 'file';
  }
  elseif (globallink_starts_with($selected_value, 'cp:')) {
    $entity_type = 'commerce_product';
  }
  elseif (globallink_starts_with($selected_value, 'ce:')) {
    $entity_type = 'custom_entity';
  }
  $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;
    }
  }
  if (module_exists('globallink_beans')) {
    module_load_include('inc', 'globallink', 'globallink_beans/globallink_beans');
    $t_arr = globallink_get_all_translatable_bean_types_and_names();
    if (!empty($t_arr)) {
      $node_types += array(
        '_bean_' => t('--Select Bean Type--'),
      );
      $node_types += $t_arr;
    }
  }
  if (module_exists('globallink_file_entity')) {
    module_load_include('inc', 'globallink', 'globallink_file_entity/globallink_file_entity');
    $t_arr = globallink_get_all_translatable_file_types();
    if (!empty($t_arr)) {
      $node_types += array(
        '_file_' => t('--Select File Type--'),
      );
      $node_types += $t_arr;
    }
  }
  if (module_exists('globallink_commerce')) {
    module_load_include('inc', 'globallink', 'globallink_commerce/globallink_commerce');
    $t_arr = globallink_get_all_translatable_cp_types_and_names();
    if (!empty($t_arr)) {
      $node_types += array(
        '_cp_' => '--Select Product Type--',
      );
      $node_types += $t_arr;
    }
  }
  if (module_exists('eck')) {
    module_load_include('inc', 'globallink', 'globallink_custom_entity/globallink_custom_entity');
    $t_arr = globallink_get_all_translatable_customentity_types_and_names();
    if (!empty($t_arr)) {
      $node_types += array(
        '_ce_' => t('--Select Bundles--'),
      );
      $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' => '&nbsp;&nbsp;&nbsp;&nbsp;',
  );
  $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 GLOBALLINK_ENTITY_TYPE_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;
      case GLOBALLINK_ENTITY_TYPE_BEAN:
        $bean_type = str_replace('bean:', '', $selected_value);
        $p_arr = globallink_get_pending_fields($bean_type, $entity_type);
        break;
      case 'file':
        $file_type = str_replace('file:', '', $selected_value);
        $p_arr = globallink_get_pending_fields($file_type, $entity_type);
        break;
      case GLOBALLINK_ENTITY_TYPE_COM:
        $file_type = str_replace('cp:', '', $selected_value);
        $p_arr = globallink_get_pending_fields($file_type, $entity_type);
        break;
      case 'custom_entity':
        $machine_name = str_replace('ce:', '', $selected_value);
        $entity_type = globallink_get_entity_type_for_bundle($machine_name);
        $bundle = $node_types[$selected_value];
        $selected_value = $bundle;
        $p_arr = globallink_get_pending_fields($bundle, $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' => '&nbsp;&nbsp;Fields:&nbsp;&nbsp;',
      '#options' => $pending_fields,
      '#field_suffix' => '&nbsp;&nbsp;&nbsp;&nbsp;',
    );
    $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'),
      ),
      'bundle' => array(
        'field' => 'bundle',
        'data' => t('Bundle'),
      ),
    );
    $ignore_field_types = array(
      'list_boolean',
      'taxonomy_term_reference',
      'list_integer',
      'list_text',
      'list_float',
    );
    $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;
      }
      if ($item->entity_type != 'node' || $item->entity_type == 'node' && entity_translation_node_supported_type($item->content_type)) {
        if ($item->field_name != 'metatags' && strpos($item->field_name, '@summary') == FALSE) {
          $query = db_select('field_config', 'tf')
            ->fields('tf', array(
            'field_name',
          ))
            ->condition('field_name', $item->field_name, '=')
            ->condition(db_or()
            ->condition(db_and()
            ->condition('translatable', '1', '='))
            ->condition(db_and()
            ->condition('type', 'image', '=')));
          $result_field = $query
            ->execute();
          $row_count = $result_field
            ->rowCount();
          if (!$row_count) {
            continue 1;
          }
        }
      }
      $count++;
      $bundle_info = $field_bundles[$item->entity_type];
      $bundle_text = $item->bundle;
      switch ($item->entity_type) {
        case GLOBALLINK_ENTITY_TYPE_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 (!field_info_instance($item->entity_type, $item->field_name, $bundle)) {
            continue 2;
          }
          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 (!field_info_instance($item->entity_type, $item->field_name, $bundle) && $item->field_name !== 'metatags') {
            continue 2;
          }
          if (isset($bundle_info[$bundle])) {
            $bundle_text = $bundle_info[$bundle]['label'];
          }
          break;
        case 'bean':
          $exploded_bl = explode(':', $item->bundle);
          $bundle = $exploded_bl[1];
          if (isset($bundle_info[$bundle])) {
            $bundle_text = $bundle_info[$bundle]['label'];
          }
          break;
        case 'file':
          $exploded_bl = explode(':', $item->bundle);
          $bundle = $exploded_bl[1];
          if (isset($bundle_info[$bundle])) {
            $bundle_text = $bundle_info[$bundle]['label'];
          }
          break;
        case GLOBALLINK_ENTITY_TYPE_COM:
          $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;
}

/**
 * Handles submission of globallink_field form.
 */
function globallink_field_submit($form, &$form_state) {
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  switch ($op) {
    case t('Go'):
      $_SESSION['globallink_selected_content_type'] = $form_state['values']['select_type'];
      break;
    case t('Update'):
      $type = $form_state['values']['select_type'];
      $select_entity_type = $form_state['values']['select_entity_type'];
      $fids = array_filter($form_state['values']['table']);
      if ($select_entity_type == 'custom_entity' && globallink_starts_with($type, 'ce:')) {
        $selected_type = str_replace('ce:', '', $type);
        $bundle = globallink_get_bundle_for_field($selected_type);
        globallink_update_gl_field_config($bundle, $fids);
      }
      else {
        globallink_update_gl_field_config($type, $fids);
      }
      $state = isset($form_state['input']['state']) ? $form_state['input']['state'] : FALSE;
      if ($state) {
        variable_set('globallink_moderation_' . $type, $state);
      }
      drupal_set_message(t('Field configurations have been saved successfully.'));
      break;
    case t('Add'):
      $field = $form_state['values']['pending_fields'];
      globallink_insert_gl_field_config($field, $_SESSION['globallink_selected_content_type'], $form_state['values']['select_entity_type']);
      drupal_set_message(t('Field configurations have been added successfully.'));
      break;
  }
}

/**
 * Updates GlobalLink field configuration.
 *
 * @param string $type
 *   The content type.
 * @param array $fids
 *   The array of field IDs.
 */
function globallink_update_gl_field_config($type, $fids) {

  // First update everything to 0 for this type.
  db_update('globallink_field_config')
    ->fields(array(
    'translatable' => 0,
  ))
    ->condition('content_type', $type, '=')
    ->condition('field_type', 'field_collection', '!=')
    ->execute();
  foreach ($fids as $fid) {
    db_update('globallink_field_config')
      ->fields(array(
      'translatable' => 1,
    ))
      ->condition('fid', $fid, '=')
      ->execute();
  }
}

/**
 * Inserts GlobalLink field configuration.
 *
 * @param string $field_name
 *   The field name.
 * @param string $content_type
 *   The content type.
 */
function globallink_insert_gl_field_config($field_name, $content_type, $entity_type) {
  if ($entity_type == GLOBALLINK_ENTITY_TYPE_NODE) {
    $p_arr = globallink_get_pending_fields($content_type, $entity_type);
  }
  elseif ($entity_type == 'fieldable_panels_pane') {
    $fpp_type = str_replace('fpp:', '', $content_type);
    $p_arr = globallink_get_pending_fields($fpp_type, $entity_type);
  }
  elseif ($entity_type == 'taxonomy_term') {
    $tax_type = str_replace('tax:', '', $content_type);
    $p_arr = globallink_get_pending_fields($tax_type, $entity_type);
  }
  elseif ($entity_type == GLOBALLINK_ENTITY_TYPE_BEAN) {
    $bean_type = str_replace('bean:', '', $content_type);
    $p_arr = globallink_get_pending_fields($bean_type, $entity_type);
  }
  elseif ($entity_type == 'file') {
    $file_type = str_replace('file:', '', $content_type);
    $p_arr = globallink_get_pending_fields($file_type, $entity_type);
  }
  elseif ($entity_type == GLOBALLINK_ENTITY_TYPE_COM) {
    $file_type = str_replace('cp:', '', $content_type);
    $p_arr = globallink_get_pending_fields($file_type, $entity_type);
  }
  elseif ($entity_type == 'custom_entity') {
    $machine_name = str_replace('ce:', '', $content_type);
    $entity_type = globallink_get_entity_type_for_bundle($machine_name);
    $content_type = globallink_get_bundle_for_field($machine_name);
    $p_arr = globallink_get_pending_fields($content_type, $entity_type);
  }
  if ($field_name == '[all]') {
    $f_keys = array_keys($p_arr);
    foreach ($f_keys as $f_key) {
      if ($f_key == '[all]') {
        continue;
      }
      $sum_field = '';
      if (strpos($f_key, '@summary') !== FALSE) {
        $sum_field = $f_key;
      }
      if ($f_key != 'title' && $f_key != 'metatags' && empty($sum_field)) {
        $field = field_info_field($f_key);
        switch ($field['type']) {
          case 'list_boolean':
          case 'file':
          case 'taxonomy_term_reference':
          case 'list_integer':
          case 'field_collection':
            break;
          default:
            db_insert('globallink_field_config')
              ->fields(array(
              'content_type' => $content_type,
              'entity_type' => $entity_type,
              'bundle' => $content_type,
              'field_name' => $f_key,
              'field_type' => $field['type'],
              'field_label' => $p_arr[$f_key],
              'translatable' => 1,
            ))
              ->execute();
        }
      }
      else {
        db_insert('globallink_field_config')
          ->fields(array(
          'content_type' => $content_type,
          'entity_type' => $entity_type,
          'bundle' => $content_type,
          'field_name' => $f_key,
          'field_type' => 'text',
          'field_label' => $p_arr[$f_key],
          'translatable' => 1,
        ))
          ->execute();
      }
    }
    if (module_exists('field_collection')) {
      globallink_insert_fc_fields($content_type);
    }
  }
  else {
    $sum_field = '';
    if (strpos($field_name, '@summary') !== FALSE) {
      $sum_field = $field_name;
    }
    if ($field_name != 'title' && $field_name != 'metatags' && empty($sum_field)) {
      $field = field_info_field($field_name);
      switch ($field['type']) {
        case 'list_boolean':
        case 'file':
        case 'list_integer':
        case 'taxonomy_term_reference':
          break;
        case 'field_collection':
          $bl = $content_type;
          if ($entity_type == GLOBALLINK_ENTITY_TYPE_COM) {
            $bl = str_replace('cp:', '', $content_type);
          }
          globallink_insert_fc($entity_type, $field_name, $content_type, $bl);
          break;
        default:
          db_insert('globallink_field_config')
            ->fields(array(
            'content_type' => $content_type,
            'entity_type' => $entity_type,
            'bundle' => $content_type,
            'field_name' => $field_name,
            'field_type' => $field['type'],
            'field_label' => $p_arr[$field_name],
            'translatable' => 1,
          ))
            ->execute();
      }
    }
    else {
      db_insert('globallink_field_config')
        ->fields(array(
        'content_type' => $content_type,
        'entity_type' => $entity_type,
        'bundle' => $content_type,
        'field_name' => $field_name,
        'field_type' => 'text',
        'field_label' => $p_arr[$field_name],
        'translatable' => 1,
      ))
        ->execute();
    }
  }
}

/**
 * Inserts field collection fields.
 *
 * @param string $node_type
 *   The node type.
 */
function globallink_insert_fc_fields($node_type) {
  $entity_type = GLOBALLINK_ENTITY_TYPE_NODE;
  $bean_type = $node_type;
  if (strpos($node_type, 'bean:') !== FALSE) {
    $bean_type = str_replace('bean:', '', $node_type);
    $entity_type = 'bean';
  }
  if (strpos($node_type, 'cp:') !== FALSE) {
    $bean_type = str_replace('cp:', '', $node_type);
    $entity_type = 'commerce_product';
  }
  $field_arr = field_info_instances($entity_type, $bean_type);
  $keys = array_keys($field_arr);
  foreach ($keys as $field_name) {
    $field_info = field_info_field($field_name);
    if ($field_info['type'] != 'field_collection' || $field_info['translatable'] == 0) {
      continue;
    }
    if (globallink_is_field_configured_for_translation($entity_type, $node_type, $field_name, $node_type)) {
      continue;
    }
    db_insert('globallink_field_config')
      ->fields(array(
      'content_type' => $node_type,
      'entity_type' => $entity_type,
      'bundle' => $node_type,
      'field_name' => $field_name,
      'field_type' => $field_info['type'],
      'field_label' => $field_arr[$field_name]['label'],
      'translatable' => 1,
    ))
      ->execute();
    $fc_field_infos = field_info_instances('field_collection_item');
    if (isset($fc_field_infos) && isset($fc_field_infos[$field_name]) && is_array($fc_field_infos[$field_name])) {
      $fc_items = array_keys($fc_field_infos[$field_name]);
      foreach ($fc_items as $fc_item) {
        if (globallink_is_field_configured_for_translation('field_collection_item', $field_name, $fc_item, $node_type)) {
          continue;
        }
        globallink_insert_fc_item_fields($node_type, $field_name, $fc_item);
      }
    }
  }
}

/**
 * Inserts field collection item fields.
 *
 * @param string $content_type
 *   The content type.
 * @param string $parent_field_name
 *   The parent's field name.
 * @param string $field_name
 *   The field name.
 */
function globallink_insert_fc_item_fields($content_type, $parent_field_name, $field_name) {
  $fc_field_info = field_info_field($field_name);
  $fc_field_instance = field_info_instance('field_collection_item', $field_name, $parent_field_name);
  switch ($fc_field_info['type']) {
    case 'list_boolean':
    case 'file':
    case 'list_integer':
    case 'taxonomy_term_reference':
      break;
    case 'field_collection':
      db_insert('globallink_field_config')
        ->fields(array(
        'content_type' => $content_type,
        'entity_type' => 'field_collection_item',
        'bundle' => $parent_field_name,
        'field_name' => $field_name,
        'field_type' => $fc_field_info['type'],
        'field_label' => $fc_field_instance['label'],
        'translatable' => 1,
      ))
        ->execute();
      $fc_field_infos = field_info_instances('field_collection_item');
      if (isset($fc_field_infos) && isset($fc_field_infos[$field_name]) && is_array($fc_field_infos[$field_name])) {
        $fc_items = array_keys($fc_field_infos[$field_name]);
        foreach ($fc_items as $fc_item) {
          if (globallink_is_field_configured_for_translation('field_collection_item', $field_name, $fc_item, $content_type)) {
            continue;
          }
          globallink_insert_fc_item_fields($content_type, $field_name, $fc_item);
        }
      }
      break;
    default:
      $translatable = 1;
      db_insert('globallink_field_config')
        ->fields(array(
        'content_type' => $content_type,
        'entity_type' => 'field_collection_item',
        'bundle' => $parent_field_name,
        'field_name' => $field_name,
        'field_type' => $fc_field_info['type'],
        'field_label' => $fc_field_instance['label'],
        'translatable' => $translatable,
      ))
        ->execute();
  }
}

Functions

Namesort descending Description
globallink_field Renders form for globallink_field.
globallink_field_page Returns form for globallink_field page.
globallink_field_submit Handles submission of globallink_field form.
globallink_insert_fc_fields Inserts field collection fields.
globallink_insert_fc_item_fields Inserts field collection item fields.
globallink_insert_gl_field_config Inserts GlobalLink field configuration.
globallink_update_gl_field_config Updates GlobalLink field configuration.