views_natural_sort_text_field.module 7.85 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * The Views Natural Sort Text Field module file.
6 7 8
 */

/**
9
 * Implements hook_views_api().
10
 */
11 12
function views_natural_sort_text_field_views_api() {
  return array(
13
    'api' => 3.0,
14 15 16
  );
}

17
/**
18
 * Implements hook_module_implements_alter().
19
 */
20 21 22 23 24 25 26
function views_natural_sort_text_field_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter' && isset($implementations['views_natural_sort_text_field'])) {
    unset($implementations['views_natural_sort_text_field']);
    $implementations['views_natural_sort_text_field'] = FALSE;
  }
}

27
/**
28
 * Implements hook_form_FORM_ID_alter().
29
 */
30
function views_natural_sort_text_field_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
31 32 33 34
  $potential_fields = views_natrual_sort_text_get_potential_fields();
  if (!isset($potential_fields[$form['#field']['id']])) {
    return;
  }
35
  // TODO: Inform the user that this field is being used in views.
36
  $form['field']['views_natural_sort_enable_sort'] = array(
37 38
    '#type' => 'checkbox',
    '#title' => t('Enable natural sorting.'),
39
    '#description' => t("Selecting this option will process and index this field's value for natural sorting in views."),
40
    '#default_value' => empty($form['#field']['views_natural_sort_enable_sort']) ? 0 : $form['#field']['views_natural_sort_enable_sort'],
41
  );
42
  $form['#submit'][] = 'views_natural_sort_text_field_form_field_ui_field_edit_form_alter_submit';
43
}
44

45
/**
46
 * Implements hook_form_FORM_ID_alter().
47
 */
48 49
function views_natural_sort_text_field_form_field_ui_field_edit_form_alter_submit($form, $form_state) {
  // Rebuild index for this field.
50 51
  if ($form_state['input']['field']['views_natural_sort_enable_sort'] != 1) {
    // TODO: Revert views back to regular sort ASC & DESC.
52 53 54 55 56 57 58 59 60 61
    db_delete('views_natural_sort')
      ->condition('field', $form['#field']['field_name'])
      ->execute();
  }
  else {
    module_load_include('inc', 'views_natural_sort', 'views_natural_sort.admin');
    views_natural_sort_rebuild_index_batch_set(views_natural_sort_text_field_views_natural_sort_get_entry_types(array($form['#field'])));
  }
}

62
/**
63
 * Implements hook_views_natural_sort_get_entry_types().
64
 */
65 66 67 68 69
function views_natural_sort_text_field_views_natural_sort_get_entry_types($fields = array()) {
  if (empty($fields)) {
    $fields = views_natural_sort_text_field_get_configured_fields();
  }
  $entry_types = array();
70
  foreach ($fields as $field) {
71
    $entry_types = array_merge($entry_types, views_natural_sort_text_field_get_entry_types_from_field($field));
72 73 74 75
  }
  return $entry_types;
}

76
/**
77
 * Returns the possible vns entry types from the text field.
78
 *
79 80 81
 * @return array
 *   An array taking on the form expected by
 *    hook_views_natural_sort_get_entry_types().
82
 */
83 84
function views_natural_sort_text_field_get_entry_types_from_field($field) {
  $entry_types = array();
85
  foreach ($field['bundles'] as $entity_type => $bundles) {
86 87 88 89 90 91 92 93
    $entry_types[] = array(
      'entity_type' => $entity_type,
      'field' => $field['field_name'],
    );
  }
  return $entry_types;
}

94
/**
95
 * Implements hook_views_natural_sort_queue_rebuild_data().
96 97 98
 */
function views_natural_sort_text_field_views_natural_sort_queue_rebuild_data($entry_type) {
  if (!in_array($entry_type, views_natural_sort_text_field_views_natural_sort_get_entry_types())) {
99 100
    return array();
  }
101
  $queue = views_natural_sort_get_queue();
102
  $field = field_info_field($entry_type['field']);
103
  $data = array();
104 105 106 107 108 109 110 111 112
  $bundles = array();

  if (isset($field['bundles'][$entry_type['entity_type']])) {
    foreach ($field['bundles'][$entry_type['entity_type']] as $key => $bundle) {
      $bundles[] = $bundle;
    }
  }

  $query = new EntityFieldQuery();
113 114 115
  $result = $query->entityCondition('entity_type', $entry_type['entity_type'])
    ->entityCondition('bundle', $bundles, 'IN')
    ->execute();
116 117 118 119 120 121 122
  $entity_ids = array();

  if (isset($result[$entry_type['entity_type']])) {
    $entity_ids = array_keys($result[$entry_type['entity_type']]);
  }

  foreach ($entity_ids as $entity_id) {
123 124
    $results = entity_load($entry_type['entity_type'], array($entity_id));
    $entity = reset($results);
125
    $entries = _views_natural_sort_text_field_to_vns($entry_type['entity_type'], $entity, $field);
126 127
    foreach ($entries as $entry) {
      $queue->createItem($entry);
128 129 130 131
    }
  }
}

