Commit 9ef7ce78 authored by generalredneck's avatar generalredneck

Fixing Indexer and sort handler.

parent 1e7190e2
<?php
namespace Drupal/views_natural_sort/Plugin/QueueWorker;
namespace Drupal\views_natural_sort\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;\
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\views_natural_sort\ViewsNaturalSortService;
/**
* Provides base functionality for the NodePublish Queue Workers.
* Provides base functionality for the VNS Entith Index Queue Workers.
*
* @QueueWorker(
* id = "views_natural_sort_entity_index",
......@@ -23,8 +21,8 @@ class EntityIndexer extends QueueWorkerBase implements ContainerFactoryPluginInt
protected $entityTypeManager;
public function __construct(EntityTypeManager $entityTypeManager, ViewsNaturalSortService $viewsNaturalSortService) {
$this->entityTypeManager = $entityTypeManager
$this->viewsNaturalSortService = $viewsNaturalSortService
$this->entityTypeManager = $entityTypeManager;
$this->viewsNaturalSortService = $viewsNaturalSortService;
}
/**
......
......@@ -15,8 +15,11 @@ use Drupal\views\Plugin\views\sort\SortPluginBase;
* @ViewsSort("natural")
*/
class Natural extends SortPluginBase {
/**
* Flag defining this particular sort as Natural or not.
*
* @var bool
*/
protected $isNaturalSort;
......@@ -25,9 +28,12 @@ class Natural extends SortPluginBase {
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->setNaturalSort(substr($this->options['order'],0,1) == 'N');
$this->setNaturalSort(substr($this->options['order'], 0, 1) == 'N');
}
/**
* {@inheritdoc}
*/
public function query() {
// If this field isn't being used as a Natural Sort Field, move along
// nothing to see here.
......@@ -35,14 +41,6 @@ class Natural extends SortPluginBase {
parent::query();
return;
}
// If someone has submitted the exposed form, lets grab it here
if ($this->options['exposed'] && $this->view->exposed_data['sort_order']) {
$temporder = $this->view->exposed_data['sort_order'];
}
// If we are using this like a normal sort, our info will be here.
else {
$temporder = &$this->options['order'];
}
// Add the Views Natural Sort table for this field.
$vns_alias = 'vns_' . $this->tableAlias;
......@@ -50,10 +48,15 @@ class Natural extends SortPluginBase {
$this->ensureMyTable();
$vns_alias = $this->query->addRelationship('vns_' . $this->tableAlias, $this->naturalSortJoin(), $this->table, $this->relationship);
}
// Sometimes we get the appended N from the sort options. Filter it out here.
$order = substr($temporder, 0, 1) == 'N' ? substr($temporder, 1) : $temporder;
$this->query->addOrderBy($vns_alias, 'content', $order);
$this->query->addOrderBy($vns_alias, 'content', substr($this->options['order'], 1));
}
/**
* Adds the views_natural_sort table to the query.
*
* @return Drupal\views\Plugin\views\join\Standard
* Join object containing views_natural_sort table.
*/
public function naturalSortJoin() {
$table_data = Views::viewsData()->get($this->table);
$configuration = [
......@@ -70,12 +73,15 @@ class Natural extends SortPluginBase {
'field' => 'field',
'value' => $this->realField,
],
]
],
];
$join = Views::pluginManager('join')->createInstance('standard', $configuration);
return $join;
}
/**
* {@inheritdoc}
*/
protected function sortOptions() {
$options = parent::sortOptions();
$options['NASC'] = $this->t('Sort ascending naturally');
......@@ -87,24 +93,38 @@ class Natural extends SortPluginBase {
* {@inheritdoc}
*/
public function adminSummary() {
if (!empty($this->options['exposed'])) {
return $this->t('Exposed');
}
$label = parent::adminSummary();
switch ($this->options['order']) {
case 'NASC':
return $this->t('natural asc');
break;
case 'NDESC':
return $this->t('natural asc');
break;
default:
return $label;
break;
}
}
/**
* Determines if this query is natural sort.
*
* @return bool
* True if natural sort, False otherwise.
*/
public function isNaturalSort() {
return $this->isNaturalSort;
}
/**
* Sets the natural sort flag.
*
* @param bool $value
* The value.
*/
protected function setNaturalSort($value) {
$this->isNaturalSort = $value;
}
......
......@@ -43,7 +43,7 @@ function views_natural_sort_module_implements_alter(&$implements, $hook) {
function views_natural_sort_views_natural_sort_get_entry_types() {
$service = Drupal::service('views_natural_sort.service');
$supported_entity_properties = $service->getViewsSupportedEntityProperties();
$entry_types = [];
$entry_types = array();
foreach ($supported_entity_properties as $entity_type => $properties) {
foreach ($properties as $property => $schema_info) {
$entry_types = new IndexRecordType($entity_type, $property);
......@@ -55,25 +55,26 @@ function views_natural_sort_views_natural_sort_get_entry_types() {
/**
* Implements hook_views_natural_sort_queue_rebuild_data().
*/
function views_natural_sort_views_natural_sort_queue_rebuild_data(array $entry_types){
function views_natural_sort_views_natural_sort_queue_rebuild_data(IndexRecordType $entry_type){
$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();
$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);
}
if (empty($supported_entity_properties[$entity_type]) ||
empty($supported_entity_properties[$entity_type][$field])) {
return;
}
return ['views_natural_sort_entity_index'];
$queue = Drupal::queue('views_natural_sort_entity_index');
$query = Drupal::entityQuery($entity_type);
$entity_ids = $query->execute();
foreach ($entity_ids as $entity_id) {
$queue->createItem([
'entity_type' => $entity_type,
'entity_id' => $entity_id
]);
}
return 'views_natural_sort_entity_index';
}
/**
......@@ -141,13 +142,14 @@ function views_natural_sort_queue_data_for_rebuild(array $entry_types = []) {
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();
$queues = [];
foreach ($entry_types as $entry_type) {
$queues = array_unique(array_merge($queues, array_filter($moduleHandler->invokeAll('views_natural_sort_queue_rebuild_data', [$entry_type]))));
}
$operations = [];
foreach ($queues as $queue) {
$operations[] = [
'views_natural_sort_rebuild_index'
'views_natural_sort_rebuild_index',
[$queue],
];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment