? taxonomy_enhancer_d6.patch
? theme
? field_types/text/.svn
Index: taxonomy_enhancer.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_enhancer/taxonomy_enhancer.info,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 taxonomy_enhancer.info
--- taxonomy_enhancer.info 31 Jul 2008 15:53:45 -0000 1.2.2.1
+++ taxonomy_enhancer.info 11 Sep 2008 18:23:45 -0000
@@ -2,3 +2,4 @@
name = "Taxonomy Enhancer"
description = "Extend taxonomy terms with customizable fields"
package = "Taxonomy Enhancer"
+core = 6.x
Index: taxonomy_enhancer.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_enhancer/taxonomy_enhancer.install,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 taxonomy_enhancer.install
--- taxonomy_enhancer.install 31 Jul 2008 15:53:45 -0000 1.2.2.1
+++ taxonomy_enhancer.install 11 Sep 2008 18:23:45 -0000
@@ -7,25 +7,56 @@
*/
function taxonomy_enhancer_install() {
- switch ($GLOBALS['db_type']) {
- case 'mysqli':
- case 'mysql':
- $sql = 'CREATE TABLE {taxonomy_enhancer_fields} (
- vid int(11) unsigned NOT NULL,
- fid varchar(255) NOT NULL default "",
- title varchar(255) NOT NULL,
- module varchar(255) NOT NULL,
- type varchar(255) NOT NULL,
- weight int(11) NOT NULL default "0",
- settings text,
- PRIMARY KEY (vid,fid)
- ) /*!40100 DEFAULT CHARACTER SET utf8 */;';
+ drupal_install_schema('taxonomy_enhancer');
+}
- db_query($sql);
- break;
- }
+function taxonomy_enhancer_uninstall() {
+ drupal_uninstall_schema('taxonomy_enhancer');
}
+function taxonomy_enhancer_schema() {
+ $schema['taxonomy_enhancer_fields'] = array(
+ 'fields' => array(
+ 'vid' => array(
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ ),
+ 'fid' => array(
+ 'type' => 'varchar',
+ 'length' => '255',
+ 'not null' => TRUE,
+ ),
+ 'title' => array(
+ 'type' => 'varchar',
+ 'length' => '255',
+ 'not null' => TRUE
+ ),
+ 'module' => array(
+ 'type' => 'varchar',
+ 'length' => '255',
+ 'not null' => TRUE,
+ 'description' => 'taxonomy enhancer field provider module'
+ ),
+ 'type' => array(
+ 'type' => 'varchar',
+ 'length' => '255',
+ 'not null' => TRUE
+ ),
+ 'weight' => array(
+ 'type' => 'int',
+ 'not null' => TRUE,
+ 'default' => 0,
+ ),
+ 'settings' => array(
+ 'serialize' => TRUE,
+ 'type' => 'text',
+ ),
+ ),
+ 'primary key' => array('vid', 'fid'),
+ );
+ return $schema;
+}
function taxonomy_enhancer_update_10() {
$ret = array();
Index: taxonomy_enhancer.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/taxonomy_enhancer/taxonomy_enhancer.module,v
retrieving revision 1.2.2.4
diff -u -p -r1.2.2.4 taxonomy_enhancer.module
--- taxonomy_enhancer.module 31 Jul 2008 16:17:37 -0000 1.2.2.4
+++ taxonomy_enhancer.module 11 Sep 2008 18:23:45 -0000
@@ -9,33 +9,26 @@
*/
/**
- * Implementation of hook_init().
- *
- * This is used to check if any updates failed
- */
-function taxonomy_enhancer_init() {
- $level = variable_get('taxonomy_enhancer_update_fail', FALSE);
- if ($level !== FALSE) {
- db_query('UPDATE {system} SET schema_version = %d WHERE name = "taxonomy_enhancer"', $level);
- variable_del('taxonomy_enhancer_update_fail');
- }
-}
-
-
-/**
* Implementation of hook_form_alter().
*
* @param string $form_id
* @param array $form
*/
-function taxonomy_enhancer_form_alter($form_id, &$form) {
+function taxonomy_enhancer_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'taxonomy_form_vocabulary' :
- _taxonomy_enhancer_form_alter_vocabulary($form);
+ _taxonomy_enhancer_form_alter_vocabulary($form, $form_state, $form_id);
break;
case 'taxonomy_form_term' :
- _taxonomy_enhancer_form_alter_term($form);
+ _taxonomy_enhancer_form_alter_term($form, $form_state, $form_id);
+ break;
+
+ case 'taxonomy_manager_form':
+ //only add enhancer fields when the term data form is loaded
+ if (is_array($form['term_data']['tid'])) {
+ _taxonomy_enhancer_form_alter_term($form['term_data'], $form_state, $form_id);
+ }
break;
}
}
@@ -46,7 +39,8 @@ function taxonomy_enhancer_form_alter($f
*
* @param array $form
*/
-function _taxonomy_enhancer_form_alter_vocabulary(&$form) {
+function _taxonomy_enhancer_form_alter_vocabulary(&$form, &$form_state, $form_id) {
+
if (isset($form['vid']['#value'])) {
$vid = $form['vid']['#value'];
}
@@ -99,7 +93,6 @@ function _taxonomy_enhancer_form_alter_v
'#type' => 'textfield',
'#title' => t('Field Title'),
'#default_value' => $field->title,
- '#required' => TRUE,
'#description' => t('The title is the human-friendly identified of the field.'),
);
@@ -107,12 +100,13 @@ function _taxonomy_enhancer_form_alter_v
'#type' => 'select',
'#title' => t('Field Type'),
'#options' => taxonomy_enhancer_get_field_types(),
- '#default_value' => $field->type,
+ '#default_value' => array($field->module .'|'. $field->type),
'#description' => t('The type is used to define the data that this field represents, eg text, date, etc.'),
);
$form['fields']['created'][$id]['weight'] = array(
'#type' => 'weight',
'#title' => t('Field Weight'),
+ '#default_value' => $field->weight,
'#description' => t('The weight controls where this field appears in the Extended Terms form.'),
);
@@ -128,7 +122,7 @@ function _taxonomy_enhancer_form_alter_v
$form['fields']['new'] = array(
'#type' => 'fieldset',
'#title' => t('Add New Field'),
- '#validate' => array('_taxonomy_enhancer_validate_new' => array()),
+ '#element_validate' => array('_taxonomy_enhancer_validate_new'),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
);
@@ -179,22 +173,28 @@ function _taxonomy_enhancer_form_alter_v
*
* @param array $form
*/
-function _taxonomy_enhancer_validate_new($form) {
- if (empty($form['fid']['#value']) && empty($form['title']['#value'])) {
+function _taxonomy_enhancer_validate_new($form, &$form_state) {
+ $form_values = $form_state['values'];
+ $field_id = $form_values['fields']['new']['fid'];
+ $field_title = $form_values['fields']['new']['title'];
+
+ if (empty($field_id) && empty($field_title)) {
return;
}
-
- if (preg_match('|[^a-z0-9_\-]|', $form_values['fid']['#value'])) {
+ if (preg_match('|[^a-z0-9_\-]|', $field_id)) {
form_set_error('fields][new][fid', t('Name/Field ID must be lowercase alphanumeric & underscore only'));
}
-
- if (!empty($form['fid']['#value']) && empty($form['title']['#value'])) {
+ if (!empty($field_id) && empty($field_title)) {
form_set_error('fields][new][title', t('Both field Name and Title must be set when creating a new field'));
}
-
- if (empty($form['fid']['#value']) && !empty($form['title']['#value'])) {
+ if (empty($field_id) && !empty($field_title)) {
form_set_error('fields][new][fid', t('Both field Name and Title must be set when creating a new field'));
}
+ // Add the 'field_' prefix.
+ if (!empty($field_id) && substr($field_id, 0, 6) != 'field_') {
+ $field_id = 'field_'. $field_id;
+ form_set_value($form['fid'], $field_id, $form_state);
+ }
}
@@ -203,7 +203,7 @@ function _taxonomy_enhancer_validate_new
*
* @param array $form
*/
-function _taxonomy_enhancer_form_alter_term(&$form) {
+function _taxonomy_enhancer_form_alter_term(&$form, &$form_state, $form_id) {
if (isset($form['tid']['#value'])) {
$tid = $form['tid']['#value'];
$term = taxonomy_get_term($tid);
@@ -230,21 +230,20 @@ function _taxonomy_enhancer_form_alter_t
$form['fields'] = array_merge($form['fields'], $extra);
}
-
$form['submit']['#weight'] = $form['delete']['#weight'] = 10;
}
/**
* Public function to "load" the extended data for a term.
- * Note: Load is different to View! Load will attach the data in a more 'raw' form. The data is not ready for viewing.
+ * Note: Load is different to View! Load will attach the data in a more 'raw' form. The data is not ready for
+ * viewing.
*
* @param object $term
* Term object to be extended
*/
function taxonomy_enhancer_load_term(&$term) {
$fields = taxonomy_enhancer_get_fields_by_vocabulary($term->vid);
- $term->fields = array();
foreach ($fields as $field) {
$term->{$field->fid} = module_invoke($field->module, 'te_api', 'load', 'value', $field, $term);
@@ -252,6 +251,7 @@ function taxonomy_enhancer_load_term(&$t
}
+
/**
* Public function to extend a given term. This is effectively the "View" function.
* This will invoke the view hooks and generate the extended fields in a form ready for display.
@@ -261,12 +261,14 @@ function taxonomy_enhancer_load_term(&$t
*/
function taxonomy_enhancer_extend_term(&$term) {
$fields = taxonomy_enhancer_get_fields_by_vocabulary($term->vid);
- $term->fields = array();
-
+ $output = "";
foreach ($fields as $field) {
- $data = module_invoke($field->module, 'te_api', 'view', 'value', $field, $term);
- $term->{$field->fid} = taxonomy_enhancer_theme('taxonomy_enhanced_field', $field, $data);
+ $term->{$field->fid} = module_invoke($field->module, 'te_api', 'load', 'value', $field, $term);
+ module_invoke($field->module, 'te_api', 'view', 'value', $field, $term);
+ $output .= theme('taxonomy_enhancer_field',$field, $term)."
";
}
+
+ return $output;
}
@@ -279,104 +281,99 @@ function taxonomy_enhancer_extend_term(&
*/
function taxonomy_enhancer_taxonomy($op, $type, $array = NULL) {
switch ($type) {
- case 'vocabulary' :
- switch ($op) {
- case 'update' :
- if (!empty($array['fields']['new']['fid'])) {
- //Get the module and field type from the concatendated form type field.
- list($module, $field_type) = explode('|', $array['fields']['new']['type']);
-
- //Build a form array for inserting and module_invoking...ing.
- $field = array(
- 'vid' => $array['vid'],
- 'fid' => $array['fields']['new']['fid'],
- 'title' => $array['fields']['new']['title'],
- 'module' => $module,
- 'type' => $field_type,
- 'weight' => $array['fields']['new']['weight'],
- 'settings' => serialize(array()),
- );
+ case 'vocabulary':
+ _taxonomy_enhancer_vocabulary($op, $array);
+ break;
- //Create the entry in the DB
- db_query('INSERT INTO {taxonomy_enhancer_fields} (vid, fid, title, module, type, weight, settings) VALUES(%d, "%s", "%s", "%s", "%s", %d, "%s")', $field);
+ case 'term':
+ _taxonomy_enhancer_term($op, $array);
+ break;
+ }
+}
- //Invoke the module's hook
- module_invoke($module, 'te_api', 'insert', 'field', $field);
- }
+function _taxonomy_enhancer_term($op, $term) {
+ $fields = taxonomy_enhancer_get_fields_by_vocabulary($term['vid']);
- //Handle updating of fields...
- if (isset($array['fields']['created']) && is_array($array['fields']['created'])) {
- foreach ($array['fields']['created'] as $fid => $field) {
- //Get the module and type from the concatenated field (eg, 'taxonomy_enhancer_text|text'
- list($module, $type) = explode('|', $field['type']);
-
- //Compare the old field's module and type to the submitted field. If its changed then blank out the settings.
- $old_field = taxonomy_enhancer_get_field($array['vid'], $field['fid']);
- if ($old_field->module == $module && $old_field->type = $type) {
- $settings = module_invoke($module, 'te_api', 'update', 'field', $field);
- }
- else {
- $settings = array();
- }
-
- //Write the field.
- db_query(
- 'UPDATE {taxonomy_enhancer_fields} SET title = "%s", module = "%s", type = "%s", weight = %d, settings = "%s" WHERE vid = %d AND fid = "%s"',
- $field['title'], $module, $type, $field['weight'], serialize($settings), $array['vid'], $field['fid']
- );
- }
- }
+ foreach ($fields as $field) {
+ module_invoke($field->module, 'te_api', $op, 'value', $field, $term);
+ }
+}
- //Delete any checked fields
- $delete = array_filter($array['fields']['delete']['fields']);
- if (!empty($delete)) {
- foreach ($delete as $fid) {
- $field = taxonomy_enhancer_get_field($array['vid'], $fid);
- module_invoke($field->module, 'te_api', 'delete', 'field', $field);
- db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $array['vid'], $field->fid);
- }
- }
- break;
+function _taxonomy_enhancer_vocabulary($op, $vocabulary) {
+ switch ($op) {
+ case 'update' :
+ if (!empty($vocabulary['fields']['new']['fid'])) {
+ //Get the module and field type from the concatendated form type field.
+ list($module, $field_type) = explode('|', $vocabulary['fields']['new']['type']);
+
+ //Build a form array for inserting and module_invoking...ing.
+ $field = array(
+ 'vid' => $vocabulary['vid'],
+ 'fid' => $vocabulary['fields']['new']['fid'],
+ 'title' => $vocabulary['fields']['new']['title'],
+ 'module' => $module,
+ 'type' => $field_type,
+ 'weight' => $vocabulary['fields']['new']['weight'],
+ 'settings' => array(),
+ );
+ //Create the entry in the DB
+ drupal_write_record('taxonomy_enhancer_fields', $field);
- case 'delete' :
- $fields = taxonomy_enhancer_get_fields_by_vocabulary($array['vid']);
- foreach ($fields as $field) {
- module_invoke($field->module, 'te_api', 'delete', 'field', $field);
- db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $array['vid'], $field->fid);
- }
- break;
+ //Invoke the module's hook
+ module_invoke($module, 'te_api', 'insert', 'field', $field);
}
- break;
-
- case 'term' :
- $fields = taxonomy_enhancer_get_fields_by_vocabulary($array['vid']);
-
- switch ($op) {
- case 'update' :
- foreach ($fields as $field) {
- module_invoke($field->module, 'te_api', 'update', 'value', $field, $array);
+ //Handle updating of fields...
+ if (isset($vocabulary['fields']['created']) && is_array($vocabulary['fields']['created'])) {
+ foreach ($vocabulary['fields']['created'] as $fid => $field) {
+ //Get the module and type from the concatenated field (eg, 'taxonomy_enhancer_text|text'
+ list($module, $type) = explode('|', $field['type']);
+
+ //Compare the old field's module and type to the submitted field. If its changed then blank out the settings.
+ $old_field = taxonomy_enhancer_get_field($vocabulary['vid'], $field['fid']);
+ if ($old_field->module == $module && $old_field->type == $type) {
+ $settings = module_invoke($module, 'te_api', 'update', 'field', $field);
}
- break;
-
- case 'insert' :
- foreach ($fields as $field) {
- module_invoke($field->module, 'te_api', 'insert', 'value', $field, $array);
+ else {
+ $settings = array();
}
- break;
- case 'delete' :
- foreach ($fields as $field) {
- module_invoke($field->module, 'te_api', 'delete', 'value', $field, $array);
+ //Write the field.
+ $record->title = $field['title'];
+ $record->module = $module;
+ $record->type = $type;
+ $record->weight = $field['weight'];
+ $record->settings = $settings;
+ $record->vid = $vocabulary['vid'];
+ $record->fid = $field['fid'];
+ drupal_write_record('taxonomy_enhancer_fields', $record, array('vid', 'fid'));
+ }
+ }
+
+ //Delete any checked fields
+ if ($vocabulary['fields']['delete']['fields']) {
+ $delete = array_filter($vocabulary['fields']['delete']['fields']);
+ if (!empty($delete)) {
+ foreach ($delete as $fid) {
+ $field = taxonomy_enhancer_get_field($vocabulary['vid'], $fid);
+ module_invoke($field->module, 'te_api', 'delete', 'field', $field);
+ db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $vocabulary['vid'], $field->fid);
}
- break;
+ }
+ }
+ break;
+
+ case 'delete' :
+ $fields = taxonomy_enhancer_get_fields_by_vocabulary($vocabulary['vid']);
+ foreach ($fields as $field) {
+ module_invoke($field->module, 'te_api', 'delete', 'field', $field);
+ db_query('DELETE FROM {taxonomy_enhancer_fields} WHERE vid = %d AND fid = "%s"', $vocabulary['vid'], $field->fid);
}
break;
}
}
-
/**
* Get an individual field identified by Vocabulary ID and Field ID
*
@@ -388,15 +385,15 @@ function taxonomy_enhancer_taxonomy($op,
* Array of field objects
*/
function taxonomy_enhancer_get_field($vid, $fid) {
- static $field_cache;
+ static $fields;
- if (!isset($field_cache[$vid][$fid])) {
- $field_cache[$vid][$fid] = db_fetch_object(db_query('SELECT * FROM {taxonomy_enhancer_fields} f WHERE vid = %d AND fid = "%s"', $vid, $fid));
- $field_cache[$vid][$fid] = drupal_unpack($field_cache[$vid][$fid], 'settings');
- unset($field_cache[$vid][$fid]->setttings);
+ if (!isset($fields[$vid][$fid])) {
+ $fields[$vid][$fid] = db_fetch_object(db_query('SELECT * FROM {taxonomy_enhancer_fields} f WHERE vid = %d AND fid = "%s"', $vid, $fid));
+ $fields[$vid][$fid] = drupal_unpack($fields[$vid][$fid], 'settings');
+ unset($fields[$vid][$fid]->setttings);
}
- return $field_cache[$vid][$fid];
+ return $fields[$vid][$fid];
}
@@ -425,7 +422,6 @@ function taxonomy_enhancer_get_fields_by
$fields[$vid] = $temp;
}
-
return $fields[$vid];
}
@@ -435,6 +431,7 @@ function taxonomy_enhancer_get_fields_by
* - Returns -1 (negative one) if 'A' is less than 'B'
* - Returns 1 (positive one) if 'A' is more than 'B'
* - Returns 0 (zero) if 'A' and 'B' are identical
+ * ripped from _user_sort.
*
* @param object $a
* Field Object A
@@ -444,18 +441,9 @@ function taxonomy_enhancer_get_fields_by
* Comparison result
*/
function _taxonomy_enhancer_field_sort($a, $b) {
- if ($a->weight < $b->weight) {
- return -1;
- }
- elseif ($aweight > $bweight) {
- return 1;
- }
- elseif (isset($a->title) && isset($b->title)) {
- return strnatcasecmp($a->title, $b->title);
- }
- else {
- return 1;
- }
+ $a = (array)$a + array('weight' => 0, 'title' => '');
+ $b = (array)$b + array('weight' => 0, 'title' => '');
+ return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : ($a['title'] < $b['title'] ? -1 : 1));
}
@@ -468,7 +456,6 @@ function _taxonomy_enhancer_field_sort($
function taxonomy_enhancer_get_field_types() {
$items = array();
-
foreach (module_implements('te_api') as $module) {
$result = module_invoke($module, 'te_api', 'list', 'field');
@@ -483,51 +470,114 @@ function taxonomy_enhancer_get_field_typ
}
+
+
+//TODO: may change themeing
+//TODO: may need to sort on type
/**
- * Theme function inspired by/leeched from Views (views_theme)
- *
- * Easily theme any item to an enhanced term field.
- * @param $function
- * The name of the function to call.
- * @param $field
- * The field being themed.
- * @param $data
- * The data being outputted
+ * taxonomy_enhancer_theme is takes a term object and renders formatted content
+ * this is called in the theme_taxonomy_term_page function, which should be placed in the theme.
*/
-function taxonomy_enhancer_theme() {
- $args = func_get_args();
- $function = array_shift($args);
- $field = $args[0];
+//
+function taxonomy_enhancer_theme(&$term) {
+ $path = drupal_get_path('module', 'taxonomy_enhanced') .'/theme';
+ return array(
+ 'taxonomy_enhancer_field' => array(
+ // 'template' => 'taxonomy-enhanced-field', //TODO: escaped for now need to fix the path
+ //TODO: if object changes we may just need null to execute this
+ //'arguments' => array('form' => array()),
+ 'arguments' => array('form' => NULL),//TODO may need argument here
+ 'path' => $path,
+ ),
+ );
+}
- if (!($func = theme_get_function($function .'_'. $field->fid))) {
- $func = theme_get_function($function);
- }
- if ($func) {
- return call_user_func_array($func, $args);
+function theme_taxonomy_enhancer_field($field, $term) {
+ //$field comes from $term object, but renders like cck fields, so we are changing the terminology here
+ //$field contains attributes of the field; $term contains the content with the field as an array item
+ $fid = $field->fid;
+ //can specify 'value' or 'safe' for the type
+ $field_output = taxonomy_enhancer_content_format($fid, $term->{$fid}, 'safe'); //is an array
+ $output = "";
+ if(!empty($field_output)){
+ $output .= "
".$field->title."
"; + $output .= "".$field_output."
"; } + return $output; } + /** - * Default Theme function for a generic field. + * Format a field item for display. + * Based on CCK content_format, but reduced to match up with taxonomy_enhancer text module capabilities * - * @param object $field - * Field object - * @param string $data - * String to be outputted - * @return string - * Rendered Output - */ -function theme_taxonomy_enhanced_field($field, $data) { - switch ($field->format) { - case 'raw' : - return $data; - default : - return '