Commit ff38c2e7 authored by Nicholas Thompson's avatar Nicholas Thompson
Browse files

Work on porting Page Title to Drupal 7

parent 679d592c
......@@ -5,7 +5,7 @@ foreach (element_children($form['title']) as $key) {
$row = array();
$row[] = drupal_render($form['title'][$key]);
$row[] = drupal_render($form['pattern'][$key]);
$rows[] = $row;
}
......
; $Id$
name = Page Title
description = "Enhanced control over the page title (in the <head> tag)."
dependencies[] = token
core = 6.x
\ No newline at end of file
core = 7.x
files[] = page_title.module
files[] = page_title.admin.inc
......@@ -12,24 +12,82 @@ function page_title_install() {
function page_title_schema() {
$schema['page_title'] = array(
'fields' => array(
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'type' => array('type' => 'varchar', 'length' => 15, 'not null' => TRUE, 'default' => 'node'),
'id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'page_title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '')
),
'primary key' => array('nid'),
'primary key' => array('type', 'id'),
);
return $schema;
}
/**
* Implementation of hook_update_n().
*/
function page_title_update_6200() {
$ret = array();
if (db_column_exists('page_title', 'id')) {
return $ret;
}
db_create_table($ret, 'page_title_temp', array(
'fields' => array(
'type' => array('type' => 'varchar', 'length' => 15, 'not null' => TRUE, 'default' => 'node'),
'id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'page_title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '')
),
'primary key' => array('type', 'id'),
));
$ret[] = update_sql('INSERT INTO {page_title_temp} (id, page_title) SELECT nid, page_title FROM {page_title}');
db_rename_table($ret, 'page_title', 'page_title_old');
db_rename_table($ret, 'page_title_temp', 'page_title');
$display_settings = variable_get('page_title_display', array());
foreach ($display_settings as $type) {
if ($type) {
variable_set('page_title_type_'. $type .'_showfield', 1);
}
}
variable_del('page_title_display');
return $ret;
}
/**
* Implementation of hook_uninstall().
*/
function page_title_uninstall() {
db_query('DROP TABLE {page_title}');
drupal_uninstall_schema('page_title');
// Clear variables
variable_del('page_title_default');
variable_del('page_title_individual');
variable_del('page_title_front');
foreach (node_get_types('names') AS $type => $name) {
variable_del("page_title_display_$type");
variable_del('page_title_blog');
variable_del('page_title_user');
variable_del('page_title_user_showfield');
variable_del('page_title_pager_pattern');
// Clear the node specific variables
$types = node_type_get_names();
foreach ($types as $type => $name) {
variable_del("page_title_type_{$type}");
variable_del("page_title_type_{$type}_showfield");
}
}
\ No newline at end of file
// Clear the vocab specific variables
if(module_exists('taxonomy')) {
$vocabs = taxonomy_get_vocabularies();
foreach ($vocabs as $vid => $vocab) {
variable_del("page_title_vocab_{$vid}");
variable_del("page_title_vocab_{$vid}_showfield");
}
}
}
......@@ -15,24 +15,22 @@
/**
* Implementation of hook_help().
*/
function page_title_help($section) {
switch ($section) {
case 'admin/help#page_title':
$output = t('<p>Provides control over the &lt;title> element on a page using token patterns and an optional textfield to override the node title.</p>');
$output .= t('<p>Below are all the tokens you can use with <strong><em>Page Title</em></strong>:</p>');
$output .= theme('token_help');
break;
function page_title_help($path, $arg) {
$output = NULL;
switch ($path) {
case 'admin/content/page_title':
$output = t("<p>Drupal's default page title follows one of two patterns:</p>
<ol>
<li><strong>Default Page:</strong> <em>page title</em> | <em>site name</em></li>
<li><strong>Default Frontpage:</strong> <em>site name</em> | <em>site slogan</em></li>
</ol>
<p>The <strong>Page Title</strong> module lets you change these defaults in two ways. First, you can adjust the patterns below using the placeholders given. This will change the way the default page titles are created. Second, on every content creation form you can have the option of specifying a title that is different than the title of the node. If a value is provided, this will be used to generate the <code>[page-title]</code> placeholder. If left blank, <code>[page-title]</code> will inherit the node's title.</p>
<p><code>[page-title]</code> will default to the value returned from <code>drupal_get_title</code> if there is no overriden page title.</p>");
$output = '<p>'. t('Page Title provides control over the &lt;title> element on a page using token patterns and an optional textfield to override the title of the item (be it a node, term, user or other). The Token Scope column lets you know which tokens are available for this field (Global is always available). Please click on the <strong><em>more help&hellip;</em></strong> link below if you need further assistance.') .'</p>';
break;
case 'admin/content/page_title/types':
$output = t("<p>To display a 'Page Title' field on a node (just below the 'Title' field), you will need to enable it for the appropriate content types below.</p>");
case 'admin/help#page_title':
$output = '<p>'. t('Drupal\'s default page title follows one of two patterns:') .'</p>';
$items = array(
t('<strong>Default Page</strong>: <samp><em>page title</em> | <em>site name</em></samp>'),
t('<strong>Default Frontpage</strong>: <samp><em>site name</em> | <em>site slogan</em></samp>'),
);
$output .= theme('item_list', $items, NULL, 'ol');
$output .= '<p>'. t('The <strong>Page Title</strong> module lets you change these defaults in two ways. First, you can adjust the patterns below using the placeholders given. This will change the way the default page titles are created. Second, on enabled forms (curently node, term & user editing forms) you have the option of specifying a title that is different to the title of the item. This field only appears if the <em>Show Field</em> box is checked for the item. If a value is provided it will be used to generate the <samp>[site:page-title]</samp> placeholder however if it is left blank the <samp>[site:page-title]</samp> token will inherit the item\'s own title.') .'</p>';
$output .= '<p>'. t('The <samp>[site:page-title]</samp> token will default to the value returned from <samp>drupal_get_title</samp> if there is no value specified or no available page title field.') .'</p>';
$output .= '<p>'. t('Certain types of page title pattern have access to special tokens which others do not, depending on their <em>scope</em>. All patterns have access to the <strong>Global</strong> scope. Content type patterns have access to the <strong>Node</strong> tokens, vocabulary patterns have access to the <strong>Taxonomy</strong> tokens and finally the user patterns have access to the <strong>User</strong> tokens.') .'</p>';
break;
}
return $output;
......@@ -51,29 +49,13 @@ function page_title_perm() {
* Implementation of hook_menu().
*/
function page_title_menu() {
$items = array();
$items['admin/content/page_title'] = array(
'title' => t('Page titles'),
'description' => t('Enhanced control over the page titles (in the &lt;head&gt; tag).'),
'title' => 'Page titles',
'description' => 'Enhanced control over the page titles (in the &lt;head&gt; tag).',
'page callback' => 'drupal_get_form',
'page arguments' => array('page_title_admin_settings'),
'access callback' => 'user_access',
'access arguments' => array('administer page titles'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/content/page_title/settings'] = array(
'title' => t('Default settings'),
'description' => t('Control the page title settings.'),
'access callback' => 'user_access',
'access arguments' => array('administer page titles'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/content/page_title/types'] = array(
'title' => t('Content type settings'),
'description' => t('Control the display of the Page title field.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('page_title_admin_types'),
'access callback' => 'user_access',
'access arguments' => array('administer page titles'),
'type' => MENU_LOCAL_TASK,
);
......@@ -83,274 +65,505 @@ function page_title_menu() {
/**
* Implementation of hook_theme
* Implementation of hook_theme().
*/
function page_title_theme() {
return array(
'page_title_admin_settings' => array(
'template' => 'page_title-admin-settings-form',
'arguments' => array('form' => NULL),
),
'page_title_preprocess_page' => array(
'arguments' => array('vars' => NULL),
),
);
}
/**
* Displays the form for the standard settings tab.
* Implementation of hook_node_type().
*
* @return
* array A structured array for use with Forms API.
* Updates settings after a node type change.
*/
function page_title_admin_settings() {
//Define the titles
$form['title']['page_title_default'] = array('#type' => 'markup', '#value' => 'Default Pattern');
$form['title']['page_title_front'] = array('#type' => 'markup', '#value' => 'Frontpage Pattern');
//Define the 'default' token patterns
$form['pattern']['page_title_default'] = array('#type' => 'textfield', '#default_value' => variable_get('page_title_default', '[page-title] | [site-name]'), '#maxlength' => 128, '#description' => t('This is the pattern used in a situation where a pattern is not defined specifically for a content type below.'));
$form['pattern']['page_title_front'] = array('#type' => 'textfield', '#default_value' => variable_get('page_title_front', '[site-name] | [site-slogan]'), '#maxlength' => 128, '#description' => t('This is the frontpage pattern.'));
//Definate the patterns per-node-type
$types = node_get_types();
foreach ($types as $type) {
$key = 'page_title_type_' . $type->type;
$form['title'][$key] = array('#type' => 'markup', '#value' => t('Pattern for %type', array('%type' => $type->name)), );
$form['pattern'][$key] = array('#type' => 'textfield', '#default_value' => variable_get($key, ''), '#maxlength' => 128, '#description' => t('If left blank, will inherit from default settings.'));
function page_title_node_type($op, $info) {
// Handle a content type rename
if ($op == 'update' && !empty($info->old_type) && $info->type != $info->old_type) {
// Load the old node type settings.
$temp = variable_get('page_title_type_'. $info->old_type, '');
// If the settings aren't empty, then save them into the new type
if (!empty($temp)) {
variable_set('page_title_type_'. $info->type, $temp);
}
// Delete the old setting
variable_del('page_title_type_'. $info->old_type);
// Essentially, do the same as above but with the _showfield suffix for the node type
$temp = variable_get('page_title_type_'. $info->old_type .'_showfield', 0);
if ($temp) {
variable_set('page_title_type_'. $info->type .'_showfield', $temp);
}
variable_del('page_title_type_'. $info->old_type .'_showfield');
}
// If deleted, remove the variables
if ($op == 'delete') {
variable_del('page_title_type_'. $info->type);
variable_del('page_title_type_'. $info->type .'_showfield');
}
//Add the system buttons to the form
$form = system_settings_form($form);
//Overide the theme function back to our own one
$form['#theme'] = 'page_title_admin_settings';
return $form;
}
/**
* Displays the form for the "Content creation types" tab.
*
* @return
* array A structured form array for use with Forms API.
* Implementation of hook_form_alter().
*/
function page_title_admin_types() {
$form['page_title_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Content creation forms'),
'#description' => t('Display a "Page title" field for these content types.'),
);
function page_title_form_alter(&$form, $form_state, $form_id) {
// If we dont have permission to set the title then we need to abort this alter now!
if (!user_access('set page title')) return;
$form['page_title_fieldset']['page_title_display'] = array(
'#type' => 'checkboxes',
'#default_value' => variable_get('page_title_display', array()),
'#options' => node_get_types('names'),
);
// Check we're editing a node and also check that the node type's 'show field' is enabled
if ($form['#id'] == 'node-form') {
$key = 'page_title_type_'. $form['type']['#value'] .'_showfield';
if (variable_get($key, 0)) {
$page_title = isset($form['#node']->page_title) ? $form['#node']->page_title : NULL;
$form['page_title'] = array(
'#type' => 'fieldset',
'#title' => t('Page title settings'),
'#collapsible' => TRUE,
'#collapsed' => empty($page_title),
'#group' => 'additional_settings',
'#weight' => 35,
'#attached_js' => array(drupal_get_path('module', 'page_title') . '/page_title.js'),
);
$form['page_title']['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
'#default_value' => $page_title,
'#size' => 60,
'#maxlength' => 255,
);
}
}
// Check we're editing a taxonomy term and also check that the terms vocabulary's 'show field' is enabled
elseif ($form_id == 'taxonomy_form_term') {
$key = 'page_title_vocab_'. $form['vid']['#value'] .'_showfield';
if (variable_get($key, 0)) {
$form['advanced']['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
'#default_value' => isset($form['tid']) ? page_title_load_title($form['tid']['#value'], 'term') : '',
'#size' => 60,
'#maxlength' => 255,
'#weight' => -20,
);
}
}
// Check we're editing a user profile and also check that the user settings's have 'show field' enabled
elseif ($form_id == 'user_profile_form') {
if (variable_get('page_title_user_showfield', 0)) {
$form['account']['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
'#default_value' => page_title_load_title($form['_account']['#value']->uid, 'user'),
'#size' => 60,
'#maxlength' => 255,
'#weight' => 20,
);
}
}
elseif ($form_id == 'node_type_form') {
$form['page_title'] = array(
'#type' => 'fieldset',
'#title' => t('Page Title Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
);
$form['page_title']['show_field'] = array(
'#type' => 'checkboxes',
'#title' => t('Page Title Field'),
'#description' => t('If checked, the <em>Page Title</em> field will appear on the node edit form for those who have permission to set the title.'),
'#options' => array(
'show_field' => t('Show field'),
),
'#default_value' => array('show_field' => variable_get('page_title_type_'. $form['#node_type']->type .'_showfield', 0) ? 'show_field' : 0),
);
// Add save button, etc to the form.
return system_settings_form($form);
$form['page_title']['pattern'] = array(
'#type' => 'textfield',
'#title' => t('Page Title Pattern'),
'#default_value' => variable_get('page_title_type_'. $form['#node_type']->type, ''),
'#description' => t('Enter the <em>Page Title</em> pattern you want to use for this node type. For more information, please use the !link settings page', array('!link' => l('Page Title', 'admin/content/page_title'))),
);
$form['#submit'][] = 'page_title_node_type_form_submit';
}
}
/**
* Implementation of hook_node_type().
*
* Updates settings after a node type change.
* Submite handler for the node_type_form element added in the hook_form_alter() above.
*/
function page_title_node_type($op, $info) {
if ($op == 'update' && !empty($info->old_type) and $info->type != $info->old_type) {
//Get the display settings for node types
$display = variable_get('page_title_display', array());
//If the old one is set, set the new one
if ($display[$info->old_type]) {
$display[$info->type] = $info->type;
function page_title_node_type_form_submit($form, &$form_state) {
$show_field = $form_state['values']['page_title']['show_field']['show_field'] ? 1 : 0;
variable_set('page_title_type_'. $form_state['values']['type'] .'_showfield', $show_field);
variable_set('page_title_type_'. $form_state['values']['type'], $form_state['values']['page_title']['pattern']);
// For some reason the node module adds the fieldset as a separate entry in the variables table... we dont want this!
variable_del('page_title_'. $form_state['values']['type']);
}
/**
* Implementation of hook_node_load().
*/
function page_title_node_load($nodes) {
foreach ($nodes as $node) {
if ($page_title = page_title_load_title($node->nid, 'node')) {
$node->page_title = $page_title;
}
//Otherwise the new type is disabled like the old one was
else {
$display[$info->type] = 0;
}
}
/**
* Implementation of hook_node_insert().
*/
function page_title_node_insert($node) {
if (user_access('set page title') && isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0) {
db_insert('page_title')->fields(array('type' => 'node', 'id' => $node->nid, 'page_title' => $node->page_title))->execute();
}
}
/**
* Implementation of hook_node_update().
*/
function page_title_node_update($node) {
if (user_access('set page title') && isset($node->page_title) && drupal_strlen(trim($node->page_title)) > 0) {
db_merge('page_title')->key(array('type' => 'node', 'id' => $node->nid))->fields(array('page_title' => $node->page_title))->execute();
}
}
/**
* Implementation of hook_node_delete().
*/
function page_title_node_delete($node) {
db_delete('page_title')->condition('type', 'node')->condition('id', $node->nid)->execute();
}
/**
* Implementation of hook_taxonomy_term_update().
*/
function page_title_taxonomy_term_update($term) {
if (user_access('set page title')) {
if (isset($term->page_title) && drupal_strlen(trim($term->page_title)) > 0) {
db_merge('page_title')->key(array('type' => 'term', 'id' => $term->tid))->fields(array('page_title' => $term->page_title))->execute();
}
//Unset the old type
unset($display[$info->old_type]);
//Save the settings
variable_set('page_title_display', $display);
//Get the old pattern, if set
$old_pattern = variable_get('page_title_type_' . $info->old_type, '');
//If it was set then set the new one to whatever the old pattern was
if (!empty($old_pattern)) {
variable_set('page_title_type_' . $info->type, $old_pattern);
else {
page_title_taxonomy_term_delete($term);
}
//Delete the old pattern
variable_del('page_title_type_' . $info->old_type);
}
}
/**
* Implementation of hook_form_alter().
* Implementation of hook_taxonomy_term_delete().
*/
function page_title_form_alter(&$form, $form_state, $form_id) {
//If we dont have permission to set the title then we need to abort this alter now!
if (!user_access('set page title')) return;
$display = variable_get('page_title_display', array());
//Check we're editing a node form and also check that the node type 'value' is enabled
if ($form['#id'] == 'node-form' && $display[$form['type']['#value']]) {
$form['page_title'] = array(
'#type' => 'textfield',
'#title' => t('Page title'),
'#description' => t('Optionally specify a different title to appear in the &lt;title&gt; tag of the page.'),
'#default_value' => $form['#node']->page_title,
'#size' => 60,
'#maxlength' => 255,
'#weight' => -4,
);
function page_title_taxonomy_term_delete($term) {
db_delete('page_title')->condition('type', 'term')->condition('id', $term->tid)->execute();
}
/**
* Implementation of hook_taxonomy_term_insert().
*/
function page_title_taxonomy_term_insert($term) {
if (user_access('set page title') && isset($term->page_title) && drupal_strlen(trim($term->page_title)) > 0) {
db_insert('page_title')->fields(array('type' => 'term', 'id' => $term->tid, 'page_title' => $term->page_title))->execute();
}
}
/**
* Implementation of hook_nodeapi().
* Implementation of hook_user().
*/
function page_title_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
/*
function page_title_user($op, &$edit, &$account) {
dpm(func_get_args());
switch ($op) {
case 'update':
db_query("DELETE FROM {page_title} WHERE nid = %d", $node->nid);
// fallthrough to insert intentional!
if (user_access('set page title')) {
db_delete('page_title')->condition('type', 'user')->condition('id', $account->uid)->execute();
}
// Fallthrough to insert is intentional!
case 'insert':
if (isset($node->page_title) && strlen(trim($node->page_title)) > 0) {
db_query("INSERT INTO {page_title} VALUES (%d, '%s')", $node->nid, $node->page_title);
if (isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0 && user_access('set page title')) {
db_insert('page_title')->fields(array('type' => 'user', 'id' => $account->uid, 'page_title' => $edit['page_title']))->execute();
}
break;
}
}
*/
case 'delete':
db_query('DELETE FROM {page_title} WHERE nid = %d', $node->nid);
break;
case 'load':
return array('page_title' => page_title_node_get_title($node->nid));
function page_title_user_insert(&$edit, &$account, $category) {
if (user_access('set page title') && isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0) {
db_insert('page_title')->fields(array('type' => 'user', 'id' => $account->uid, 'page_title' => $edit['page_title']))->execute();
}
}
function page_title_user_update(&$edit, &$account, $category) {
if (user_access('set page title')) {
if (isset($edit['page_title']) && drupal_strlen(trim($edit['page_title'])) > 0) {
db_merge('page_title')->key(array('type' => 'user', 'id' => $account->uid))->fields(array('page_title' => $edit['page_title']))->execute();
}
else {
db_delete('page_title')->condition('type', 'user')->condition('id', $account->uid)->execute();
}
}
}
/**
* Implementation of hook_user_cancel().
*/
function page_title_user_cancel(&$edit, &$account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish' : break;
case 'user_cancel_reassign' : break;
case 'user_cancel_delete' :
db_delete('page_title')->condition('type', 'user')->condition('id', $account->uid)->execute();
break;
}
}
/**
* Simple wrapper function to get the currently set title for a page
*
* @return unknown
* @return
* string the title for the current page
*/
function page_title_get_title() {
$display_options = variable_get('page_title_display', array());
$node = ((arg(0) == 'node') && (is_numeric(arg(1)))) ? node_load(arg(1)) : NULL;
if ($display_options[$node->type] && !empty($node->page_title)) {
return check_plain(strip_tags($node->page_title));
// If we're looking at a node or a comment on a node, get the node object from the menu system.
if ((arg(0) == 'node' && is_numeric(arg(1))) || (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) && module_exists('comment')) {
$node = menu_get_object();
// If the node has a custom page title and the node type is configured to have a custom page title (ie, it's not a leftover from a previous setting), then use it.
if (!empty($node->page_title) && variable_get('page_title_type_'. $node->type .'_showfield', 0)) {
$title = check_plain(strip_tags($node->page_title));
}
}
else {
return strip_tags(drupal_get_title());
// If we're looking at either a user profile page or a users blog page, get the user title
elseif (($user = menu_get_object('user_uid_optional')) || ($user = menu_get_object('user'))) {
if (variable_get('page_title_user_showfield', 0) && ($user_title = page_title_load_title($user->uid, 'user'))) {
$title = check_plain(strip_tags($user_title));
}
}
// If we're looking at a taxonomy term page, get the term title
elseif (module_exists('taxonomy') && ($term = menu_get_object('taxonomy_term', 2))) {
if (variable_get('page_title_vocab_'. $term->vid .'_showfield', 0) && ($term_title = page_title_load_title($term->tid, 'term'))) {
$title = check_plain(strip_tags($term_title));
}
}
}
// If nothing above set a title, give the legacy function a chance to act
if (empty($title)) {
$title = check_plain(strip_tags(page_title_set_title()));
}