Commit 96cc034b authored by Cody Craven's avatar Cody Craven
Browse files

Increased functionality more towards a working release. Taxonomy term display...

Increased functionality more towards a working release. Taxonomy term display is now saveable, hookable, and editable.
parent 98dd4eb7
// $Id$
Taxonomy Display 7.x-dev, 2011-03-02
Taxonomy Display Sandbox
------------------------------
Made a bunch of commits no one cares about, it is a sandbox module after all.
Added CHANGELOG.txt
Initial commit.
\ No newline at end of file
......@@ -14,26 +14,39 @@ abstract class TaxonomyDisplayHandlerForm {
*
* @param array $form
* A fieldset for configuration to display to the user for this handler.
* @param array $form_state
* The form state.
* @param array $values
* The values from the sub-form taken from $form_state, note any alterations
* will affect the values in $form_state.
* @param mixed $options
* The data that was stored on behalf of the handler resulting from the
* return of the TaxonomyDisplayHandlerForm::formSubmit implementation.
*
* @return TaxonomyTermDisplayHandler
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
abstract public function formFieldset(&$form, &$form_state);
abstract public function formFieldset(&$form, &$values, $options = NULL);
/**
* Perform any processing desired and return the result to be stored.
*
* @param array $form
* A fieldset for configuration to display to the user for this handler.
* @param array $form_state
* The form state.
* A fieldset containing the sub-form rendered to the user.
* @param array $values
* The values from the sub-form taken from $form_state, note any alterations
* will affect the values in $form_state.
*
* @return mixed
* Any single variable or no variable can be returned. Whatever is returned
* will be stored on behalf of the plugin for later use.
*
* @see TaxonomyDisplayHandlerForm::formFieldset()
*/
abstract public function formSubmit(&$form, &$form_state);
public function formSubmit($form, &$values) {
// Override formSubmit in your implementing handler if any data is to be
// stored on behalf of your handler on submit.
return;
}
/**
* Perform validation on the custom formFieldset when the user submits.
......@@ -42,16 +55,24 @@ abstract class TaxonomyDisplayHandlerForm {
* validation can be skipped in some cases.
*
* @param array $form
* A fieldset for configuration to display to the user for this handler.
* @param array $form_state
* The form state.
* A fieldset containing the sub-form rendered to the user.
* @param array $values
* The values from the sub-form taken from $form_state, note any alterations
* will affect the values in $form_state.
*
* @return TaxonomyTermDisplayHandler
*
* @see TaxonomyDisplayHandlerForm::formFieldset()
*/
public function formValidate(&$form, &$form_state) {
public function formValidate($form, &$values) {
// Override formValidate in your implementing handler if any validation work
// is to be done.
if (FALSE) {
// This is an example of how you can set an error on one of your form
// elements with the key 'example_element'.
form_set_error(implode('][', $form['example_element']['#parents']), t('Error message'));
}
return $this;
}
}
\ No newline at end of file
......@@ -8,9 +8,9 @@ abstract class TaxonomyDisplayTermDisplayHandler extends TaxonomyDisplayHandlerF
/**
* Render the output to be displayed when the user views the taxonomy term.
*
* @param $term
* @param termObject $term
* The taxonomy term to be displayed.
* @param $options
* @param mixed $options
* The data that was stored on behalf of the handler resulting from the
* return of the TaxonomyDisplayHandlerForm::formSubmit implementation.
*
......@@ -19,5 +19,5 @@ abstract class TaxonomyDisplayTermDisplayHandler extends TaxonomyDisplayHandlerF
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
abstract public function displayTerm($term, $options);
abstract public function displayTerm($term, $options = NULL);
}
......@@ -10,8 +10,31 @@ class TaxonomyDisplayTermDisplayHandlerCore extends TaxonomyDisplayTermDisplayHa
*
* @see TaxonomyDisplayTermDisplayHandler::displayTerm()
*/
public function displayTerm($term, $options) {
return '';
public function displayTerm($term, $options = NULL) {
$build = array();
// Allow display of term's additional content even if the description field
// is blank, if the option is enabled.
// @see http://drupal.org/node/796692
if ($options && $options['display_fields_description_empty']) {
$build = array(
'#prefix' => '<div class="term-listing-heading">',
'#suffix' => '</div>',
'term' => taxonomy_term_view($term, 'full'),
);
if (empty($term->description)) {
unset($build['description']);
}
}
elseif (!empty($term->description)) {
$build = array(
'#prefix' => '<div class="term-listing-heading">',
'#suffix' => '</div>',
'term' => taxonomy_term_view($term, 'full'),
);
}
return $build;
}
/**
......@@ -19,16 +42,27 @@ class TaxonomyDisplayTermDisplayHandlerCore extends TaxonomyDisplayTermDisplayHa
*
* @see TaxonomyDisplayHandlerForm::formFieldset()
*/
public function formFieldset(&$form, &$form_state) {
$form['#description'] = t('Hit!');
public function formFieldset(&$form, &$values, $options = NULL) {
$form['#description'] = t('The core Drupal functionality will be used to display the term.');
$form['display_fields_description_empty'] = array(
'#default_value' => isset($options['display_fields_description_empty']) ? $options['display_fields_description_empty'] : 0,
'#description' => t('If a taxonomy term does not have a description entered Drupal core will not display any of its fields. Selecting <em>yes</em> will overcome <a href="!link">this problem</a>.', array('!link' => url('http://drupal.org/node/796692'))),
'#options' => array(t('No'), t('Yes')),
'#title' => t('Render term when the description field is empty?'),
'#type' => 'radios',
);
}
/**
* We do not want anything stored so return null.
* We store values to access later for rendering and editing.
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
public function formSubmit(&$form, &$form_state) {
return;
public function formSubmit($form, &$values) {
// We are using the exact keys that our formFieldset() implementation
// defines and we want all of the values stored, so we have no need to alter
// them before returning.
return $values;
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ class TaxonomyDisplayTermDisplayHandlerHidden extends TaxonomyDisplayTermDisplay
*
* @see TaxonomyDisplayTermDisplayHandler::displayTerm()
*/
public function displayTerm($term, $options) {
public function displayTerm($term, $options = NULL) {
return '';
}
......@@ -19,8 +19,8 @@ class TaxonomyDisplayTermDisplayHandlerHidden extends TaxonomyDisplayTermDisplay
*
* @see TaxonomyDisplayHandlerForm::formFieldset()
*/
public function formFieldset(&$form, &$form_state) {
$form['#description'] = t('By selecting this option the term will not be displayed to the user when viewed. Leaving only the title and associated content.');
public function formFieldset(&$form, &$values, $options = NULL) {
$form['#description'] = t('The term will not be displayed to the user when viewed. Leaving only the title and associated content.');
}
/**
......@@ -28,7 +28,5 @@ class TaxonomyDisplayTermDisplayHandlerHidden extends TaxonomyDisplayTermDisplay
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
public function formSubmit(&$form, &$form_state) {
return;
}
public function formSubmit($form, &$values) {}
}
\ No newline at end of file
......@@ -5,8 +5,8 @@ core = 7.x
dependencies[] = taxonomy
files[] = taxonomy_display.module
; Our abstract classes for plugins
files[] = classes/handler_form.inc
files[] = classes/associated_display.inc
files[] = classes/handler_form.inc
files[] = classes/term_display.inc
; Our class handlers for plugins
; - For term displays
......
......@@ -29,7 +29,7 @@ function taxonomy_display_enable() {
// If the vocabulary does not exist delete the taxonomy_display as it is now
// an orphan.
if (!isset($vocabularies[$taxonomy_display->machine_name])) {
taxonomy_display_taxonomy_vocabulary_delete($taxonomy_display->machine_name);
taxonomy_display_delete_taxonomy_dislpay($taxonomy_display->machine_name);
}
}
}
......
......@@ -15,6 +15,57 @@ function taxonomy_display_ajax_term_display_callback($form, $form_state) {
return $form['additional_settings']['taxonomy_display']['term_display_form'];
}
/**
* Helper function; delete taxonomy display data.
*
* @param string $machine_name
* The machine name of the vocabulary's taxonomy display data to remove.
* @param string|NULL|FALSE $watchdog_message
* Provide a watchdog message as a string. If null then a generic default
* message will be used. If false then no watchdog message will be recorded.
* @param array $watchdog_variables
* Variables for substitution in the watchdog message.
* @param int $watchdog_severity
* One of the defined watchdog constant severities: WATCHDOG_EMERGENCY,
* WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING,
* WATCHDOG_NOTICE, WATCHDOG_INFO, WATCHDOG_DEBUG. Defaults to
* WATCHDOG_NOTICE.
*
* @return void
*/
function taxonomy_display_delete_taxonomy_dislpay($machine_name, $watchdog_message = NULL, $watchdog_variables = array(), $watchdog_severity = WATCHDOG_NOTICE) {
// Delete our display record for the term being removed.
$count = (bool) db_delete('taxonomy_display')
->condition('machine_name', $machine_name)
->execute();
// If a record was deleted then log it in watchdog.
if ($count && $watchdog_message !== FALSE) {
if (empty($watchdog_message)) {
$watchdog_message = 'Taxonomy display settings deleted for %name.';
$watchdog_variables = array('%name' => $machine_name);
}
watchdog('taxonomy_display', $watchdog_message, $watchdog_variables, $watchdog_severity);
}
}
/**
* Helper function; retrieve taxonomy display settings from the database.
*
* @param string $machine_name
* The machine name of the vocabulary's taxonomy display data to fetch.
*
* @return array|FALSE
* Return associated array if a record is found, else false.
*/
function taxonomy_display_fetch_taxonomy_display($machine_name) {
return db_select('taxonomy_display', 'td')
->fields('td')
->condition('machine_name', $machine_name)
->execute()
->fetchAssoc();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
......@@ -51,11 +102,7 @@ function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$fo
return;
}
$stored_settings = db_select('taxonomy_display', 'td')
->fields('td')
->condition('machine_name', $form['#bundle'])
->execute()
->fetchAssoc();
$stored_settings = taxonomy_display_fetch_taxonomy_display($form['#bundle']);
$form['additional_settings']['taxonomy_display'] = array(
'#description' => 'Configure how the taxonomy term display page should be presented when viewed for this vocabulary.',
......@@ -64,17 +111,16 @@ function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$fo
);
$fieldset =& $form['additional_settings']['taxonomy_display'];
if (isset($form_state['values']['additional_settings']['taxonomy_display'])) {
$state_values =& $form_state['values']['additional_settings']['taxonomy_display'];
$values =& $form_state['values']['additional_settings']['taxonomy_display'];
}
// Populate $options with plugins that are available.
$options = taxonomy_display_plugins('term');
// TODO: Rewrite supporting missing plugins.
// Discover which option should be selected from term display plugins.
if (isset($state_values) && isset($state_values['term_display_select']) &&
array_key_exists($state_values['term_display_select'], $options)) {
$selected = $state_values['term_display_select'];
if (isset($values) && isset($values['term_display_select']) &&
array_key_exists($values['term_display_select'], $options)) {
$selected = $values['term_display_select'];
}
elseif (isset($stored_settings['term_display_plugin']) &&
array_key_exists($stored_settings['term_display_plugin'], $options)) {
......@@ -86,7 +132,7 @@ function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$fo
$fieldset['term_display_select'] = array(
'#ajax' => array(
'callback' => 'taxonomy_display_ajax_term_display_callback',
'wrapper' => 'term_display_form_replace',
'wrapper' => 'replace-term-display-form',
),
'#default_value' => $selected,
'#description' => t('Select which plugin you would like to display the term\'s content.'),
......@@ -95,27 +141,71 @@ function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$fo
'#type' => 'select',
);
if (isset($form_state['storage']['term_display_last_handler'])) {
$last_handler = $form_state['storage']['term_display_last_handler'];
if (isset($form_state['taxonomy_display']['term_display_handler'])) {
$last_handler = $form_state['taxonomy_display']['term_display_handler'];
}
$form_state['storage']['term_display_last_handler'] = $selected;
$form_state['taxonomy_display']['term_display_handler'] = $selected;
$fieldset['term_display_form'] = array(
'#prefix' => '<div id="term_display_form_replace">',
'#prefix' => '<div id="replace-term-display-form">',
'#suffix' => '</div>',
'#title' => t('!label term display options', array('!label' => $options[$selected])),
'#tree' => TRUE,
'#type' => 'fieldset',
);
$term_display_class = new $selected;
// Get the fieldset from our handler, if the handler was the one used on the
// last form build then send the array of values for it's field.
if ($last_handler == $selected) {
$term_display_class->formFieldset($fieldset['term_display_form'], $state_values['term_display_form']);
// Get the fieldset from the selected handler.
$term_display = new $selected;
// Set $options as null
$options = NULL;
if ($stored_settings) {
$options = unserialize($stored_settings['term_display_options']);
}
else {
$term_display_class->formFieldset($fieldset['term_display_form'], array());
// If the handler wasn't the one used on the last form build then reset the
// $form_state values for it.
if (!isset($last_handler) || $last_handler != $selected) {
$values['term_display_form'] = array();
}
$term_display->formFieldset($fieldset['term_display_form'], $values['term_display_form'], $options);
// Add our #validate and #submit callback which will call plugins' methods.
$form['#validate'][] = 'taxonomy_display_form_field_ui_display_overview_validate';
$form['#submit'][] = 'taxonomy_display_form_field_ui_display_overview_submit';
}
/**
* Form submit callback; save Taxonomy Display settings for term pages.
*/
function taxonomy_display_form_field_ui_display_overview_submit($form, &$form_state) {
$values =& $form_state['values']['additional_settings']['taxonomy_display'];
$td_form =& $form['additional_settings']['taxonomy_display'];
// Perform submit functions for our term display handler.
$term_display = new $values['term_display_select'];
$term_display_options = $term_display->formSubmit($td_form['term_display_form'], $values['term_display_form']);
// TODO: Perform submit functions for our associated content handler.
// Save data
taxonomy_display_save_taxonomy_display($form['#bundle'], array(
'term_display_plugin' => $values['term_display_select'],
'term_display_options' => $term_display_options,
'associated_display_plugin' => '',
'associated_display_options' => '',
));
}
/**
* Form validate callback; validate Taxonomy Display settings for term pages.
*/
function taxonomy_display_form_field_ui_display_overview_validate($form, &$form_state) {
$values =& $form_state['values']['additional_settings']['taxonomy_display'];
$td_form =& $form['additional_settings']['taxonomy_display'];
// Perform validation functions for our term display handler.
$term_display = new $values['term_display_select'];
$term_display->formValidate($td_form['term_display_form'], $values['term_display_form']);
// TODO: Perform validation functions for our associated content handler.
}
/**
......@@ -195,6 +285,103 @@ function taxonomy_display_taxonomy_display_plugins() {
);
}
/**
* Helper function; save a taxonomy display record in the database.
*
* @param string $machine_name
* The machine name of the vocabulary's taxonomy display data to save.
* @param array $save_data
*
* @param string|NULL|FALSE $watchdog_message
* Provide a watchdog message as a string. If null then a generic default
* message will be used. If false then no watchdog message will be recorded.
* @param array $watchdog_variables
* Variables for substitution in the watchdog message.
* @param int $watchdog_severity
* One of the defined watchdog constant severities: WATCHDOG_EMERGENCY,
* WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING,
* WATCHDOG_NOTICE, WATCHDOG_INFO, WATCHDOG_DEBUG. Defaults to
* WATCHDOG_NOTICE.
*
* @return void
*
* @todo test this works
*/
function taxonomy_display_save_taxonomy_display($machine_name, $save_data = array(), $watchdog_message = NULL, $watchdog_variables = array(), $watchdog_severity = WATCHDOG_NOTICE) {
// Prepare the fields to be used for the insert or update query.
$query_fields = array();
foreach($save_data as $k => $v) {
switch ($k) {
case 'machine_name':
case 'term_display_plugin':
case 'associated_display_plugin':
$query_fields[$k] = $v;
break;
case 'term_display_options':
case 'associated_display_options':
$query_fields[$k] = serialize($v);
break;
default:
break;
}
}
// Allow other modules to alter our fields before they are inserted/updated.
drupal_alter('taxonomy_display_save_fields', $query_fields, $save_data);
// Retrieve the machine name, tells us whether to insert or update.
$update = taxonomy_display_fetch_taxonomy_display($machine_name);
// Perform our save
try {
// If update
if ($update) {
// Do not proceed if there are no fields to update.
if (empty($query_fields)) {
return;
}
// Query
$count = (bool) db_update('taxonomy_display')
->fields($query_fields)
->condition('machine_name', $machine_name)
->execute();
if ($count && $watchdog_message !== FALSE) {
if (empty($watchdog_message)) {
$watchdog_message = 'Taxonomy display vocabulary data updated for %machine_name.';
$watchdog_variables = array('%machine_name' => $machine_name);
}
watchdog('taxonomy_display', $watchdog_message, $watchdog_variables, $watchdog_severity);
}
}
// Else insert
else {
// If the record is being inserted we do not want to force the user to
// supply machine_name twice for no reason, if they know it is an insert.
if (!isset($query_fields['machine_name'])) {
$query_fields['machine_name'] = $machine_name;
}
// Query
$value = db_insert('taxonomy_display')
->fields($query_fields)
->execute();
if ($watchdog_message !== FALSE) {
if (empty($watchdog_message)) {
$watchdog_message = 'Taxonomy display vocabulary data created for %machine_name.';
$watchdog_variables = array('%machine_name' => $machine_name);
}
watchdog('taxonomy_display', $watchdog_message, $watchdog_variables, $watchdog_severity);
}
}
}
catch(Exception $e) {
drupal_set_message(t('Taxonomy display data save failed. Message = %message, query= %query',
array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
}
}
/**
* Page callback; displays all nodes associated with a term.
*
......@@ -205,10 +392,52 @@ function taxonomy_display_taxonomy_display_plugins() {
* The page content.
*/
function taxonomy_display_taxonomy_term_page($term) {
// TODO: Select record from the database and run through plugins.
// Build breadcrumb like core does, we could make this alterable in the future
// especially if something like custom breadcrumbs tells us how they would
// like to alter this.
// @see taxonomy_term_page()
$current = (object) array(
'tid' => $term->tid,
);
$breadcrumb = array();
while ($parents = taxonomy_get_parents($current->tid)) {
$current = array_shift($parents);
$breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid);
}
$breadcrumb[] = l(t('Home'), NULL);
$breadcrumb = array_reverse($breadcrumb);
drupal_set_breadcrumb($breadcrumb);
drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);
// Build our content
$build = array();
$display_settings = taxonomy_display_fetch_taxonomy_display($term->vocabulary_machine_name);
// Term display
// Set the default display handler if one is not set or the plugin is missing.
if (!$display_settings || !class_exists($display_settings['term_display_plugin'], TRUE)) {
$display_settings = array(
'term_display_plugin' => 'TaxonomyDisplayTermDisplayHandlerCore',
);
}
// Set the term display options.
if (!$display_settings || !isset($display_settings['term_display_options'])) {
$display_settings['term_display_options'] = null;
}
else {
$display_settings['term_display_options'] = unserialize($display_settings['term_display_options']);
}
$term_display = new $display_settings['term_display_plugin'];
$build['term_heading'] = $term_display->displayTerm($term, $display_settings['term_display_options']);
// @todo Associated content display
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
$build = taxonomy_term_page($term);
// Example build
// TODO: remove
if (false) {
$build = taxonomy_term_page($term);
}
return $build;
}
......@@ -216,6 +445,10 @@ function taxonomy_display_taxonomy_term_page($term) {
/**
* Title callback; display title for term pages.
*
* As of now we do not do anything with this. If we come up with something that
* would be helpful or requested we can add it. This is simply to make all of
* the hook_menu_alter functions we changed point to the same module.
*
* @param $term
* A term object.
*
......@@ -232,31 +465,10 @@ function taxonomy_display_taxonomy_term_title($term) {
* @todo test this works
*/
function taxonomy_display_taxonomy_vocabulary_delete($vocabulary) {
// We use this function to delete taxonomy displays other than as a hook
// invocation, one such case works off taxonomy_vocabulary_get_names() which
// provides the vocabularies as arrays.
if (is_array($vocabulary)) {
$vocabulary = (object) $vocabulary;
}
// Delete our display record for the term being removed.
$count = (bool) db_delete('taxonomy_display')
->condition('machine_name', $vocabulary->machine_name)
->execute();
// If a record was deleted then log it in watchdog.
if ($count) {
watchdog('taxonomy_display', 'Taxonomy display settings deleted for %name in response to the vocabulary being deleted.', array('%name' => $vocabulary->machine_name), WATCHDOG_NOTICE);
}
}
/**
* Implements taxonomy_vocabulary_insert().
*/
function taxonomy_display_taxonomy_vocabulary_insert($vocabulary) {
// Upon inserting a vocabulary term add it to our displays with the default
// selection.
// TODO: Insert table
// Delete our display record for the vocabulary being removed.
taxonomy_display_delete_taxonomy_dislpay($vocabulary->machine_name,
'Taxonomy display settings deleted for %name in response to the vocabulary being deleted.',
array('%name' => $vocabulary->machine_name));
}
/**
......@@ -267,22 +479,11 @@ function taxonomy_display_taxonomy_vocabulary_insert($vocabulary) {
function taxonomy_display_taxonomy_vocabulary_update($vocabulary) {
// If the machine name is changed update our display table so that the display
// settings still apply.
if ($vocabulary->old_machine_name != $vocabulary->machine_name) {
try {
$count = (bool) db_update('taxonomy_display')
->fields(array(
'machine_name' => $vocabulary->machine_name,
))
->condition('machine_name', $vocabulary->old_machine_name)
->execute();
if ($count) {
watchdog('taxonomy_display', 'Taxonomy display vocabulary changed machine name from %old to %new in response to the vocabulary machine name being altered.', array('%old' => $vocabulary->old_machine_name, '%new' => $vocabulary->machine_name), WATCHDOG_NOTICE);
}