From 4543a45967d30b70a6b708e8063cbebde8b3a49b Mon Sep 17 00:00:00 2001 From: Colan Schwartz Date: Tue, 17 Jul 2012 15:53:29 -0400 Subject: [PATCH] Issue #1688286 by colan: Add language-specific tags. --- metatag.install | 43 ++++++++++++++++++++++++++++++++++++- metatag.module | 63 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/metatag.install b/metatag.install index 47518f9..8128b5f 100644 --- a/metatag.install +++ b/metatag.install @@ -78,8 +78,15 @@ function metatag_schema() { 'not null' => TRUE, 'serialize' => TRUE, ), + 'language' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The language of the tag', + ), ), - 'primary key' => array('entity_type', 'entity_id'), + 'primary key' => array('entity_type', 'entity_id', 'language'), ); $schema['cache_metatag'] = drupal_get_schema_unprocessed('system', 'cache'); @@ -110,3 +117,37 @@ function metatag_update_7001() { ); db_change_field('metatag_config', 'cid', 'cid', $field); } + +/** + * Add the {metatag}.language field. + */ +function metatag_update_7002() { + + // Set the target table and field name. + $table_name = 'metatag'; + $field_name = 'language'; + + // Don't add the new field if it already exists. + if (!db_field_exists($table_name, $field_name)) { + + // Describe the new field. + $field_definition = array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The language of the tag', + ); + + // Add it and update the primary key. + db_add_field($table_name, $field_name, $field_definition); + db_drop_primary_key($table_name); + db_add_primary_key($table_name, array('entity_type', 'entity_id', 'language')); + + // Set default values. + db_update($table_name) + ->fields(array($field_name => language_default('language'))) + ->execute(); + } +} + diff --git a/metatag.module b/metatag.module index 9899f5e..d6f909a 100644 --- a/metatag.module +++ b/metatag.module @@ -271,12 +271,44 @@ function metatag_config_cache_clear() { ctools_export_load_object_reset('metatag_config'); } -function metatag_metatags_load($type, $id) { - $metatags = metatag_metatags_load_multiple($type, array($id)); +/** + * Load an entity's tags. + * + * @param $type + * The entity type to load + * @param $id + * The ID of the entity to load + * @param $language + * The language of the data. + * @return + * A list of meta tags for the entity ID. + */ +function metatag_metatags_load($type, $id, $language = NULL) { + + // If unset, set the default language. + $language = $language ? $language : language_default('language'); + + $metatags = metatag_metatags_load_multiple($type, array($id), $language); return !empty($metatags) ? reset($metatags) : array(); } -function metatag_metatags_load_multiple($type, array $ids) { +/** + * Load tags for multiple entities. + * + * @param $type + * The entity type to load + * @param $ids + * The list of entity IDs + * @param $language + * The language of the data. + * @return + * An array of tag data, keyed by ID. + */ +function metatag_metatags_load_multiple($type, array $ids, $language = NULL) { + + // If unset, set the default language. + $language = $language ? $language : language_default('language'); + // Double check entity IDs are numeric thanks to Entity API module. $ids = array_filter($ids, 'is_numeric'); if (empty($ids)) { @@ -289,15 +321,32 @@ function metatag_metatags_load_multiple($type, array $ids) { return array(); } - $metatags = db_query("SELECT entity_id, data FROM {metatag} WHERE entity_type = :type AND entity_id IN (:ids)", array( + $metatags = db_query("SELECT entity_id, data FROM {metatag} WHERE (entity_type = :type) AND (entity_id IN (:ids)) AND (language = :language)", array( ':type' => $type, ':ids' => $ids, + ':language' => $language, ))->fetchAllKeyed(); $metatags = array_map('unserialize', $metatags); return $metatags; } -function metatag_metatags_save($type, $id, $metatags) { +/** + * Save an entity's tags. + * + * @param $type + * The entity type to load + * @param $id + * The entity's ID + * @param $metatags + * All of the tag information + * @param $language + * The language in which to save the data + */ +function metatag_metatags_save($type, $id, $metatags, $language = NULL) { + + // If unset, set the default language. + $language = $language ? $language : language_default('language'); + // Check that $id is numeric because of Entity API and string IDs. if (!is_numeric($id)) { return; @@ -306,7 +355,7 @@ function metatag_metatags_save($type, $id, $metatags) { // Allow other modules to alter the metatags prior to saving. foreach (module_implements('metatag_presave') as $module) { $function = "{$module}_metatag_presave"; - $function($metatags, $type, $id); + $function($metatags, $type, $id, $language); } if (empty($metatags)) { @@ -315,6 +364,7 @@ function metatag_metatags_save($type, $id, $metatags) { db_delete('metatag') ->condition('entity_type', $type) ->condition('entity_id', $id) + ->condition('language', $language) ->execute(); } else { @@ -323,6 +373,7 @@ function metatag_metatags_save($type, $id, $metatags) { ->key(array( 'entity_type' => $type, 'entity_id' => $id, + 'language' => $language, )) ->fields(array( 'data' => serialize($metatags), -- 1.7.0.4