summaryrefslogtreecommitdiffstats
path: root/views_natural_sort.module
blob: d164eca9ac4f18e3646e7c11c7422caa485abfe4 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php

/**
 * @file
 * Contains views_natural_sort.module..
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\views_natural_sort\IndexRecordType;

/**
 * Implements hook_help().
 */
function views_natural_sort_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the views_natural_sort module.
    case 'help.page.views_natural_sort':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Sort results naturally on a node&#039;s title skipping articles like &quot;the&quot; and &quot;a.&quot;') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function views_natural_sort_module_implements_alter(&$implements, $hook) {
  if ($hook == 'views_data_alter') {
    // Make views natural sort always last so we get all the up to date info.
    $group = $implements['views_natural_sort'];
    unset($implements['views_natural_sort']);
    $implements['views_natural_sort'] = $group;
  }
}

/**
 * Implements hook_views_natural_sort_get_entry_types().
 * @Not-Rewritten
 */
function views_natural_sort_views_natural_sort_get_entry_types() {
  $service = Drupal::service('views_natural_sort.service');
  $supported_entity_properties = $service->getViewsSupportedEntityProperties();
  $entry_types = [];
  foreach ($supported_entity_properties as $entity_type => $properties) {
    foreach ($properties as $property => $schema_info) {
      $entry_types = new IndexRecordType($entity_type, $property);
    }
  }
  return $entry_types;
}

/**
 * Implements hook_views_natural_sort_queue_rebuild_data().
 */
function views_natural_sort_views_natural_sort_queue_rebuild_data(array $entry_types){
  $service = Drupal::service('views_natural_sort.service');
  $supported_entity_properties = $service->getViewsSupportedEntityProperties();
  $queue = Drupal::queue('views_natural_sort_entity_index');
  foreach ($entry_types as $entry_type) {
    $entity_type = $entry_type->getEntityType();
    $field = $entry_type->getField();

    if (empty($supported_entity_properties[$entity_type]) ||
      empty($supported_entity_properties[$entity_type][$field])) {
      continue;
    }
    $query = Drupal::entityQuery($entity_type);
    $entity_ids = $query->execute();
    foreach ($entity_ids as $entity_id) {
      $queue->createItem($entity_id);
    }
  }
  return ['views_natural_sort_entity_index'];
}

/**
 * Implements hook_entity_insert().
 *
 * This keeps our natural sort index up to date.
 */
function views_natural_sort_entity_insert(EntityInterface $entity) {
  $service = Drupal::service('views_natural_sort.service');
  $supported_entity_properties = $service->getViewsSupportedEntityProperties();
  if (isset($supported_entity_properties[$entity->getEntityTypeId()])) {
    $service->storeIndexRecordsFromEntity($entity);
  }
}

/**
 * Implementation of hook_entity_update().
 *
 * This keeps our natural sort index up to date.
 */
function views_natural_sort_entity_update(EntityInterface $entity) {
  views_natural_sort_entity_insert($entity);
}

/**
 * Implementation of hook_entity_delete().
 *
 * This keeps our natural sort index clean.
 */
function views_natural_sort_entity_delete(EntityInterface $entity) {
  views_natural_sort_remove($entry = array(
    'eid' => $entity->id(),
    'entity_type' => $entity->getEntityTypeId(),
  ));
}

/**
 * Remove a views_natural_sort index entry based on keys
 *
 * @param array $index_entry
 *   Mirrors the views_natural_sort table
 *     $eid - Entity Id of the item referenced
 *     $entity_type - The Entity Type. Ex. node
 *     $field - (optional) reference to the property or field name
 *     $delta - (optional)the item number in that field or property
 *   If an optional parameter doesn't exist, this is treated as a wild card
 *   delete
 * @Not-Rewritten
 */
function views_natural_sort_remove($index_entry) {
  $query = db_delete('views_natural_sort')
    ->condition('eid', $index_entry['eid'])
    ->condition('entity_type', $index_entry['entity_type']);
  if (isset($index_entry['field'])) {
    $query->condition('field', $index_entry['field']);
  }
  if (isset($index_entry['delta'])) {
    $query->condition('delta', $index_entry['delta']);
  }
  $query->execute();
}

function views_natural_sort_queue_data_for_rebuild(array $entry_types = []) {
  $moduleHandler = \Drupal::moduleHandler();
  if (empty($entry_types)) {
    $entry_types = $moduleHandler->invokeAll('views_natural_sort_get_entry_types');
  }
  $queues = array_unique($moduleHandler->invokeAll('views_natural_sort_queue_rebuild_data', $entry_types));
  kpr($queues);
  exit();
  $operations = [];
  foreach ($queues as $queue) {
    $operations[] = [
      'views_natural_sort_rebuild_index'
      [$queue],
    ];
  }

  $batch = [
    'operations' => $operations,
    'title' => t('Rebuilding Views Natural Sort Indexing Entries'),
    'finished' => 'views_natural_sort_finish_rebuild',
  ];
  batch_set($batch);
}

function views_natural_sort_rebuild_index($queue_name, &$context) {
  /** @var QueueInterface $queue */
  $queue = \Drupal::queue($queue_name);
  /** @var QueueWorkerInterface $queue_worker */
  $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
  $config = \Drupal::configFactory()->get('views_natural_sort.settings');

    // Alias sandbox for easier referencing.
  $sandbox = &$context['sandbox'];
    // Alias results for easier referencing.
  $results = &$context['results'];
  if (empty($sandbox)) {
    $sandbox['current'] = 0;
    $sandbox['max'] = $queue->numberOfItems();
    $sandbox['items_per_batch'] = $config->get('rebuild_items_per_batch');
  }
  for ($i = 0; $i < $sandbox['items_per_batch'] && $sandbox['current'] < $sandbox['max']; $i++) {
    while ($item = $queue->claimItem()) {
      try {
        $queue_worker->processItem($item->data);
        $queue->deleteItem($item);
      }
      catch (SuspendQueueException $e) {
        $queue->releaseItem($item);
        break;
      }
      catch (\Exception $e) {
        watchdog_exception('npq', $e);
      }
    }
    $item = $queue->claimItem(10);
    if ($item) {
      views_natural_sort_process_index_queue($item->data);
      $queue->deleteItem($item);
    }
    $sandbox['current']++;
  }
  $results['entries'] = $sandbox['current'];
  if ($sandbox['current'] != $sandbox['max']) {
    $context['finished'] = $sandbox['current'] / $sandbox['max'];
  }
}

function views_natural_sort_finish_rebuild($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('Index rebuild has completed.'));
    drupal_set_message(t('Indexed %count.', [
      '%count' => \Drupal::translation()->formatPlural($results['entries'], '1 entry', '@count entries'),
    ]));
  }
}