Commit 5b004c24 authored by Cody Craven's avatar Cody Craven
Browse files

Initial commit.

parents
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
?>
<?php
// $Id$
/**
* Abstract class to act as the taxonomy display plugins form handing.
*
* The $form and $form_state parameters listed within this section are not true
* $form and $form_state but rather a fieldset and form_state['values'] for the
* fields in that fielset.
*/
abstract class TaxonomyDisplayHandlerForm {
/**
* Prepare the form that users will configure.
*
* @param array $form
* A fieldset for configuration to display to the user for this handler.
* @param array $form_state
* The form state.
*
* @return TaxonomyTermDisplayHandler
*/
abstract public function formFieldset(&$form, &$form_state);
/**
* 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.
*
* @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.
*/
abstract public function formSubmit(&$form, &$form_state);
/**
* Perform validation on the custom formFieldset when the user submits.
*
* This method does not need to be defined in the implementing class as
* 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.
*
* @return TaxonomyTermDisplayHandler
*/
public function formValidate(&$form, &$form_state) {
// Override formValidate in your implementing handler if any validation work
// is to be done.
return $this;
}
}
\ No newline at end of file
<?php
// $Id$
/**
* @todo Write class explanation
*/
abstract class TaxonomyDisplayTermDisplayHandler extends TaxonomyDisplayHandlerForm {
/**
* Render the output to be displayed when the user views the taxonomy term.
*
* @param $term
* The taxonomy term to be displayed.
* @param $options
* The data that was stored on behalf of the handler resulting from the
* return of the TaxonomyDisplayHandlerForm::formSubmit implementation.
*
* @return string
* The output the user will see when viewing the term.
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
abstract public function displayTerm($term, $options);
}
<?php
// $Id$
/**
* Add a display handler that will use the Drupal core method of display.
*/
class TaxonomyDisplayTermDisplayHandlerCore extends TaxonomyDisplayTermDisplayHandler {
/**
*
*
* @see TaxonomyDisplayTermDisplayHandler::displayTerm()
*/
public function displayTerm($term, $options) {
return '';
}
/**
*
*
* @see TaxonomyDisplayHandlerForm::formFieldset()
*/
public function formFieldset(&$form, &$form_state) {
$form['#description'] = t('Hit!');
}
/**
* We do not want anything stored so return null.
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
public function formSubmit(&$form, &$form_state) {
return;
}
}
\ No newline at end of file
<?php
// $Id$
/**
* Add a display handler that will hide the term when viewed.
*/
class TaxonomyDisplayTermDisplayHandlerHidden extends TaxonomyDisplayTermDisplayHandler {
/**
* Do not display anything to the user, we return an empty string.
*
* @see TaxonomyDisplayTermDisplayHandler::displayTerm()
*/
public function displayTerm($term, $options) {
return '';
}
/**
* Set a description when the user selects this option.
*
* @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.');
}
/**
* Do not want anything stored, return void.
*
* @see TaxonomyDisplayHandlerForm::formSubmit()
*/
public function formSubmit(&$form, &$form_state) {
return;
}
}
\ No newline at end of file
; $Id$
name = Taxonomy display
description = Modify the display of taxonomy term pages per vocabulary.
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/term_display.inc
; Our class handlers for plugins
; - For term displays
files[] = handlers/term/core.inc
files[] = handlers/term/hidden.inc
; - For associated content displays
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Install, update and uninstall functions for the taxonomy display module.
*/
/**
* Implements hook_enable().
*
* Perform cleanup from while taxonomy_display was disabled.
*
* @todo test this works
*/
function taxonomy_display_enable() {
watchdog('taxonomy_display', 'Taxonomy display is performing cleanup in response to being enabled, any changes made will be subsequently logged.', array(), WATCHDOG_INFO);
// Retrieve all the existing vocabularies.
$vocabularies = taxonomy_vocabulary_get_names();
// Select taxonomy displays we have in the DB.
$taxonomy_displays = db_select('taxonomy_display', 'td')
->fields('td', array('machine_name'))
->execute();
// Loop through each display
foreach ($taxonomy_displays as $taxonomy_display) {
// 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);
}
}
}
/**
* Implements hook_schema().
*/
function taxonomy_display_schema() {
$schema['taxonomy_display'] = array(
'description' => 'Per vocabulary configuration for term pages.',
'fields' => array(
'machine_name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The vocabulary machine name.',
),
'term_display_plugin' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The plugin used to display the term.',
),
'term_display_options' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'The plugin data for the term display.',
),
'associated_display_plugin' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The plugin used to display the associated content.',
),
'associated_display_options' => array(
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'The plugin data for the associated content display.',
),
),
'primary key' => array('machine_name'),
);
return $schema;
}
/**
* Implements hook_uninstall().
*/
function taxonomy_display_uninstall() {}
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Hooks for the taxonomy display module.
*/
/**
* Return just the term display sub-form.
*
* @return renderable array
*/
function taxonomy_display_ajax_term_display_callback($form, $form_state) {
return $form['additional_settings']['taxonomy_display']['term_display_form'];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function taxonomy_display_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
// Only proceed if we are on taxonomy_term entity type and the user has admin
// taxonomy display.
if ($form['#entity_type'] != 'taxonomy_term' || !user_access('administer taxonomy display')) {
return;
}
// Make things easy on users getting started with the module.
// We require the 'full' #view_mode to configure the taxonomy term page.
if ($form['#view_mode'] == 'default') {
$field = array(
'#title' => 'Term page display',
'#type' => 'fieldset',
);
// TODO: There's got to be a better way to find out if 'full' is enabled,
// need to look.
foreach ($form['additional_settings']['modes']['view_modes_custom']['#default_value'] as $value) {
if ($value == 'full') {
$field['#description'] = 'To alter the term page display you need to go to the <em>Taxonomy term page</em> view mode.';
break;
}
}
if (!isset($field['#description'])) {
$field['#description'] = 'Enable the <em>Taxonomy term page</em> view mode under the <em>Custom display settings</em> tab to alter the term page display.';
}
$form['additional_settings']['taxonomy_display'] = $field;
}
// Only proceed if we are on the full display mode.
if ($form['#view_mode'] != 'full') {
return;
}
$stored_settings = db_select('taxonomy_display', 'td')
->fields('td')
->condition('machine_name', $form['#bundle'])
->execute()
->fetchAssoc();
$form['additional_settings']['taxonomy_display'] = array(
'#description' => 'Configure how the taxonomy term display page should be presented when viewed for this vocabulary.',
'#title' => 'Term page display',
'#type' => 'fieldset',
);
$fieldset =& $form['additional_settings']['taxonomy_display'];
if (isset($form_state['values']['additional_settings']['taxonomy_display'])) {
$state_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'];
}
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' => 'term_display_form_replace',
),
'#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['storage']['term_display_last_handler'])) {
$last_handler = $form_state['storage']['term_display_last_handler'];
}
$form_state['storage']['term_display_last_handler'] = $selected;
$fieldset['term_display_form'] = array(
'#prefix' => '<div id="term_display_form_replace">',
'#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']);
}
else {
$term_display_class->formFieldset($fieldset['term_display_form'], array());
}
}
/**
* Implements of hook_menu_alter().
*/
function taxonomy_display_menu_alter(&$items) {
$items['taxonomy/term/%taxonomy_term']['page callback'] = 'taxonomy_display_taxonomy_term_page';
$items['taxonomy/term/%taxonomy_term']['title callback'] = 'taxonomy_display_taxonomy_term_title';
$items['taxonomy/term/%taxonomy_term']['file'] = 'taxonomy_display.module';
$items['taxonomy/term/%taxonomy_term']['module'] = 'taxonomy_display';
}
/**
* Implements hook_permission().
*/
function taxonomy_display_permission() {
$permissions = array(
'administer taxonomy display' => array(
'title' => t('Administer taxonomy display'),
'description' => t('Change the display settings for taxonomy term pages in each vocabulary.'),
),
);
return $permissions;
}
/**
* Retrieve an array of Taxonomy Display plugins.
*
* @param null|string $type
* Only retrieve plugins of a specific type, expected values 'term' and
* 'associated'. By default, NULL, this will return an array with both.
*
* @return array|void
* If an expected value is provided for $type, or no value is provided, an
* array will be returned. If an unexpected value is provided nothing will be
* returned.
*/
function taxonomy_display_plugins($type = NULL) {
// TODO: Add caching, likely not a big issue as this is only called in admin
// areas, but still.
$plugins = module_invoke_all('taxonomy_display_plugins');
// Expose our retrieved plugins to altering.
drupal_alter('taxonomy_display_plugins', $plugins);
if (is_null($type)) {
return $plugins;
}
if (array_key_exists($type, $plugins)) {
return $plugins[$type];
}
}
/**
* Implements hook_taxonomy_display_plugins().
*
* Taxonomy Display invokes this hook when looking for plugins for displaying
* taxonomy term and taxonomy term's associated content.
*/
function taxonomy_display_taxonomy_display_plugins() {
// To add custom plugins in your own hook implementation return an array with
// the format below:
return array(
// In the two arrays 'associated' and 'term', provide the implementing class
// name of your handler as the keys and the text to be displayed to the user
// for display as the value.
// Note: All of the class's files are in our module's .info files array.
'associated' => array(
// TODO: Add core, hidden, views
),
'term' => array(
'TaxonomyDisplayTermDisplayHandlerCore' => t('Core'),
'TaxonomyDisplayTermDisplayHandlerHidden' => t('Hidden'),
),
);
}
/**
* Page callback; displays all nodes associated with a term.
*
* @param $term
* A term object.
*
* @return
* The page content.
*/
function taxonomy_display_taxonomy_term_page($term) {
// TODO: Select record from the database and run through plugins.
module_load_include('inc', 'taxonomy', 'taxonomy.pages');
$build = taxonomy_term_page($term);
return $build;
}
/**
* Title callback; display title for term pages.
*
* @param $term
* A term object.
*
* @return
* The term name to be used as the page title.
*/
function taxonomy_display_taxonomy_term_title($term) {
return taxonomy_term_title($term);
}
/**
* Implements taxonomy_vocabulary_delete().
*
* @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
}
/**
* Implements taxonomy_vocabulary_update().
*
* @todo test this works
*/
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);
}
}
catch(Exception $e) {
drupal_set_message(t('db_update failed. Message = %message, query= %query',
array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
}
}
}
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