diff --git a/nodewords.admin.inc b/nodewords.admin.inc index ecd399a..86c108f 100644 --- a/nodewords.admin.inc +++ b/nodewords.admin.inc @@ -304,6 +304,16 @@ function nodewords_settings_form() { '#description' => t('Some search engines punish sites that use the same meta tags on different pages. Uncheck this option if you want to suppress the repetition of the same meta tags on pages that use the pager - if unchecked, Drupal will only display the meta tags for the first page and not for subsequent pages. If unsure, select this option.'), '#default_value' => variable_get('nodewords_list_repeat', FALSE), ); + + $form['metatags_creation']['nodewords_enable_tokens'] = array( + '#type' => 'checkbox', + '#title' => t('Enable tokens'), + '#description' => t('This setting allows to use the tokens implemented in token.module in meta tags content.', array( + '@token' => 'http://drupal.org/project/token', + )), + '#default_value' => variable_get('nodewords_enable_tokens', TRUE) && module_exists('token'), + '#disabled' => !module_exists('token'), + ); $form['metatags_creation']['nodewords_use_frontpage_tags'] = array( '#type' => 'checkbox', @@ -361,6 +371,7 @@ function nodewords_settings_form() { return $form; } + /** * Function to validate the base URL. */ diff --git a/nodewords.module b/nodewords.module index 6f055ed..1360cdc 100644 --- a/nodewords.module +++ b/nodewords.module @@ -746,7 +746,7 @@ function nodewords_delete_tags($type, $id) { } } -/** +/** * Return the form used to set the meta tags values. * * @param $type @@ -770,6 +770,9 @@ function nodewords_form($type, $tags, $options = array()) { ); $edit_tags = variable_get('nodewords_edit', array()); $form = array(); + $tokens_support = FALSE; + $tokens_type = array(); + $tokens_enabled = module_exists('token') && variable_get('nodewords_enable_tokens', TRUE); $options += $default_options; if (isset($options['tag_options']) && is_array($options['tag_options'])) { @@ -806,6 +809,11 @@ function nodewords_form($type, $tags, $options = array()) { if ($bool) { continue; } + + if (!empty($info['tokens'])) { + $description[] = ' ' . t('This meta tag supports tokens.') . ''; + $tokens_support = TRUE; + } $bool = ( ( @@ -834,6 +842,10 @@ function nodewords_form($type, $tags, $options = array()) { $form['#weight'] = $options['fieldset:weight']; $form['#group'] = 'additional_settings'; } + + if ($tokens_enabled && $tokens_support) { + nodewords_add_tokens_help($form, $type); + } return $form; } @@ -1252,6 +1264,118 @@ function nodewords_migrate_api() { } +function nodewords_replace_tokens($content, $options = array()) { + if (empty($content) || !variable_get('nodewords_enable_tokens', TRUE) || !module_exists('token')) { + return $content; + } + + // Always include the global context. + $token_objects['global'] = NULL; + + $options += _nodewords_detect_type_and_id(); + + // Allow other modules to alter the context used for tokens below if the + // current context is not yet specific. + switch ($options['type']) { + case NODEWORDS_TYPE_DEFAULT: + case NODEWORDS_TYPE_FRONTPAGE: + case NODEWORDS_TYPE_PAGE: + case NODEWORDS_TYPE_PAGER: + $original_type = $options['type']; + $arg = arg() + array_fill(0, MENU_MAX_PARTS, NULL); + foreach (module_implements('nodewords_type_id') as $module) { + $function = $module . '_nodewords_type_id'; + $function($options, $arg); + if ($options['type'] != $original_type) { + break; + } + } + break; + } + + switch ($options['type']) { + case NODEWORDS_TYPE_NODE: + $token_objects['node'] = node_load($options['id']); + break; + + case NODEWORDS_TYPE_TERM: + $token_objects['taxonomy'] = taxonomy_get_term($options['id']); + break; + + case NODEWORDS_TYPE_VOCABULARY: + $token_objects['vocabulary'] = taxonomy_vocabulary_load($options['id']); + break; + + case NODEWORDS_TYPE_USER: + $token_objects['user'] = user_load($options['id']); + break; + } + + // Perform token replacement, making sure all tokens are replaced even if + // there is no replacement value for a token. + $content = token_replace_multiple($content, $token_objects, TOKEN_PREFIX, TOKEN_SUFFIX, array('clear' => TRUE)); + + return $content; +} + + +/** + * Add the tokens help to the form. + * + * Add the tokens help to the form passed as argument. + * It is responsability of the calling function to verify that token.module is + * enabled. + * + * @param &$form + * The form to which the help text will be added. + * @param $type + * An array containing information about the object for which the meta tags + * are being edited. + */ +function nodewords_add_tokens_help(&$form, $type) { + // Always include the global context. + $token_types[] = 'global'; + + switch ($type['type']) { + case NODEWORDS_TYPE_DEFAULT: + case NODEWORDS_TYPE_PAGE: + $token_types[] = 'node'; + $token_types[] = 'taxonomy'; + $token_types[] = 'vocabulary'; + $token_types[] = 'user'; + break; + + case NODEWORDS_TYPE_NODE: + $token_types[] = 'node'; + break; + + case NODEWORDS_TYPE_TERM: + $token_types[] = 'taxonomy'; + break; + + case NODEWORDS_TYPE_VOCABULARY: + $token_types[] = 'vocabulary'; + break; + + case NODEWORDS_TYPE_USER: + $token_types[] = 'user'; + break; + } + + $form['token_help'] = array( + '#title' => t('Replacement patterns'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#description' => t('Use the raw versions of tokens to avoid problems with HTML entities.'), + ); + + $form['token_help']['help'] = array( + '#value' => theme('token_tree', $token_types), + ); +} + + /** * Internal functions */ diff --git a/nodewords_basic/nodewords_basic.module b/nodewords_basic/nodewords_basic.module index 9e979d0..f3fa3cd 100644 --- a/nodewords_basic/nodewords_basic.module +++ b/nodewords_basic/nodewords_basic.module @@ -46,7 +46,7 @@ function nodewords_basic_nodewords_tags_info() { ), 'label' => t('Page title'), 'permission' => 'edit meta tag PAGE TITLE', - 'weight' => array('page_title' => -199), + 'weight' => array('page_title' => -199), ), 'description' => array( 'callback' => 'nodewords_basic_description', @@ -60,6 +60,7 @@ function nodewords_basic_nodewords_tags_info() { 'description' => '

%content

' ), ), + 'tokens' => TRUE, 'weight' => array('description' => -189), ), 'abstract' => array( @@ -71,6 +72,7 @@ function nodewords_basic_nodewords_tags_info() { 'abstract' => NODEWORDS_META, ), ), + 'tokens' => TRUE, 'weight' => array('abstract' => -186), ), 'keywords' => array( @@ -85,6 +87,7 @@ function nodewords_basic_nodewords_tags_info() { 'keywords' => '

%content

' ), ), + 'tokens' => TRUE, 'weight' => array('keywords' => -185), ), 'copyright' => array( @@ -99,6 +102,7 @@ function nodewords_basic_nodewords_tags_info() { 'copyright' => '

%content

' ), ), + 'tokens' => TRUE, 'weight' => array('copyright' => -169), ), 'canonical' => array( @@ -118,7 +122,7 @@ function nodewords_basic_nodewords_tags_info() { 'canonical' => NODEWORDS_LINK_REL, ), ), - 'weight' => array('canonical' => -139), + 'weight' => array('canonical' => -139), ), 'revisit-after' => array( 'callback' => 'nodewords_basic_revisit_after', @@ -322,7 +326,12 @@ function nodewords_basic_abstract_form(&$form, $content, $options) { * Set the meta tag content. */ function nodewords_basic_abstract_prepare(&$tags, $content, $options) { - $tags['abstract'] = empty($content['value']) ? (!empty($options['default']['abstract']['value']) ? $options['default']['abstract']['value'] : '') : $content['value']; + if (empty($content['value'])) { + $tags['abstract'] = nodewords_replace_tokens((!empty($options['default']['abstract']['value']) ? $options['default']['abstract']['value'] : ''), $options); + } + else { + $tags['abstract'] = nodewords_replace_tokens($content['value'], $options); + } } /** @@ -419,7 +428,12 @@ function nodewords_basic_copyright_form(&$form, $content, $options) { * Set the meta tag content. */ function nodewords_basic_copyright_prepare(&$tags, $content, $options) { - $tags['copyright'] = empty($content['value']) ? (!empty($options['default']['copyright']['value']) ? $options['default']['copyright']['value'] : '') : $content['value']; + if (empty($content['value'])) { + $tags['copyright'] = nodewords_replace_tokens((!empty($options['default']['copyright']['value']) ? $options['default']['copyright']['value'] : ''), $options); + } + else { + $tags['copyright'] = nodewords_replace_tokens($content['value'], $options); + } } /** @@ -475,7 +489,12 @@ function nodewords_basic_description_form(&$form, $content, $options) { * Set the meta tag content. */ function nodewords_basic_description_prepare(&$tags, $content, $options) { - _nodewords_prepare_description($tags, $content, $options, 'description'); + if (empty($content['value'])) { + $tags['description'] = nodewords_replace_tokens((!empty($options['default']['description']['value']) ? $options['default']['description']['value'] : ''), $options); + } + else { + $tags['description'] = nodewords_replace_tokens($content['value'], $options); + } } /** @@ -542,7 +561,9 @@ function nodewords_basic_keywords_prepare(&$tags, $content, $options) { } } - $tags['keywords'] = nodewords_unique_values($value); + $tags['keywords'] = nodewords_unique_values( + nodewords_replace_tokens($value . ',' . variable_get('nodewords_last_keywords', ''), $options) + ); } function nodewords_basic_keywords_settings_form(&$form, $form_id, $options) {