Commit f4631ea0 authored by generalredneck's avatar generalredneck

Fixed views_data_alter and entry_types to look at properties on all entities.

parent 92da2ce6
name = Views Natural Sort
description = Sort results naturaly on a node's title skipping articles like "the" and "a."
description = Sort results naturaly on a node's title skipping articles like "the" and "a."
dependencies[] = views
dependencies[] = entity
package = Views
core = 7.x
......
......@@ -70,10 +70,44 @@ function views_natural_sort_views_api() {
* Implements hook_views_natural_sort_get_entry_types().
*/
function views_natural_sort_views_natural_sort_get_entry_types() {
return array(array(
'entity_type' => 'node',
'field' => 'title',
));
$supported_entity_properties = views_natural_sort_get_supported_entity_properties();
$entry_types = array();
foreach ($supported_entity_properties as $entity_type => $properties) {
foreach ($properties as $property => $schema_info) {
$entry_types[] = array(
'entity_type' => $entity_type,
'field' => $property,
);
}
}
return $entry_types;
}
function views_natural_sort_get_supported_entity_properties() {
$entity_property_info = entity_get_property_info();
$entity_info = entity_get_info();
$supported_properties = array();
foreach ($entity_property_info as $entity_type => $info) {
if (empty($supported_properties[$entity_type])) {
$supported_properties[$entity_type] = array();
}
$properties = $info['properties'];
foreach($properties as $property => $property_info) {
$property_info = array_merge($property_info, entity_property_info_defaults());
if ($property_info['type'] != 'text' || empty($property_info['schema field'])) {
continue;
}
$schema = drupal_get_schema($entity_info[$entity_type]['base table']);
if ($schema['fields'][$property_info['schema field']]['type'] != 'varchar') {
continue;
}
$supported_properties[$entity_type][$property] = array(
'base_table' => $entity_info[$entity_type]['base table'],
'schema_field' => $property_info['schema field'],
);
}
}
return $supported_properties;
}
/**
......@@ -152,7 +186,7 @@ function views_natural_sort_store_multiple(array $index_entries) {
*/
function views_natural_sort_store(array $index_entry) {
//This should take a formatted object and store it into the views_natural_sort table.
// This should take a formatted object and store it into the views_natural_sort table.
$string = views_natural_sort_transform($index_entry);
// The size limit on the content field for views_natual_sort is sometimes not
......
......@@ -7,7 +7,30 @@
* is most of the code.
*/
function views_natural_sort_views_data_alter(&$data) {
$data['node']['title']['sort']['handler'] = 'views_natural_sort_handler_sort';
function views_natural_sort_views_data_alter(&$views_data) {
$supported_entity_properties = views_natural_sort_get_supported_entity_properties();
$schema_fields = array();
foreach($supported_entity_properties as $entity => $properties) {
foreach($properties as $property => $schema_info) {
if (empty($schema_fields[$schema_info['base_table']])) {
$schema_fields[$schema_info['base_table']] = array();
}
$schema_fields[$schema_info['base_table']][] = $schema_info['schema_field'];
}
}
foreach ($views_data as $table => &$data) {
if (empty($schema_fields[$table])) {
continue;
}
foreach ($data as $view_field => &$view_field_data) {
if (isset($view_field_data['field']) &&
((isset($view_field_data['field']['field']) && in_array($view_field_data['field']['field'], $schema_fields[$table])) || in_array($view_field, $schema_fields[$table])) &&
$view_field_data['sort']['handler'] == 'views_handler_sort') {
// We've verified that this is a perfect candidate to add our sort
// handler to.
$view_field_data['sort']['handler'] = 'views_natural_sort_handler_sort';
}
}
}
}
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