132
/**
133
 * Implements hook_field_attach_insert().
134
 */
135 136 137
function views_natural_sort_text_field_field_attach_insert($entity_type, $entity) {
  $configured_fields = views_natural_sort_text_field_get_configured_fields();
  foreach ($configured_fields as $field) {
138
    if (in_array($entity_type, array_keys($field['bundles']))) {
139 140 141 142 143
      _views_natural_sort_text_field_store($entity_type, $entity, $field);
    }
  }
}

144
/**
145
 * Implements hook_field_attach_update().
146
 */
147 148 149
function views_natural_sort_text_field_field_attach_update($entity_type, $entity) {
  $configured_fields = views_natural_sort_text_field_get_configured_fields();
  foreach ($configured_fields as $field) {
150
    if (in_array($entity_type, array_keys($field['bundles']))) {
151 152 153 154 155
      _views_natural_sort_text_field_store($entity_type, $entity, $field);
    }
  }
}

156
/**
157
 * Return a list of fields that have vns sort enabled on them.
158
 *
159 160
 * @return array
 *   An array of field information. See field_info_fields().
161
 */
162 163 164 165 166 167 168 169 170 171 172
function views_natural_sort_text_field_get_configured_fields() {
  $potential_fields = views_natrual_sort_text_get_potential_fields();
  $returned_fields = array();
  foreach ($potential_fields as $field) {
    if (!empty($field['views_natural_sort_enable_sort'])) {
      $returned_fields[$field['id']] = $field;
    }
  }
  return $returned_fields;
}

173
/**
174
 * Get a list of fields that are regular text fields that we could use vns on.
175
 *
176 177
 * @return array
 *   An array of field information. See field_info_fields().
178
 */
179
function views_natrual_sort_text_get_potential_fields() {
180 181
  $all_fields = field_info_fields();
  $returned_fields = array();
182
  foreach ($all_fields as $field_name => $field) {
183 184
    if ($field['module'] == 'text' &&
      $field['storage']['type'] == 'field_sql_storage' &&
185
      $field['columns']['value']['type'] == 'varchar') {
186 187 188 189 190
      $returned_fields[$field['id']] = $field;
    }
  }
  return $returned_fields;
}
191

192
/**
193
 * Helper function to store the text field vns records.
194
 *
195 196 197 198 199 200
 * @param string $entity_type
 *   A valid drupal entity type machine name.
 * @param object $entity
 *   A standard object representing an entity from like entity_load().
 * @param array $field
 *   An array representing a field's info record. See field_info_fields().
201
 */
202
function _views_natural_sort_text_field_store($entity_type, $entity, array $field) {
203 204
  views_natural_sort_store_multiple(_views_natural_sort_text_field_to_vns($entity_type, $entity, $field));
}
205 206

/**
207 208 209 210 211 212 213 214
 * Convert an entity object to a vns record.
 *
 * @param string $entity_type
 *   A valid drupal entity type machine name.
 * @param object $entity
 *   A standard object representing an entity from like entity_load().
 * @param array $field
 *   An array reprenting a field's info record. See field_info_fields().
215
 *
216 217 218 219 220 221 222 223 224
 * @return array
 *   An array of records formatted as:
 *     array(
 *       eid => Entity Id,
 *       entity_type => Entity Type Machine Name,
 *       field => Field Machine Name,
 *       delta => Data's Field Delta,
 *       content => Data,
 *     )
225
 */
226
function _views_natural_sort_text_field_to_vns($entity_type, $entity, array $field) {
227 228 229 230
  $entity_info = entity_get_info($entity_type);
  $entity_id_property = $entity_info['entity keys']['id'];
  $field_name = $field['field_name'];

231
  // TODO: add support for Field Language.
232
  $entries = array();
233
  $field_items = field_get_items($entity_type, $entity, $field_name);
generalredneck's avatar
generalredneck committed
234
  if (!empty($field_items)) {
235
    foreach ($field_items as $delta => $row) {
generalredneck's avatar
generalredneck committed
236 237 238 239 240
      $entries[] = array(
        'eid' => $entity->$entity_id_property,
        'entity_type' => $entity_type,
        'field' => $field_name,
        'delta' => $delta,
241
        'content' => $row['value'],
generalredneck's avatar
generalredneck committed
242 243
      );
    }
244 245 246
  }
  return $entries;
}