Commit a88662fc authored by generalredneck's avatar generalredneck

WIP: Start of the move of functionality to D8

 This is a non-working commit that gets the ball going on some of
 the translation that is needed to make this work. Testing with
 Titles on Nodes first.
parent 3c2e1e7f
<?php
namespace Drupal\views_natural_sort\Plugin\views\sort;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\sort\SortPluginBase;
/**
* Sort plugin used to allow Natural Sorting.
*
* @ingroup views_sort_handlers
*
* @ViewsSort("natural")
*/
class Natural extends SortPluginBase {
/**
*
*/
protected $isNaturalSort;
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->setNaturalSort(substr($this->options['order'],0,1) == 'N');
}
public function query() {
// If this field isn't being used as a Natural Sort Field, move along
// nothing to see here.
if (!$this->isNaturalSort()) {
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->table_alias;
if (empty($this->query->relationships[$vns_alias])) {
$this->ensure_my_table();
$vns_alias = $this->query->add_relationship('vns_' . $this->table_alias, $this->natural_sort_join(), $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->add_orderby($vns_alias, 'content', $order);
}*/
}
protected function sortOptions() {
$options = parent::sortOptions();
$options['NASC'] = $this->t('Sort ascending naturally');
$options['NDESC'] = $this->t('Sort descending naturally');
}
/**
* {@inheritdoc}
*/
public function adminSummary() {
$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;
}
}
public function isNaturalSort() {
return $this->isNaturalSort;
}
protected function setNaturalSort($value) {
$this->isNaturalSort = $value;
}
}
name: Views Natural Sort
type: module
description: Sort results naturaly on a node's title skipping articles like "the" and "a."
core: 8.x
package: Views
dependencies:
- views
<?php
// $Id$
/**
* @file
* Provides a views filter that sorts titles by a more natural manner by
* ignoring articles like "The" and "A."
*
* Normal sort:
* A Chorus Line
* All American
* Fiddler on the Roof
* Oklahoma!
* The King And I
*
* Natural sort:
* All American
* A Chorus Line
* Fiddler on the Roof
* The King And I
* Oklahoma!
*/
/**
* Implementation of hook_menu().
* @file
* Contains views_natural_sort.module..
*/
function views_natural_sort_menu() {
//
$items = array();
$items['admin/settings/views_natural_sort'] = array(
'title' => 'Views Natural Sort',
'description' => 'Provides options for what should be indexed for sorting.',
'page callback' => 'drupal_get_form',
'page arguments' => array('views_natural_sort_settings_form'),
'access callback' => 'user_access',
'access arguments' => array('administer views'),
'file' => 'views_natural_sort.admin.inc',
'type' => MENU_NORMAL_ITEM
);
return $items;
}
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implementation of hook_views_api().
* Implements hook_help().
*/
function views_natural_sort_views_api() {
return array(
'api' => 2.0,
);
}
/**
* Implementation of hook_nodeapi().
*
* This keeps our natural sort index up to date.
*/
function views_natural_sort_nodeapi(&$node, $op) {
//
switch ($op) {
case 'update':
case 'insert':
_views_natural_sort_store_node($node);
break;
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 = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Sort results naturaly on a node&#039;s title skipping articles like &quot;the&quot; and &quot;a.&quot;') . '</p>';
return $output;
default:
}
}
/**
* Helper function for writing node data to our sort index.
*
* @param $node
* A drupal node object containing at least a nid and title.
*/
function _views_natural_sort_store_node($node) {
$record = new stdClass();
$record->nid = $node->nid;
$record->field = 'title';
$record->content = _views_natural_sort_filter_content($node->title);
// Try to update. On fail, try inserting.
$return = drupal_write_record('views_natural_sort', $record, array('nid', 'field'));
if (!db_affected_rows()) {
$return = drupal_write_record('views_natural_sort', $record);
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;
}
return $return;
}
/**
* Helper function for filtering out what the value we should sort on is.
* @param $content
* Original content.
* @return
* Filtered value.
*/
function _views_natural_sort_filter_content($content) {
return preg_replace('/^(The|A)\s/i', '', $content);
}
<?php
// $Id$
/**
* @file
* Views related hooks.
*
* Automatically included by views. Because of the nature of this module this
* is most of the code.
*/
/**
* Implementation of hook_views_data().
* Implements hook_views_data_alter().
*/
function views_natural_sort_views_data() {
$data['views_natural_sort']['table']['join'] = array(
'node' => array(
'left_field' => 'nid',
'field' => 'nid',
// We only support sorting on title so go ahead and build filter it into the join.
'extra' => array(
array(
'field' => 'field',
'value' => 'title',
'operator' => '=',
)
)
),
);
$data['views_natural_sort']['content'] = array(
'title' => 'Title - Natural',
'group' => t('Node'),
'help' => t('Sort title using a natural sort.'),
'sort' => array(
'handler' => 'views_handler_sort',
),
);
return $data;
function views_natural_sort_views_data_alter(array &$data) {
$data['node_field_data']['title']['sort']['id'] = 'natural';
}
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