diff --git a/taxonomy_title.admin.inc b/taxonomy_title.admin.inc index a0e401a..aeb14bb 100644 --- a/taxonomy_title.admin.inc +++ b/taxonomy_title.admin.inc @@ -7,18 +7,24 @@ function taxonomy_title_admin_settings(){ $form = array(); - + // Get all taxonomy vocabularies. $vocabs = taxonomy_get_vocabularies(); - + // Set up place holders for options. $heading_options = array(); $page_title_options = array(); - + // Set up holders for default values. $heading_defaults = variable_get('taxonomy_title_headings', array()); $page_title_defaults = variable_get('taxonomy_title_page_titles', array()); - + + $form['settings'] = array( + '#theme' => 'taxonomy_title_admin_settings', + ); + + $link = theme('token_tree_link', array('text' => 'Browse available tokens', 'token_types' => array('term'))); + foreach ($vocabs as $vid => $vocab) { $heading_options[$vid] = $vocab->name; $page_title_options[$vid] = $vocab->name; @@ -28,24 +34,26 @@ function taxonomy_title_admin_settings(){ if (!isset($page_title_defaults[$vid])) { $page_title_defaults[$vid] = $vid; } + + $form['settings']['taxonomy_title_default_' . $vid] = array( + '#type' => 'textfield', + '#description' => t('Leave blank for none.') . ' ' . $link, + '#default_value' => variable_get('taxonomy_title_default_' . $vid, ''), + ); } - $form['settings'] = array( - '#theme' => 'taxonomy_title_admin_settings', - ); - $form['settings']['taxonomy_title_headings'] = array( '#type' => 'checkboxes', '#options' => $heading_options, '#default_value' => $heading_defaults, ); - + if (!module_exists('page_title') && !module_exists('metatag')) { $form['settings']['taxonomy_title_page_titles'] = array( '#type' => 'checkboxes', '#options' => $page_title_options, '#default_value' => $page_title_defaults, - ); + ); } else { $form['settings']['taxonomy_title_page_titles'] = array( @@ -56,13 +64,13 @@ function taxonomy_title_admin_settings(){ ); $form['settings']['notice'] = array( '#weight' => 2, - '#markup' => '

' . t('* Since you have either the page title module or - the metatag module enabled, this module will be unable to affect the - title tags of your pages. If you would like taxonomy titles to appear - in your title tags, please configure that module to use the provided + '#markup' => '

' . t('* Since you have either the page title module or + the metatag module enabled, this module will be unable to affect the + title tags of your pages. If you would like taxonomy titles to appear + in your title tags, please configure that module to use the provided tokens. Example: [term-title]') . '

', ); } - + return system_settings_form($form); -} \ No newline at end of file +} diff --git a/taxonomy_title.module b/taxonomy_title.module index 5ed7c30..7ecc37b 100755 --- a/taxonomy_title.module +++ b/taxonomy_title.module @@ -32,12 +32,16 @@ function taxonomy_title_menu(){ */ function taxonomy_title_form_taxonomy_form_term_alter(&$form, &$form_state) { if (!(isset($_POST['op']) && $_POST['op'] == t('Delete')) || isset($_POST['confirm'])) { - $title = _taxonomy_title_get_title($form['tid']['#value']); + $title = _taxonomy_title_get_title($form['tid']['#value'], FALSE); + $alt = 'the term name'; + if ($global_default = variable_get('taxonomy_title_default_' . $form['#term']['vid'], FALSE)) { + $alt = 'the global default pattern of ' . token_replace($global_default, array('term' => $form_state['term'])) . ''; + } $form['taxonomy_title'] = array( '#type' => 'textfield', '#title' => t('Term page heading'), '#default_value' => $title, - '#description' => t('This is the title you will see in the heading tag on your taxonomy term page. If left blank, the term name will be used.'), + '#description' => t('If left blank !alt will be used.', array('!alt' => $alt)), '#weight' => -4, ); } @@ -95,24 +99,32 @@ function taxonomy_title_process_page(&$variables) { if (!empty($title)){ $term = taxonomy_term_load($tid); - $settings = taxonomy_title_get_settings(); - $heading_settings = $settings['taxonomy_title_headings']; - $page_title_settings = $settings['taxonomy_title_page_titles']; + $heading_settings = variable_get('taxonomy_title_headings', array()); + $page_title_settings = variable_get('taxonomy_title_page_titles', array()); - drupal_set_title($title); - if ($new_title = drupal_get_title()) { - - // Assure the page title (heading) is set. - if ($heading_settings[$term->vid] != 0) { - $variables['title'] = $new_title; - } - - // Set the HTML title tag. - if (($page_title_settings[$term->vid] != 0) && (!module_exists('page_title'))){ - $head_title = array(strip_tags($new_title), variable_get('site_name', 'Drupal')); - $variables['head_title'] = implode(' | ', $head_title); - } + // If there no setting, assume setting is ON. + $affect_heading = (!isset($heading_settings[$term->vid]) || $heading_settings[$term->vid] != 0); + $affect_title = (!isset($page_title_settings[$term->vid]) || $page_title_settings[$term->vid] != 0); + + // Set the page heading. + if ($affect_heading) { + $variables['title'] = $title; + } + + // Set the HTML title tag. + $site_name = variable_get('site_name', 'Drupal'); + if ($affect_title && (!module_exists('page_title')) && !module_exists('metatag')){ + $head_title = array($title, $site_name); + $variables['head_title'] = implode(' | ', $head_title); + } + else { + // Unset things, just to be safe. + $title = check_plain($term->name); + $head_title = array($title, $site_name); + $variables['head_title'] = implode(' | ', $head_title); } + + drupal_set_title($title); } } } @@ -120,13 +132,22 @@ function taxonomy_title_process_page(&$variables) { /** * Retrieves the term title. * - * @param $tid + * @param (int) $tid * The taxonomy term id of the term to delete. - * @return + * @param (bool) $default + * Whether to return a global default value if no specific value is provided. + * + * @return (string) * The taxonomy term title for the term. */ -function _taxonomy_title_get_title($tid) { +function _taxonomy_title_get_title($tid, $default = TRUE) { $title = db_query("SELECT title FROM {taxonomy_title} WHERE tid = :tid", array(':tid' => $tid))->fetchField(); + if ($title == FALSE && $default == TRUE) { + // Check for global default value. + $term = taxonomy_term_load($tid); + $default = variable_get('taxonomy_title_default_' . $term->vid, ''); + $title = token_replace($default, array('term' => $term)); + } if (function_exists('i18n_string_translate')) { $title = i18n_string_translate(array('taxonomy_title', 'term', $tid, 'title'), $title, array('sanitize' => FALSE)); } @@ -195,32 +216,6 @@ function _taxonomy_title_delete_title($tid) { } /** - * Helper function: sets all default usage to ON. - */ -function taxonomy_title_get_settings() { - $vocabs = taxonomy_get_vocabularies(); - - $heading_defaults = variable_get('taxonomy_title_headings', array()); - $page_title_defaults = variable_get('taxonomy_title_page_titles', array()); - - foreach ($vocabs as $vid => $vocab) { - if (!isset($heading_defaults[$vid])) { - $heading_defaults[$vid] = $vid; - } - if (!isset($page_title_defaults[$vid])) { - $page_title_defaults[$vid] = $vid; - } - } - - $settings = array( - 'taxonomy_title_headings' => $heading_defaults, - 'taxonomy_title_page_titles' => $page_title_defaults, - ); - - return $settings; -} - -/** * Implements hook_i18n_string_info() */ function taxonomy_title_i18n_string_info() { diff --git a/taxonomy_title.theme.inc b/taxonomy_title.theme.inc index 9d175e3..b5378f2 100644 --- a/taxonomy_title.theme.inc +++ b/taxonomy_title.theme.inc @@ -12,14 +12,15 @@ function theme_taxonomy_title_admin_settings($variables) { if (!module_exists('page_title') && !module_exists('metatag')) { $title_head = t('Affect title tag'); - } + } else { $title_head = t('Affect title tag *'); } $header = array( - t('Terms in vocabulary'), - array('data' => t('Affect Heading tag'), 'class' => 'checkbox'), + t('Terms in vocabulary'), + t('Global default value'), + array('data' => t('Affect Heading tag'), 'class' => 'checkbox'), array('data' => $title_head, 'class' => 'checkbox'), ); @@ -27,10 +28,12 @@ function theme_taxonomy_title_admin_settings($variables) { foreach($form['taxonomy_title_page_titles']['#options'] as $vid => $name) { $row = array(); $row[] = check_plain($name); + $row[] = drupal_render($form['taxonomy_title_default_' . $vid]); unset($form['taxonomy_title_headings'][$vid]['#title']); $row[] = array('data' => drupal_render($form['taxonomy_title_headings'][$vid]), 'class' => 'checkbox'); unset($form['taxonomy_title_page_titles'][$vid]['#title']); $row[] = array('data' => drupal_render($form['taxonomy_title_page_titles'][$vid]), 'class' => 'checkbox'); + $rows[] = $row; } @@ -41,7 +44,12 @@ function theme_taxonomy_title_admin_settings($variables) { $title_path = drupal_get_path('module', 'taxonomy_title') . '/includes/' . 'title.png'; $title_example = theme('image', array('path' => $title_path)); - $rows[] = array(t('Example'), array('data' => $heading_example, 'class' => 'checkbox'), array('data' => $title_example, 'class' => 'checkbox')); + $rows[] = array( + t('Example'), + array('data' => ''), + array('data' => $heading_example, 'class' => 'checkbox'), + array('data' => $title_example, 'class' => 'checkbox'), + ); // Create the table inside the form. $form['settings'] = array(