Commit ea124aa9 authored by generalredneck's avatar generalredneck

Updating module to allow every single varchar property on every entity.

parent a98f6d49
...@@ -80,79 +80,65 @@ function views_natural_sort_views_natural_sort_get_entry_types() { ...@@ -80,79 +80,65 @@ function views_natural_sort_views_natural_sort_get_entry_types() {
return $entry_types; 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;
}
/** /**
* Implements hook_views_natural_sort_get_rebuild_data(); * Implements hook_views_natural_sort_get_rebuild_data();
*/ */
function views_natural_sort_views_natural_sort_get_rebuild_data($entry_type){ function views_natural_sort_views_natural_sort_get_rebuild_data($entry_type){
if($entry_type['entity_type'] != 'node' || $entry_type['field'] != 'title') { $supported_entity_properties = views_natural_sort_get_supported_entity_properties();
if (empty($supported_entity_properties[$entry_type['entity_type']]) ||
empty($supported_entity_properties[$entry_type['entity_type']][$entry_type['field']])) {
return array(); return array();
} }
module_load_include('inc', 'views_natural_sort', 'views_natural_sort.node'); $entities = entity_load($entry_type['entity_type']);
$result = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->execute();
$data = array(); $data = array();
foreach ($result as $row ) { foreach($entities as $eid => $entity) {
$data[] = _views_natural_sort_node_to_vns($row); $data[] = array(
'eid' => $eid,
'entity_type' => $entry_type['entity_type'],
'field' => $entry_type['field'],
'delta' => 0,
'content' => $entity->$entry_type['field'],
);
} }
return $data; return $data;
} }
/** /**
* Implements hook_node_insert(). * Implements hook_entity_insert().
* *
* This keeps our natural sort index up to date. * This keeps our natural sort index up to date.
*/ */
function views_natural_sort_node_insert($node) { function views_natural_sort_entity_insert($entity, $type) {
module_load_include('inc', 'views_natural_sort', 'views_natural_sort.node'); $supported_entity_properties = views_natural_sort_get_supported_entity_properties();
_views_natural_sort_store_node($node); if (empty($supported_entity_properties[$type])) {
return;
}
foreach ($supported_entity_properties[$type] as $property => $property_info) {
views_natural_sort_store(views_natural_sort_entity_to_vns($entity, $type, $property));
}
} }
/** /**
* Implementation of hook_node_update(). * Implementation of hook_entity_update().
* *
* This keeps our natural sort index up to date. * This keeps our natural sort index up to date.
*/ */
function views_natural_sort_node_update($node) { function views_natural_sort_entity_update($entity, $type) {
module_load_include('inc', 'views_natural_sort', 'views_natural_sort.node'); views_natural_sort_entity_insert($entity, $type);
_views_natural_sort_store_node($node);
} }
/** /**
* Implementation of hook_node_delete(). * Implementation of hook_entity_delete().
* *
* This keep sour natural sort index clean. * This keep sour natural sort index clean.
*/ */
function views_natural_sort_node_delete($node) { function views_natural_sort_entity_delete($entity, $type) {
module_load_include('inc', 'views_natural_sort', 'views_natural_sort.node'); $entity_info = entity_get_info($entity_type);
_views_natural_sort_remove_node($node); $id_field = $entity_info['entity_keys']['id'];
views_natural_sort_remove($entry = array(
'eid' => $entity->$id_field,
'entity_type' => $type,
));
} }
/** /**
...@@ -217,17 +203,22 @@ function views_natural_sort_store(array $index_entry) { ...@@ -217,17 +203,22 @@ function views_natural_sort_store(array $index_entry) {
* Mirrors the views_natural_sort table * Mirrors the views_natural_sort table
* $eid - Entity Id of the item referenced * $eid - Entity Id of the item referenced
* $entity_type - The Entity Type. Ex. node * $entity_type - The Entity Type. Ex. node
* $field - reference to the property or field name * $field - (optional) reference to the property or field name
* $delta - the item number in that field or property * $delta - (optional)the item number in that field or property
* If an optional parameter doesn't exist, this is treated as a wild care
* delete
*/ */
function views_natural_sort_remove($index_entry) { function views_natural_sort_remove($index_entry) {
//TODO: Make sure all the keys are in the index_entry. $query = db_delete('views_natural_sort')
return db_delete('views_natural_sort')
->condition('eid', $index_entry['eid']) ->condition('eid', $index_entry['eid'])
->condition('entity_type', $index_entry['entity_type']) ->condition('entity_type', $index_entry['entity_type']);
->condition('field', $index_entry['field']) if (isset($index_entry['field'])) {
->condition('delta', $index_entry['delta']) $query->condition('field', $index_entry['field']);
->execute(); }
if (isset($index_entry['delta'])) {
$query->condition('delta', $index_entry['delta']);
}
$query->execute();
} }
/** /**
...@@ -286,3 +277,49 @@ function views_natural_sort_get_transformations($index_entry) { ...@@ -286,3 +277,49 @@ function views_natural_sort_get_transformations($index_entry) {
drupal_alter('views_natural_sort_transformations', $transformations, $index_entry); drupal_alter('views_natural_sort_transformations', $transformations, $index_entry);
return $transformations; return $transformations;
} }
function views_natural_sort_get_supported_entity_properties() {
$supported_properties = &drupal_static(__FUNCTION__, array());
if (empty($supported_properties)) {
$entity_property_info = entity_get_property_info();
$entity_info = entity_get_info();
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;
}
function views_natural_sort_entity_to_vns($entity, $entity_type, $field) {
$supported_entity_properties = views_natural_sort_get_supported_entity_properties();
if (empty($supported_entity_properties[$entity_type]) ||
empty($supported_entity_properties[$entity_type][$field])) {
throw new Exception("$entity_type -> $field doesn't exist. Cannot create Views Natural Sort record");
}
$entity_info = entity_get_info($entity_type);
$id_field = $entity_info['entity keys']['id'];
return array(
'eid' => $entity->$id_field,
'entity_type' => $entity_type,
'field' => $field,
'delta' => 0,
'content' => $entity->$field,
);
}
<?php
/**
* @file
*
* Node specific views_natural_sort code.
*/
/**
* 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) {
views_natural_sort_store(_views_natural_sort_node_to_vns($node));
}
/**
* Helper function for getting an index_entry from a node object.
*
* @param stdClass $node
* Node Entity
*
* @return array
* index_entry array
*/
function _views_natural_sort_node_to_vns($node) {
return array(
'eid' => $node->nid,
'entity_type' => 'node',
'field' => 'title',
'delta' => 0,
'content' => $node->title,
);
}
/**
* Helper function for removing node data from our sort index.
*
* @param $node
* A drupal node object containing at least a nid.
*
* @return int
* The number of rows deleted.
*/
function _views_natural_sort_remove_node($node) {
return views_natural_sort_remove(array(
'eid' => $node->nid,
'entity_type' => 'node',
'field' => 'title',
'delta' => 0,
));
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
function views_natural_sort_text_field_views_api() { function views_natural_sort_text_field_views_api() {
return array( return array(
'api' => 2.0, 'api' => 3.0,
); );
} }
......
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