Commit f4dd1fea authored by Cody Craven's avatar Cody Craven
Browse files

Issue #1247802 by codycraven: Added plugin support for taxonomy breadcrumbs.

parent 5a0d2b80
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Taxonomy Display 7.x-1.x, YYYY-MM-DD Taxonomy Display 7.x-1.x, YYYY-MM-DD
------------------------------------ ------------------------------------
Issue #1247802 by codycraven: Added plugin support for taxonomy breadcrumbs.
Issue #1237136 by codycraven: Expose term object on term page to altering. Issue #1237136 by codycraven: Expose term object on term page to altering.
Issue #1230200 by R.Muilwijk: Fixed exported views not recognized. Issue #1230200 by R.Muilwijk: Fixed exported views not recognized.
Issue #1231346 by codycraven: Remove the ability to select whether taxonomy Issue #1231346 by codycraven: Remove the ability to select whether taxonomy
......
<?php
/**
* Abstract class for exposing taxonomy term breadcrumbs to generation.
*/
abstract class TaxonomyDisplayBreadcrumbDisplayHandler extends TaxonomyDisplayHandlerForm {
/**
* Build and attach breadcrumb to page.
*
* @param termObject $term
* The taxonomy term to build breadcrumb for.
*
* @return void
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
* @see taxonomy_term_page()
*/
abstract public function buildBreadcrumb($term);
}
<?php
/**
* Add a display handler to generate the same breadcrumb from Drupal core.
*/
class TaxonomyDisplayBreadcrumbDisplayHandlerCore extends TaxonomyDisplayBreadcrumbDisplayHandler {
/**
* Do not display anything to the user, we return an empty string.
*
* @see TaxonomyDisplayBreadcrumbDisplayHandler::buildBreacrumb()
*/
public function buildBreadcrumb($term) {
// Build breadcrumb like core does.
// @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);
}
/**
* Set a description when the user selects this option.
*
* @see TaxonomyDisplayHandlerForm::formFieldset()
*/
public function formFieldset(&$form, &$values, $options = NULL) {
$form['#description'] = t('The Drupal core breadcrumb will be used for the taxonomy terms.');
}
/**
* Do not want anything stored, return void.
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
public function formSubmit($form, &$values) {}
}
<?php
/**
* Add a display handler that will omit the breadcrumb on term pages.
*/
class TaxonomyDisplayBreadcrumbDisplayHandlerHidden extends TaxonomyDisplayBreadcrumbDisplayHandler {
/**
* Do not display anything to the user, we return an empty string.
*
* @see TaxonomyDisplayBreadcrumbDisplayHandler::buildBreacrumb()
*/
public function buildBreadcrumb($term) {
drupal_set_breadcrumb('');
}
/**
* Set a description when the user selects this option.
*
* @see TaxonomyDisplayHandlerForm::formFieldset()
*/
public function formFieldset(&$form, &$values, $options = NULL) {
$form['#description'] = t('The breadcrumb for the term will not be displayed to the user when viewed.');
}
/**
* Do not want anything stored, return void.
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
public function formSubmit($form, &$values) {}
}
...@@ -62,8 +62,10 @@ function taxonomy_display_admin_form(&$form, &$form_state) { ...@@ -62,8 +62,10 @@ function taxonomy_display_admin_form(&$form, &$form_state) {
$fieldset =& $form['additional_settings']['taxonomy_display']; $fieldset =& $form['additional_settings']['taxonomy_display'];
// Build the plugin forms for term and associated content display. // Build the plugin forms for term and associated content display.
_taxonomy_display_admin_form_build_plugin_form('term', $fieldset, $form_state, $stored_settings); $plugin_types = _taxonomy_display_plugin_types();
_taxonomy_display_admin_form_build_plugin_form('associated', $fieldset, $form_state, $stored_settings); foreach ($plugin_types as $k => $v) {
_taxonomy_display_admin_form_build_plugin_form($k, $fieldset, $form_state, $stored_settings);
}
// Add selection for adding taxonomy term core feed to page // Add selection for adding taxonomy term core feed to page
$fieldset['add_feed'] = array( $fieldset['add_feed'] = array(
...@@ -122,20 +124,15 @@ function taxonomy_display_admin_form_submit($form, &$form_state) { ...@@ -122,20 +124,15 @@ function taxonomy_display_admin_form_submit($form, &$form_state) {
$save_data = array(); $save_data = array();
// Perform submit functions for our term display handler. $plugin_types = _taxonomy_display_plugin_types();
if ($values['term_display_select'] != 'missing') { // Perform submit functions for our plugins.
$save_data['term_display_plugin'] = $values['term_display_select']; foreach($plugin_types as $k => $v) {
if ($values[$k . '_display_select'] != 'missing') {
$term_display = new $values['term_display_select']; $save_data[$k . '_display_plugin'] = $values[$k . '_display_select'];
$save_data['term_display_options'] = $term_display->formSubmit($td_form['term_display_form'], $values['term_display_form']);
}
// Perform submit functions for our associated content handler. $class = new $values[$k . '_display_select'];
if ($values['associated_display_select'] != 'missing') { $save_data[$k . '_display_options'] = $class->formSubmit($td_form[$k . '_display_form'], $values[$k . '_display_form']);
$save_data['associated_display_plugin'] = $values['associated_display_select']; }
$associated_display = new $values['associated_display_select'];
$save_data['associated_display_options'] = $associated_display->formSubmit($td_form['associated_display_form'], $values['associated_display_form']);
} }
// Save the add_feed setting. // Save the add_feed setting.
...@@ -152,16 +149,13 @@ function taxonomy_display_admin_form_validate($form, &$form_state) { ...@@ -152,16 +149,13 @@ function taxonomy_display_admin_form_validate($form, &$form_state) {
$values =& $form_state['values']['additional_settings']['taxonomy_display']; $values =& $form_state['values']['additional_settings']['taxonomy_display'];
$td_form =& $form['additional_settings']['taxonomy_display']; $td_form =& $form['additional_settings']['taxonomy_display'];
// Perform validation functions for our term display handler. $plugin_types = _taxonomy_display_plugin_types();
if ($values['term_display_select'] != 'missing') { // Perform submit functions for our plugins.
$term_display = new $values['term_display_select']; foreach ($plugin_types as $k => $v) {
$term_display->formValidate($td_form['term_display_form'], $values['term_display_form']); if ($values[$k . '_display_select'] != 'missing') {
} $class = new $values[$k . '_display_select'];
$class->formValidate($td_form[$k . '_display_form'], $values[$k . '_display_form']);
// Perform validation functions for our associated content display handler. }
if ($values['associated_display_select'] != 'missing') {
$associated_display = new $values['associated_display_select'];
$associated_display->formValidate($td_form['associated_display_form'], $values['associated_display_form']);
} }
} }
...@@ -174,6 +168,13 @@ function taxonomy_display_ajax_associated_display_callback($form, $form_state) { ...@@ -174,6 +168,13 @@ function taxonomy_display_ajax_associated_display_callback($form, $form_state) {
return $form['additional_settings']['taxonomy_display']['associated_display_form']; return $form['additional_settings']['taxonomy_display']['associated_display_form'];
} }
/**
* Return just the breadcrumb display sub-form.
*/
function taxonomy_display_ajax_breadcrumb_display_callback($form, $form_state) {
return $form['additional_settings']['taxonomy_display']['breadcrumb_display_form'];
}
/** /**
* Return just the term display sub-form. * Return just the term display sub-form.
* *
...@@ -198,7 +199,7 @@ function taxonomy_display_field_ui_layouts_validate($form, &$form_state) { ...@@ -198,7 +199,7 @@ function taxonomy_display_field_ui_layouts_validate($form, &$form_state) {
* Helper function; build the admin form for plugins. * Helper function; build the admin form for plugins.
* *
* @param string $type * @param string $type
* Either term or associated. * Either 'term', 'associated', or 'breadcrumb'.
* @param array $fieldset * @param array $fieldset
* Form array that we will modify. * Form array that we will modify.
* @param array|null $form_state * @param array|null $form_state
...@@ -211,20 +212,16 @@ function taxonomy_display_field_ui_layouts_validate($form, &$form_state) { ...@@ -211,20 +212,16 @@ function taxonomy_display_field_ui_layouts_validate($form, &$form_state) {
* @see taxonomy_display_admin_form() * @see taxonomy_display_admin_form()
*/ */
function _taxonomy_display_admin_form_build_plugin_form($type, &$fieldset, &$form_state, $stored_settings) { function _taxonomy_display_admin_form_build_plugin_form($type, &$fieldset, &$form_state, $stored_settings) {
// Set variables needed based on type $plugin_types = _taxonomy_display_plugin_types();
switch ($type) {
case 'associated': if (!isset($plugin_types[$type])) {
$select_field_title = t('Associated content display'); return;
$select_field_description = t('Select which plugin you would like to display the term\'s associated content.');
break;
case 'term':
$select_field_title = t('Term display');
$select_field_description = t('Select which plugin you would like to display the term\'s content.');
break;
default:
return;
} }
// Set variables needed based on type
$select_field_title = t($plugin_types[$type]['long']);
$select_field_description = t('Select which plugin you would like to display the term\'s ' . $plugin_types[$type]['description']);
$values = NULL; $values = NULL;
if (isset($form_state['values']['additional_settings']['taxonomy_display'])) { if (isset($form_state['values']['additional_settings']['taxonomy_display'])) {
$values =& $form_state['values']['additional_settings']['taxonomy_display']; $values =& $form_state['values']['additional_settings']['taxonomy_display'];
......
...@@ -6,6 +6,7 @@ dependencies[] = taxonomy ...@@ -6,6 +6,7 @@ dependencies[] = taxonomy
files[] = taxonomy_display.module files[] = taxonomy_display.module
; Our abstract classes for plugins ; Our abstract classes for plugins
files[] = classes/associated_display.inc files[] = classes/associated_display.inc
files[] = classes/breadcrumb_display.inc
files[] = classes/handler_form.inc files[] = classes/handler_form.inc
files[] = classes/term_display.inc files[] = classes/term_display.inc
; Our class handlers for plugins ; Our class handlers for plugins
...@@ -15,4 +16,7 @@ files[] = handlers/term/hidden.inc ...@@ -15,4 +16,7 @@ files[] = handlers/term/hidden.inc
; - For associated content displays ; - For associated content displays
files[] = handlers/associated/core.inc files[] = handlers/associated/core.inc
files[] = handlers/associated/hidden.inc files[] = handlers/associated/hidden.inc
files[] = handlers/associated/views.inc files[] = handlers/associated/views.inc
\ No newline at end of file ; - For breadcrumb generation
files[] = handlers/breadcrumb/core.inc
files[] = handlers/breadcrumb/hidden.inc
...@@ -83,6 +83,20 @@ function taxonomy_display_schema() { ...@@ -83,6 +83,20 @@ function taxonomy_display_schema() {
'default' => 1, 'default' => 1,
'description' => 'Whether to add Drupal\'s core feed.', 'description' => 'Whether to add Drupal\'s core feed.',
), ),
'breadcrumb_display_plugin' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The plugin used to display the breadcrumb.',
),
'breadcrumb_display_options' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'The plugin data for the breadcrumb display.',
),
), ),
'primary key' => array('machine_name'), 'primary key' => array('machine_name'),
); );
...@@ -122,4 +136,33 @@ function taxonomy_display_update_7002() { ...@@ -122,4 +136,33 @@ function taxonomy_display_update_7002() {
'default' => 1, 'default' => 1,
'description' => 'Whether to add Drupal\'s core feed.', 'description' => 'Whether to add Drupal\'s core feed.',
)); ));
} }
\ No newline at end of file
/**
* Implements hook_update_N().
*/
function taxonomy_display_update_7003() {
// Add fields on taxonomy_display records in the storage system to add support
// for our new breadcrumb plugin type.
// See http://drupal.org/node/1247802
// Add field with a default value for the core breadcrumb type.
db_add_field('taxonomy_display', 'breadcrumb_display_plugin', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'TaxonomyDisplayBreadcrumbDisplayHandlerCore',
'description' => 'The plugin used to display the breadcrumb.',
));
// Remove the default value now that existing records have been updated with
// the core value.
db_field_set_default('taxonomy_display', 'breadcrumb_display_plugin', '');
db_add_field('taxonomy_display', 'breadcrumb_display_options', array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'The plugin data for the breadcrumb display.',
));
}
...@@ -52,47 +52,44 @@ function taxonomy_display_delete_taxonomy_dislpay($machine_name, $watchdog_messa ...@@ -52,47 +52,44 @@ function taxonomy_display_delete_taxonomy_dislpay($machine_name, $watchdog_messa
* Return associated array. * Return associated array.
*/ */
function taxonomy_display_fetch_taxonomy_display($machine_name) { function taxonomy_display_fetch_taxonomy_display($machine_name) {
// TODO: Add caching
$result = db_select('taxonomy_display', 'td') $result = db_select('taxonomy_display', 'td')
->fields('td') ->fields('td')
->condition('machine_name', $machine_name) ->condition('machine_name', $machine_name)
->execute() ->execute()
->fetchAssoc(); ->fetchAssoc();
$plugin_types = _taxonomy_display_plugin_types();
// Set defaults if a result was not found // Set defaults if a result was not found
if (!$result) { if (!$result) {
return array( $defaults = array(
'machine_name' => $machine_name, 'machine_name' => $machine_name,
'term_display_plugin' => 'TaxonomyDisplayTermDisplayHandlerCore',
'term_display_options' => NULL,
'associated_display_plugin' => 'TaxonomyDisplayAssociatedDisplayHandlerCore',
'associated_display_options' => NULL,
'add_feed' => 1, 'add_feed' => 1,
'no_record' => TRUE, 'no_record' => TRUE,
); );
foreach($plugin_types as $k => $v) {
$defaults[$k . '_display_plugin'] = 'TaxonomyDisplay' . ucfirst($k) . 'DisplayHandlerCore';
$defaults[$k . '_dipslay_options'] = NULL;
}
return $defaults;
} }
// Prepare term display data for use. // Prepare plugins' data for use.
if (!class_exists($result['term_display_plugin'], TRUE)) { foreach ($plugin_types as $k => $v) {
watchdog('taxonomy_display', 'The taxonomy term display plugin assigned to the %name vocabulary is missing, Drupal default settings were used instead.', array('%name' => $machine_name), WATCHDOG_WARNING); if (!class_exists($result[$k . '_display_plugin'])) {
// Note that if providing translations for this module you must provide a
$result['term_display_plugin'] = 'TaxonomyDisplayTermDisplayHandlerCore'; // translation for this watchdog warning for each $value.
$result['term_display_options'] = NULL; watchdog('taxonomy_display', 'The taxonomy ' . $v['short'] . ' plugin assigned to the %name vocabulary is missing, Drupal default settings were used instead.', array('%name' => $machine_name), WATCHDOG_WARNING);
$result['term_plugin_missing'] = TRUE;
}
else {
$result['term_display_options'] = unserialize($result['term_display_options']);
}
// Prepare associated content display data for use.
if (!class_exists($result['associated_display_plugin'], TRUE)) {
watchdog('taxonomy_display', 'The taxonomy associated content plugin assigned to the %name vocabulary is missing, Drupal default settings were used instead.', array('%name' => $machine_name), WATCHDOG_WARNING);
$result['associated_display_plugin'] = 'TaxonomyDisplayAssociatedDisplayHandlerCore'; $result[$k . '_display_plugin'] = 'TaxonomyDisplay' . ucfirst($k) . 'DisplayHandlerCore';
$result['associated_display_options'] = NULL; $result[$k . '_display_options'] = NULL;
$result['associated_plugin_missing'] = TRUE; $result[$k . '_plugin_missing'] = TRUE;
} }
else { else {
$result['associated_display_options'] = unserialize($result['associated_display_options']); $result[$k . '_display_options'] = unserialize($result[$k . '_display_options']);
}
} }
return $result; return $result;
...@@ -115,7 +112,7 @@ function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$fo ...@@ -115,7 +112,7 @@ function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$fo
* @param array $form * @param array $form
* Form array. * Form array.
* @param string $fetch * @param string $fetch
* String either 'term' or 'associated'. * String either 'term', 'associated', or 'breadcrumb'.
* *
* @return array|FALSE * @return array|FALSE
* Returns the form fieldset if valid, otherwise boolean false. * Returns the form fieldset if valid, otherwise boolean false.
...@@ -155,8 +152,9 @@ function taxonomy_display_permission() { ...@@ -155,8 +152,9 @@ function taxonomy_display_permission() {
* Retrieve an array of Taxonomy Display plugins. * Retrieve an array of Taxonomy Display plugins.
* *
* @param null|string $type * @param null|string $type
* Only retrieve plugins of a specific type, expected values 'term' and * Only retrieve plugins of a specific type, expected values 'term',
* 'associated'. By default, NULL, this will return an array with both. * 'associated', and 'breadcrumb'. By default, NULL, this will return an array
* with all.
* *
* @return array|void * @return array|void
* If an expected value is provided for $type, or no value is provided, an * If an expected value is provided for $type, or no value is provided, an
...@@ -185,13 +183,14 @@ function taxonomy_display_plugins($type = NULL) { ...@@ -185,13 +183,14 @@ function taxonomy_display_plugins($type = NULL) {
* Implements hook_taxonomy_display_plugins(). * Implements hook_taxonomy_display_plugins().
* *
* Taxonomy Display invokes this hook when looking for plugins for displaying * Taxonomy Display invokes this hook when looking for plugins for displaying
* taxonomy term and taxonomy term's associated content. * taxonomy term, taxonomy term's associated content, and taxonomy term's
* breadcrumbs.
*/ */
function taxonomy_display_taxonomy_display_plugins() { function taxonomy_display_taxonomy_display_plugins() {
// To add custom plugins in your own hook implementation return an array with // To add custom plugins in your own hook implementation return an array with
// the format below: // the format below:
$plugins = array( $plugins = array(
// In the two arrays 'associated' and 'term', provide the implementing class // In the array's sencondary arrays, provide the implementing class
// name of your handler as the keys and the text to be displayed to the user // name of your handler as the keys and the text to be displayed to the user
// for display as the value. // for display as the value.
// //
...@@ -201,6 +200,10 @@ function taxonomy_display_taxonomy_display_plugins() { ...@@ -201,6 +200,10 @@ function taxonomy_display_taxonomy_display_plugins() {
'TaxonomyDisplayAssociatedDisplayHandlerCore' => t('Core'), 'TaxonomyDisplayAssociatedDisplayHandlerCore' => t('Core'),
'TaxonomyDisplayAssociatedDisplayHandlerHidden' => t('Hidden'), 'TaxonomyDisplayAssociatedDisplayHandlerHidden' => t('Hidden'),
), ),
'breadcrumb' => array(
'TaxonomyDisplayBreadcrumbDisplayHandlerCore' => t('Core'),
'TaxonomyDisplayBreadcrumbDisplayHandlerHidden' => t('Hidden'),
),
'term' => array( 'term' => array(
'TaxonomyDisplayTermDisplayHandlerCore' => t('Core'), 'TaxonomyDisplayTermDisplayHandlerCore' => t('Core'),
'TaxonomyDisplayTermDisplayHandlerHidden' => t('Hidden'), 'TaxonomyDisplayTermDisplayHandlerHidden' => t('Hidden'),
...@@ -246,11 +249,13 @@ function taxonomy_display_save_taxonomy_display($machine_name, $save_data = arra ...@@ -246,11 +249,13 @@ function taxonomy_display_save_taxonomy_display($machine_name, $save_data = arra
case 'machine_name': case 'machine_name':
case 'term_display_plugin': case 'term_display_plugin':
case 'associated_display_plugin': case 'associated_display_plugin':
case 'breadcrumb_display_plugin':
case 'add_feed': case 'add_feed':
$query_fields[$k] = $v; $query_fields[$k] = $v;
break; break;
case 'term_display_options': case 'term_display_options':
case 'associated_display_options': case 'associated_display_options':
case 'breadcrumb_display_options':
$query_fields[$k] = serialize($v); $query_fields[$k] = serialize($v);
break; break;
default: default:
...@@ -328,21 +333,9 @@ function taxonomy_display_taxonomy_term_page($term) { ...@@ -328,21 +333,9 @@ function taxonomy_display_taxonomy_term_page($term) {
// Load settings for this vocabulary term. // Load settings for this vocabulary term.
$display_settings = taxonomy_display_fetch_taxonomy_display($term->vocabulary_machine_name); $display_settings = taxonomy_display_fetch_taxonomy_display($term->vocabulary_machine_name);
// Build breadcrumb like core does, we could make this alterable in the future // Breadcrumb display
// especially if something like custom breadcrumbs tells us how they would $breadcrumb_display = new $display_settings['breadcrumb_display_plugin'];
// like to alter this. $breadcrumb_display->buildBreadcrumb($term);
// @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);
// Conditionally add core Drupal feed to page // Conditionally add core Drupal feed to page
if ($display_settings['add_feed']) { if ($display_settings['add_feed']) {
...@@ -408,3 +401,30 @@ function taxonomy_display_taxonomy_vocabulary_update($vocabulary) { ...@@ -408,3 +401,30 @@ function taxonomy_display_taxonomy_vocabulary_update($vocabulary) {
array('%old' => $vocabulary->old_machine_name, '%new' => $vocabulary->machine_name)); array('%old' => $vocabulary->old_machine_name, '%new' => $vocabulary->machine_name));
} }
} }
/**
* Helper; Retrieve array of plugin types for taxonomy display.
*
* By using this function and iterating over the values in utilizing code we
* avoid having duplicate assignments which makes management and verification
* extremely difficult and tedious.
*/
function _taxonomy_display_plugin_types() {
return array(
'term' => array(
'short' => 'term display',
'description' => 'content',
'long' => 'Term display',
),
'associated' => array(
'short' => 'associated content',
'description' => 'associated content',
'long' => 'Associated content display',
),
'breadcrumb' => array(
'short' => 'breadcrumb',
'description' => 'breadcrumb',
'long' => 'Breadcrumb display',
),
);
}
Supports Markdown
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