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

Added missing plugin support and resolved errors when a term is viewed and the...

Added missing plugin support and resolved errors when a term is viewed and the vocabulary does not have any taxonomy_display settings.
parent 57f86224
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
* @see taxonomy_display_form_fieldset() * @see taxonomy_display_form_fieldset()
*/ */
function taxonomy_display_admin_form(&$form, &$form_state) { function taxonomy_display_admin_form(&$form, &$form_state) {
// Only proceed if we are on taxonomy_term entity type and the user has admin // Only proceed if we are on taxonomy_term entity type and the user has admin
// taxonomy display. // taxonomy display.
if ($form['#entity_type'] != 'taxonomy_term' || !user_access('administer taxonomy display')) { if ($form['#entity_type'] != 'taxonomy_term' || !user_access('administer taxonomy display')) {
return; return;
...@@ -49,6 +49,7 @@ function taxonomy_display_admin_form(&$form, &$form_state) { ...@@ -49,6 +49,7 @@ function taxonomy_display_admin_form(&$form, &$form_state) {
return; return;
} }
// Retrieve the stored taxonomy display settings for the vocabulary.
$stored_settings = taxonomy_display_fetch_taxonomy_display($form['#bundle']); $stored_settings = taxonomy_display_fetch_taxonomy_display($form['#bundle']);
$form['additional_settings']['taxonomy_display'] = array( $form['additional_settings']['taxonomy_display'] = array(
...@@ -57,121 +58,14 @@ function taxonomy_display_admin_form(&$form, &$form_state) { ...@@ -57,121 +58,14 @@ function taxonomy_display_admin_form(&$form, &$form_state) {
'#type' => 'fieldset', '#type' => 'fieldset',
); );
$fieldset =& $form['additional_settings']['taxonomy_display']; $fieldset =& $form['additional_settings']['taxonomy_display'];
$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'];
} }
// Populate $options with plugins that are available. // Build the plugin forms for term and associated content display.
$options = taxonomy_display_plugins('term'); _taxonomy_display_admin_form_build_plugin_form('term', $fieldset, $values, $stored_settings);
_taxonomy_display_admin_form_build_plugin_form('associated', $fieldset, $values, $stored_settings);
// TODO: Rewrite the term and associated fieldsets to use a helper function.
// Term display fieldset
// TODO: Rewrite supporting missing plugins.
// Discover which option should be selected from term display plugins.
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)) {
$selected = $stored_settings['term_display_plugin'];
}
else {
$selected = 'TaxonomyDisplayTermDisplayHandlerCore';
}
$fieldset['term_display_select'] = array(
'#ajax' => array(
'callback' => 'taxonomy_display_ajax_term_display_callback',
'wrapper' => 'replace-term-display-form',
),
'#default_value' => $selected,
'#description' => t('Select which plugin you would like to display the term\'s content.'),
'#options' => $options,
'#title' => t('Term display'),
'#type' => 'select',
);
if (isset($form_state['taxonomy_display']['term_display_handler'])) {
$last_handler = $form_state['taxonomy_display']['term_display_handler'];
}
$form_state['taxonomy_display']['term_display_handler'] = $selected;
$fieldset['term_display_form'] = array(
'#prefix' => '<div id="replace-term-display-form">',
'#suffix' => '</div>',
'#title' => t('!label term display options', array('!label' => $options[$selected])),
'#type' => 'fieldset',
);
// Get the fieldset from the selected handler.
$term_display = new $selected;
// Set $options as null
$term_display_options = NULL;
if ($stored_settings) {
$term_display_options = unserialize($stored_settings['term_display_options']);
}
// 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'], $term_display_options);
// Populate $options with plugins that are available.
$options = taxonomy_display_plugins('associated');
// Associated content fieldset
// TODO: Rewrite supporting missing plugins.
// Discover which option should be selected from term display plugins.
if (isset($values) && isset($values['associated_display_select']) &&
array_key_exists($values['associated_display_select'], $options)) {
$selected = $values['associated_display_select'];
}
elseif (isset($stored_settings['associated_display_plugin']) &&
array_key_exists($stored_settings['associated_display_plugin'], $options)) {
$selected = $stored_settings['associated_display_plugin'];
}
else {
$selected = 'TaxonomyDisplayAssociatedDisplayHandlerCore';
}
$fieldset['associated_display_select'] = array(
'#ajax' => array(
'callback' => 'taxonomy_display_ajax_associated_display_callback',
'wrapper' => 'replace-associated-display-form',
),
'#default_value' => $selected,
'#description' => t('Select which plugin you would like to display the term\'s associated content.'),
'#options' => $options,
'#title' => t('Associated content display'),
'#type' => 'select',
);
if (isset($form_state['taxonomy_display']['associated_display_handler'])) {
$last_handler = $form_state['taxonomy_display']['associated_display_handler'];
}
$form_state['taxonomy_display']['associated_display_handler'] = $selected;
$fieldset['associated_display_form'] = array(
'#prefix' => '<div id="replace-associated-display-form">',
'#suffix' => '</div>',
'#title' => t('!label associated content display options', array('!label' => $options[$selected])),
'#type' => 'fieldset',
);
// Get the fieldset from the selected handler.
$associated_display = new $selected;
// Set $options as null
$associated_display_options = NULL;
if ($stored_settings) {
$associated_display_options = unserialize($stored_settings['associated_display_options']);
}
// 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['associated_display_form'] = array();
}
$associated_display->formFieldset($fieldset['associated_display_form'], $values['associated_display_form'], $associated_display_options);
// Add our #validate and #submit callback which will call plugins' methods. // Add our #validate and #submit callback which will call plugins' methods.
$form['#validate'][] = 'taxonomy_display_admin_form_validate'; $form['#validate'][] = 'taxonomy_display_admin_form_validate';
...@@ -185,21 +79,28 @@ function taxonomy_display_admin_form_submit($form, &$form_state) { ...@@ -185,21 +79,28 @@ function taxonomy_display_admin_form_submit($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'];
$save_data = array();
// Perform submit functions for our term display handler. // Perform submit functions for our term display handler.
if ($values['term_display_select'] != 'missing') {
$save_data['term_display_plugin'] = $values['term_display_select'];
$term_display = new $values['term_display_select']; $term_display = new $values['term_display_select'];
$term_display_options = $term_display->formSubmit($td_form['term_display_form'], $values['term_display_form']); $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. // Perform submit functions for our associated content handler.
if ($values['associated_display_select'] != 'missing') {
$save_data['associated_display_plugin'] = $values['associated_display_select'];
$associated_display = new $values['associated_display_select']; $associated_display = new $values['associated_display_select'];
$associated_display_options = $associated_display->formSubmit($td_form['associated_display_form'], $values['associated_display_form']); $save_data['associated_display_options'] = $associated_display->formSubmit($td_form['associated_display_form'], $values['associated_display_form']);
}
// Save data // Save data
taxonomy_display_save_taxonomy_display($form['#bundle'], array( if (!empty($save_data)) {
'term_display_plugin' => $values['term_display_select'], taxonomy_display_save_taxonomy_display($form['#bundle'], $save_data);
'term_display_options' => $term_display_options, }
'associated_display_plugin' => $values['associated_display_select'],
'associated_display_options' => $associated_display_options,
));
} }
/** /**
...@@ -210,12 +111,16 @@ function taxonomy_display_admin_form_validate($form, &$form_state) { ...@@ -210,12 +111,16 @@ function taxonomy_display_admin_form_validate($form, &$form_state) {
$td_form =& $form['additional_settings']['taxonomy_display']; $td_form =& $form['additional_settings']['taxonomy_display'];
// Perform validation functions for our term display handler. // Perform validation functions for our term display handler.
if ($values['term_display_select'] != 'missing') {
$term_display = new $values['term_display_select']; $term_display = new $values['term_display_select'];
$term_display->formValidate($td_form['term_display_form'], $values['term_display_form']); $term_display->formValidate($td_form['term_display_form'], $values['term_display_form']);
}
// Perform validation functions for our associated content display handler. // Perform validation functions for our associated content display handler.
if ($values['associated_display_select'] != 'missing') {
$associated_display = new $values['associated_display_select']; $associated_display = new $values['associated_display_select'];
$associated_display->formValidate($td_form['associated_display_form'], $values['associated_display_form']); $associated_display->formValidate($td_form['associated_display_form'], $values['associated_display_form']);
}
} }
/** /**
...@@ -235,3 +140,105 @@ function taxonomy_display_ajax_associated_display_callback($form, $form_state) { ...@@ -235,3 +140,105 @@ function taxonomy_display_ajax_associated_display_callback($form, $form_state) {
function taxonomy_display_ajax_term_display_callback($form, $form_state) { function taxonomy_display_ajax_term_display_callback($form, $form_state) {
return $form['additional_settings']['taxonomy_display']['term_display_form']; return $form['additional_settings']['taxonomy_display']['term_display_form'];
} }
/**
* Helper function; build the admin form for plugins.
*
* @param string $type
* Either term or associated.
* @param array $fieldset
* Form array that we will modify.
* @param array|null $values
* Array of values submitted to the fieldset, taken from $form_state or null
* if the form has not yet been submitted.
* @param $stored_settings
* Associated array from taxonomy_display_fetch_taxonomy_display().
*
* @return void
*
* @see taxonomy_display_admin_form()
*/
function _taxonomy_display_admin_form_build_plugin_form($type, &$fieldset, &$values, $stored_settings) {
// Set variables needed based on type
switch ($type) {
case 'associated':
$select_field_title = t('Associated content display');
$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;
}
// Populate $options with plugins that are available.
$options = taxonomy_display_plugins($type);
// TODO: Finish testing missing plugin support.
// Add support for missing plugins.
if (isset($stored_settings[$type . '_plugin_missing'])) {
$options = array_merge(array('missing' => t('-- Missing plugin --')), $options);
}
// Discover which option should be selected from the display plugins.
if (isset($values[$type . '_display_select']) &&
$values[$type . '_display_select'] != 'missing' &&
array_key_exists($values[$type . '_display_select'], $options)) {
$selected = $values[$type . '_display_select'];
}
elseif (isset($options['missing'])) {
$selected = 'missing';
}
elseif (!isset($stored_settings['no_record'])) {
$selected = $stored_settings[$type . '_display_plugin'];
}
$fieldset[$type . '_display_select'] = array(
'#ajax' => array(
'callback' => 'taxonomy_display_ajax_' . $type . '_display_callback',
'wrapper' => 'replace-' . $type . '-display-form',
),
'#default_value' => $selected,
'#description' => $select_field_description,
'#options' => $options,
'#title' => $select_field_title,
'#type' => 'select',
);
if (isset($form_state['taxonomy_display'][$type . '_display_handler'])) {
$last_handler = $form_state['taxonomy_display'][$type . '_display_handler'];
}
$form_state['taxonomy_display'][$type . '_display_handler'] = $selected;
if ($selected == 'missing') {
$fieldset[$type . '_display_form'] = array(
'#description' => t('The selected display plugin is missing. Either restore the plugin or select one that is available to configure.'),
'#prefix' => '<div id="replace-' . $type . '-display-form">',
'#suffix' => '</div>',
'#type' => 'fieldset',
);
return;
}
$fieldset[$type . '_display_form'] = array(
'#prefix' => '<div id="replace-' . $type . '-display-form">',
'#suffix' => '</div>',
'#title' => t('!label display options', array('!label' => $options[$selected])),
'#type' => 'fieldset',
);
// Get the fieldset from the selected handler.
$plugin_class = new $selected;
// Set $options as null
$display_options = NULL;
if (!isset($stored_settings['no_record'])) {
$display_options = $stored_settings[$type . '_display_options'];
}
// 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[$type . '_display_form'] = array();
}
$plugin_class->formFieldset($fieldset[$type . '_display_form'], $values[$type . '_display_form'], $display_options);
}
...@@ -48,15 +48,53 @@ function taxonomy_display_delete_taxonomy_dislpay($machine_name, $watchdog_messa ...@@ -48,15 +48,53 @@ function taxonomy_display_delete_taxonomy_dislpay($machine_name, $watchdog_messa
* @param string $machine_name * @param string $machine_name
* The machine name of the vocabulary's taxonomy display data to fetch. * The machine name of the vocabulary's taxonomy display data to fetch.
* *
* @return array|FALSE * @return array
* Return associated array if a record is found, else false. * Return associated array.
*/ */
function taxonomy_display_fetch_taxonomy_display($machine_name) { function taxonomy_display_fetch_taxonomy_display($machine_name) {
return 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();
// Set defaults if a result was not found
if (!$result) {
return array(
'machine_name' => $machine_name,
'term_display_plugin' => 'TaxonomyDisplayTermDisplayHandlerCore',
'term_display_options' => NULL,
'associated_display_plugin' => 'TaxonomyDisplayAssociatedDisplayHandlerCore',
'associated_display_options' => NULL,
'no_record' => TRUE,
);
}
// Prepare term display data for use.
if (!class_exists($result['term_display_plugin'], TRUE)) {
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);
$result['term_display_plugin'] = 'TaxonomyDisplayTermDisplayHandlerCore';
$result['term_display_options'] = NULL;
$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['associated_display_options'] = NULL;
$result['associated_plugin_missing'] = TRUE;
}
else {
$result['associated_display_options'] = unserialize($result['associated_display_options']);
}
return $result;
} }
/** /**
...@@ -223,7 +261,7 @@ function taxonomy_display_save_taxonomy_display($machine_name, $save_data = arra ...@@ -223,7 +261,7 @@ function taxonomy_display_save_taxonomy_display($machine_name, $save_data = arra
// Perform our save // Perform our save
try { try {
// If update // If update
if ($update) { if (!isset($update['no_record'])) {
// Do not proceed if there are no fields to update. // Do not proceed if there are no fields to update.
if (empty($query_fields)) { if (empty($query_fields)) {
return; return;
...@@ -303,40 +341,13 @@ function taxonomy_display_taxonomy_term_page($term) { ...@@ -303,40 +341,13 @@ function taxonomy_display_taxonomy_term_page($term) {
$display_settings = taxonomy_display_fetch_taxonomy_display($term->vocabulary_machine_name); $display_settings = taxonomy_display_fetch_taxonomy_display($term->vocabulary_machine_name);
// TODO: Move term display and associated display in to a helper function.
// Term display // 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']; $term_display = new $display_settings['term_display_plugin'];
$build['term_heading'] = $term_display->displayTerm($term, $display_settings['term_display_options']); $build['term_heading'] = $term_display->displayTerm($term, $display_settings['term_display_options']);
// Associated content display // Associated content display
// Set the default display handler if one is not set or the plugin is missing. $associated_display = new $display_settings['associated_display_plugin'];
if (!$display_settings || !class_exists($display_settings['associated_display_plugin'], TRUE)) { $build = array_merge($build, $associated_display->displayAssociated($term, $display_settings['associated_display_options']));
$display_settings = array(
'term_display_plugin' => 'TaxonomyDisplayAssociatedDisplayHandlerCore',
);
}
// Set the associated display options.
if (!$display_settings || !isset($display_settings['associated_display_options'])) {
$display_settings['associated_display_options'] = NULL;
}
else {
$display_settings['associated_display_options'] = unserialize($display_settings['associated_display_options']);
}
$term_display = new $display_settings['associated_display_plugin'];
$build = array_merge($build, $term_display->displayAssociated($term, $display_settings['associated_display_options']));
return $build; return $build;
} }
......
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