summaryrefslogtreecommitdiffstats
path: root/views_natural_sort.api.php
blob: 602ba5e14c6e5691c02d50fe550f19b35e0752b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

/**
 * @file
 * Hook Definition file for Views Natural Sort.
 */

/**
 * Information used by the index rebuilding engine.
 *
 * This information is passed to each module during re-index so that modules can
 * determine whether it needs to return items or not.
 *
 * @return array
 *   Array of arrays defining fields and entities to reindex
 *     array(
 *       array(
 *         'entity_type' - string Ex. node
 *         'field ' - string Field name to indicate during re-index
 *       ),
 *     )
 */
function hook_views_natural_sort_get_entry_types() {
  return array(
    array(
      'entity_type' => 'user',
      'field' => 'book_favorites',
    ),
  );
}

/**
 * Used for a custom module to queue data that needs to be re-indexed.
 *
 * This is typicall used when the module is installed or settings are changed.
 *
 * @param array $entry_type
 *   Array representing an entry type with an entity_type field pair.
 *     $entity_type - The type of the entity we are getting
 *                    data that needs to be re-indexed from
 *     $field - The field that needs to be re-indexed.
 */
function hook_views_natural_sort_queue_rebuild_data(array $entry_type) {
  if ($entry_type['entity_type'] != 'user' || $entry_type['field'] != 'book_favorites') {
    return array();
  }
  $result = db_select('user', 'u')
    ->fields('u', array('uid', 'book_favorites'))
    ->execute();
  $queue = views_natural_sort_get_queue();
  foreach ($result as $row) {
    // Grab the data returned and queue it up for transformation.
    $queue->createItem = array(
      'eid' => $row->uid,
      'entity_type' => 'user',
      'field' => 'book_favorites',
      'delta' => 0,
      'content' => $row->book_favorites,
    );
  }
}

/**
 * Used to define custom transformations or reorder transformations.
 *
 * @param array &$transformations
 *   An array of transformations already defined.
 * @param array $index_entry
 *   A representation of the original entry that is would have been put in the
 *   database before the transformation
 *     $eid - Entity Id of the item referenced
 *     $entity_type - The Entity Type. Ex. node
 *     $field - reference to the property or field name
 *     $delta - the item number in that field or property
 *     $content - The original string before
 *                transformations.
 */
function hook_views_natural_sort_transformations_alter(array &$transformations, array $index_entry) {
  // This function will receive a single argument that is the string that needs
  // to be transformed. The transformation helps the database sort the entry
  // to be more like a human would expect it to.
  //
  // This function will return a single string as well. Note these
  // transformations happen serially, and the transformed string is passed on to
  // the next function in the list. In the example below,
  // `hook_my_special_transformation_function` will receive a string after all
  // other transformations have happened.
  $transformations[] = "_my_special_transformation_function";

  // It is worth noting that the $index_entry does have the original string in
  // it if you need to do some kind of magic. It is best to not clobber other
  // people's transformations if you can help it though.
}

/**
 * This is NOT A HOOK. Example transformation function.
 *
 * @param string $string
 *   The string to be transformed.
 *
 * @return string
 *   A transformed string used for sorting "Naturally".
 */
function _my_special_transformation_function($string) {
  return str_replace('a', '', $string);
}

/**
 * This hook has been deprecated and is no longer called.
 *
 * @deprecated
 *
 * @see hook_views_natural_sort_queue_rebuild_data
 */
function hook_views_natural_sort_queue_rebuild_data($entry_type) {
}