t('Meta tags'), 'description' => t('Meta tags fieldset.'), 'weight' => 10, ); return $extras; } /** * Implementation of hook_form_alter(). */ function nodewords_form_alter(&$form, &$form_state, $form_id) { $bool = ( isset($form['type']) && isset($form['type']['#value']) && $form_id == $form['type']['#value'] . '_node_form' && variable_get('nodewords_edit_metatags_' . $form['type']['#value'], TRUE) ); if ($bool) { $id = $form['nid']['#value']; if (!empty($form_state['values']['nodewords'])) { $tags = $form_state['values']['nodewords']; } elseif (isset($id) && is_numeric($id)) { $tags = nodewords_load_tags(NODEWORDS_MT_TYPE_NODE, $id); } else { $tags = array(); } $form['nodewords'] = nodewords_form( NODEWORDS_MT_TYPE_NODE, $tags, array( 'page:permissions:additional' => 'administer nodes', 'tag_options' => array('node_type' => $form['type']['#value']), ) ); } } /** * Implementation of hook_form_FORM_ID_alter(). */ function nodewords_form_node_type_form_alter(&$form, &$form_state) { if (isset($form['#node_type'])) { $form['nodewords'] = array( '#type' => 'fieldset', '#title' => t('Meta tags settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['nodewords']['nodewords_edit_metatags'] = array( '#type' => 'checkbox', '#title' => t('Allow editing of meta tags'), '#description' => t('If selected, the node edit form will allow the users with the right permissions to edit the meta tags associated with nodes of this content type.'), '#default_value' => variable_get('nodewords_edit_metatags_' . $form['#node_type']->type, TRUE), ); foreach (nodewords_get_possible_tags() as $name => $info) { $function = $info['tag:function:prefix'] . '_settings_form'; $options = array( 'parameters' => !empty($info['tag:function:parameters']) ? $info['tag:function:parameters'] : array(), ); if (function_exists($function)) { $function($form, 'node_type_form', $options); } } } } /** * Implementation of hook_form_FORM_ID_alter(). */ function nodewords_form_taxonomy_form_term_alter(&$form, &$form_state) { $bool = (isset($form['tid']['#value']) && !isset($form_state['confirm_delete']) && !isset($form_state['confirm_parents']) ); if ($bool) { $id = $form['tid']['#value']; if (!empty($form_state['values']['nodewords'])) { $tags = $form_state['values']['nodewords']; } elseif (isset($id) && is_numeric($id)) { $tags = nodewords_load_tags(NODEWORDS_MT_TYPE_TERM, $id); } else { $tags = array(); } $form['nodewords'] = nodewords_form( NODEWORDS_MT_TYPE_TERM, $tags, array() ); $form['submit']['#weight'] = 45; $form['delete']['#weight'] = 50; } } /** * Implementation of hook_form_FORM_ID_alter(). */ function nodewords_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) { if (isset($form['vid']['#value'])) { $id = $form['vid']['#value']; if (!empty($form_state['values']['nodewords'])) { $tags = $form_state['values']['nodewords']; } elseif (isset($id) && is_numeric($id)) { $tags = nodewords_load_tags(NODEWORDS_MT_TYPE_VOCABULARY, $id); } else { $tags = array(); } $form['nodewords'] = nodewords_form( NODEWORDS_MT_TYPE_VOCABULARY, $tags, array() ); $form['submit']['#weight'] = 45; $form['delete']['#weight'] = 50; } } /** * Implemenation of hook_help(). */ function nodewords_help($path, $arg) { switch ($path) { case 'admin/content/nodewords/meta-tags': $output = '
' . t('On this page you can enter the default values for the meta tags of your site.') . '
'; break; case 'admin/content/nodewords/meta-tags/errorpage_403': $output = '' . t('On this page you can enter the meta tags for the access denied
error page of your site.') . '
' . t('On this page you can enter the meta tags for the page not found
error page of your site.') . '
' . t('On this page you can enter the meta tags for the front page of your site.') . '
'; break; case 'admin/content/nodewords/meta-tags/other': $output = '' . t('On this page you can enter the meta tags for other pages of your site. Add meta tags for a new page.', array('@add_metatags' => url('admin/content/nodewords/meta-tags/other/add'))) . '
'; break; case 'admin/content/nodewords/meta-tags/pager': $output = '' . t('On this page you can enter the meta tags for pages that are part of a pager. This values will be used only when the option %option
in the settings page is not selected.', array('%option' => t('Repeat meta tags for lists'), '@settings_page' => url('admin/content/nodewords/settings'))) . '
' . t('On this page you can enter the meta tags for tracker pages of your site.') . '
'; break; default: $output = ''; break; } return $output; } /** * Implementation of hook_menu(). */ function nodewords_menu() { $admin_access = array('administer meta tags'); $items = array(); $items['admin/content/nodewords'] = array( 'title' => 'Meta tags', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_settings_form'), 'description' => 'Configure HTML meta tags for all the content.', 'access arguments' => $admin_access, 'type' => MENU_NORMAL_ITEM, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/settings'] = array( 'title' => 'Settings', 'access arguments' => $admin_access, 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags'] = array( 'title' => 'Default and specific meta tags', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_tags_form'), 'access arguments' => $admin_access, 'type' => MENU_LOCAL_TASK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/default'] = array( 'title' => 'Default values', 'access arguments' => $admin_access, 'type' => MENU_DEFAULT_LOCAL_TASK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/errorpage_403'] = array( 'title' => 'Error 403 page', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_tags_form', NODEWORDS_MT_TYPE_ERRORPAGE, '403'), 'access arguments' => $admin_access, 'type' => MENU_LOCAL_TASK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/errorpage_404'] = array( 'title' => 'Error 404 page', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_tags_form', NODEWORDS_MT_TYPE_ERRORPAGE, '404'), 'access arguments' => $admin_access, 'type' => MENU_LOCAL_TASK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/frontpage'] = array( 'title' => 'Front page', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_tags_form', NODEWORDS_MT_TYPE_FRONTPAGE), 'access arguments' => $admin_access, 'type' => MENU_LOCAL_TASK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/pager'] = array( 'title' => 'Pager', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_tags_form', NODEWORDS_MT_TYPE_PAGER), 'access arguments' => $admin_access, 'type' => MENU_LOCAL_TASK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/tracker'] = array( 'title' => 'Tracker pages', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_tags_form', NODEWORDS_MT_TYPE_TRACKER), 'access arguments' => $admin_access, 'type' => MENU_LOCAL_TASK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/other'] = array( 'title' => 'Other pages', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_pages_overview'), 'access arguments' => $admin_access, 'type' => MENU_LOCAL_TASK, 'weight' => 5, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/other/add'] = array( 'title' => 'Add page meta tags', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_pages_edit'), 'access arguments' => $admin_access, 'type' => MENU_CALLBACK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/other/delete/%nodewords_page'] = array( 'title' => 'Delete page meta tags', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_pages_confirm_delete', 6), 'access arguments' => $admin_access, 'parent' => 'admin/content/nodewords/meta-tags/other', 'type' => MENU_CALLBACK, 'file' => 'nodewords.admin.inc', ); $items['admin/content/nodewords/meta-tags/other/edit/%nodewords_page'] = array( 'title' => 'Edit page meta tags', 'page callback' => 'drupal_get_form', 'page arguments' => array('nodewords_pages_edit', 6), 'access arguments' => $admin_access, 'parent' => 'admin/content/nodewords/meta-tags/other', 'type' => MENU_CALLBACK, 'file' => 'nodewords.admin.inc', ); return $items; } /** * Implementation of hook_node_operations(). */ function nodewords_node_operations() { $operations = array( 'delete_metatags' => array( 'label' => t('Delete meta tags'), 'callback' => 'nodewords_mass_delete_tags', 'callback arguments' => array('type' => NODEWORDS_MT_TYPE_NODE), ), ); return $operations; } /** * Implementation of hook_nodeapi(). */ function nodewords_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { switch ($op) { case 'delete': nodewords_delete_tags(NODEWORDS_MT_TYPE_NODE, $node->nid); break; case 'insert': case 'update': if (isset($node->nodewords)) { nodewords_save_tags(NODEWORDS_MT_TYPE_NODE, $node->nid, $node->nodewords, TRUE); } break; case 'update index': $output = ''; if (isset($node->nodewords)) { $tags_info = nodewords_get_possible_tags(); foreach ($node->nodewords as $name => $content) { // Avoid the disabled meta tags. if (!isset($info[$name])) { continue; } if (!empty($tags_info[$name]['tag:template:index'])) { $output .= strtr($tags_info[$name]['tag:template:index'], '%content', $content); } } } return $output; case 'load': return array( 'nodewords' => nodewords_load_tags(NODEWORDS_MT_TYPE_NODE, $node->nid), ); } } /** * Implementation of hook_perm(). */ function nodewords_perm() { return array('administer meta tags'); } /** * Implementation of hook_preprocess_page(). */ function nodewords_preprocess_page(&$variables) { list($type, $ids) = _nodewords_detect_type_and_ids(); drupal_set_html_head(nodewords_output_tags(nodewords_get_tags($type, $ids))); $variables['head'] = drupal_get_html_head(); } /** * Implementation of hook_taxonomy(). */ function nodewords_taxonomy($op, $type, $object = NULL) { if ($type == 'term') { $id = $object['tid']; $type = NODEWORDS_MT_TYPE_TERM; } elseif ($type == 'vocabulary') { $id = $object['vid']; $type = NODEWORDS_MT_TYPE_VOCABULARY; } else { return; } switch ($op) { case 'delete': nodewords_delete_tags($type, $id); break; case 'insert': case 'update': if (isset($object['nodewords'])) { nodewords_save_tags($type, $id, $object['nodewords'], TRUE); } break; } } /** * Implementation of hook_theme(). */ function nodewords_theme() { return array( 'nodewords_pages_overview' => array( 'arguments' => array('form' => array()), ), ); } /** * Implementation of hook_user(). */ function nodewords_user($op, &$edit, &$account, $category = NULL) { switch ($op) { case 'load': if (variable_get('nodewords_enable_user_metatags', TRUE)) { $account->nodewords = nodewords_load_tags(NODEWORDS_MT_TYPE_USER, $account->uid); } break; case 'delete': nodewords_delete_tags(NODEWORDS_MT_TYPE_USER, $account->uid); break; case 'insert': case 'update': if (isset($edit['nodewords'])) { nodewords_save_tags(NODEWORDS_MT_TYPE_USER, $account->uid, $edit['nodewords'], TRUE); } break; case 'form': if ($category == 'account') { if (variable_get('nodewords_enable_user_metatags', TRUE)) { $tags = nodewords_load_tags(NODEWORDS_MT_TYPE_USER, $account->uid); $form['nodewords'] = nodewords_form( NODEWORDS_MT_TYPE_USER, $tags, array( 'page:permissions:additional' => 'administer users', ) ); return $form; } } break; } } /** * Implementation of hook_user_operations(). */ function nodewords_user_operations() { $operations = array( 'delete_metatags' => array( 'label' => t('Delete meta tags'), 'callback' => 'nodewords_mass_delete_tags', 'callback arguments' => array('type' => NODEWORDS_MT_TYPE_USER), ), ); return $operations; } /***************************************************************************** * Public functions. ****************************************************************************/ /** * Delete tags from table. */ function nodewords_delete_tags($type, $id) { db_query("DELETE FROM {nodewords} WHERE type = %d AND id = '%s'", $type, $id); if ($type == NODEWORDS_MT_TYPE_PAGE) { db_query("DELETE FROM {nodewords_custom} WHERE path = '%s'", $id); } } /** * Return the form used to set the meta tags values. * * @param $type * The object to which the meta tags are associated (node, user, taxonomy * term, etc...). * @param $tags * The meta tags array as returned by nodewords_load_tags(). * * @return * An array as requested by the form API. */ function nodewords_form($type, $tags, $options = array()) { $default_options = array( 'fieldset' => TRUE, 'fieldset:title' => t('Meta tags'), 'fieldset:weight' => 20, ); $edit_tags = variable_get('nodewords_edit', array()); $form = array(); $options += $default_options; $tag_options = array( 'default' => nodewords_load_tags(), 'type' => $type, ); $tags_info = nodewords_get_possible_tags(); $token_module_enabled = module_exists('token'); $tokens_support = FALSE; if (isset($options['tag_options'])) { $tag_options = array_merge($options['tag_options'], $tag_options); } foreach ($tags_info as $name => $info) { if (empty($edit_tags[$name])) { continue; } if (!empty($info['tag:context:allowed'])) { $bool = ( in_array('Meta Tags by Pathis now included in Nodewords; there is not need to use
Meta Tags by Path, and the module should be disabled to avoid possible conflicts. Disable the module in themodules page.'), array('@url' => url('admin/build/modules') ) ); } } } } /** * Helper function for uksort(). * Sort the form fields basing on their titles. */ function _nodewords_cmp_form_fields($a, $b, $init = FALSE) { static $form; if ($init) { $form = $a; } else { return strnatcmp($form[$a]['#title'], $form[$b]['#title']); } } /** * Try to guess the $type and $ids by looking at $_GET['q']. */ function _nodewords_detect_type_and_ids() { $arg = arg(); $bool = ( !variable_get('nodewords_list_repeat', FALSE) && isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ); if ($bool) { return array(NODEWORDS_MT_TYPE_PAGER, array(0)); } if (variable_get('site_offline', 0) && !user_access('administer site configuration')) { return array('none', array()); } if (drupal_is_front_page() && variable_get('nodewords_use_frontpage_tags', TRUE)) { return array(NODEWORDS_MT_TYPE_FRONTPAGE, array(0)); } $headers = drupal_get_headers(); if (preg_match('@HTTP/1\.[01]\x20+403@', $headers)) { return array(NODEWORDS_MT_TYPE_ERRORPAGE, array(403)); } if (preg_match('@HTTP/1\.[01]\x20+404@', $headers)) { return array(NODEWORDS_MT_TYPE_ERRORPAGE, array(404)); } if (!isset($arg[0])) { return array('none', array()); } _nodewords_load_all_includes(); foreach (module_implements('nodewords_type_id') as $module) { $result = module_invoke($module, 'nodewords_type_id', $arg); if (isset($result) && is_array($result) && count($result) > 2) { return $result; } } foreach (_nodewords_get_pages_data() as $page) { if (preg_match($page->regexp, $_GET['q'])) { return array('page', array($page->path)); } } return array('none', array()); } /** * Load the page meta tags data from the cache. * * @param $id * The ID of the page to load; by default the function loads all the custom * pages data. */ function _nodewords_get_pages_data($id = NULL) { static $pages; if (!isset($pages)) { $pages = array(); $result = db_query("SELECT * FROM {nodewords_custom} ORDER BY weight ASC"); while ($page = db_fetch_object($result)) { $page->regexp = '/^('. preg_replace('/\\\\\*/', '.*', preg_quote($page->path, '/')) .')$/'; $page->tags = nodewords_load_tags('page', $page->path); $pages[$page->pid] = $page; } } return isset($id) ? (isset($pages[$id]) ? $pages[$id] : FALSE) : $pages; } function _nodewords_teaser_match_callback($matches, $init = FALSE) { static $bool; if ($init) { $bool = $matches; } if ($bool && !empty($matches[1])) { return ' ' . $matches[1] . ' '; } return ''; } /***************************************************************************** * Private functions. ****************************************************************************/ function _nodewords_load_all_includes() { $dir = drupal_get_path('module', 'nodewords') . '/includes'; foreach (file_scan_directory($dir, '.*\.inc', array('.', '..', 'CVS'), 0, FALSE) as $filename => $info) { if (module_exists($info->name)) { include_once './' . $filename; } } } /** * @} End of "addtogroup nodewords". */