diff --git a/metatag_importer/metatag_importer.drush.inc b/metatag_importer/metatag_importer.drush.inc index af86d77..db8a5fc 100644 --- a/metatag_importer/metatag_importer.drush.inc +++ b/metatag_importer/metatag_importer.drush.inc @@ -19,6 +19,11 @@ function metatag_importer_drush_command() { 'drupal dependencies' => array('metatag'), ); + $items['metatag-convert-metatags-quick'] = array( + 'description' => dt('Convert data from Metatags Quick into Metatag'), + 'aliases' => array('mtcmq'), + ); + return $items; } @@ -72,3 +77,15 @@ function drush_metatag_importer_metatag_convert_page_title() { drush_print(dt('Converted !count record(s) from the Page Title module.', array('!count' => $count))); } + +/** + * Callback to convert all Metatags Quick data. + */ +function drush_metatag_importer_metatag_convert_metatags_quick() { + if (!drush_confirm(dt('Ready to convert all data from Metatags Quick?'))) { + return; + } + + include('metatag_importer.metatags_quick.inc'); + metatag_importer_metatags_quick_import(); +} diff --git a/metatag_importer/metatag_importer.metatags_quick.inc b/metatag_importer/metatag_importer.metatags_quick.inc new file mode 100644 index 0000000..f82077f --- /dev/null +++ b/metatag_importer/metatag_importer.metatags_quick.inc @@ -0,0 +1,191 @@ + 'title', + 'keywords' => 'keywords', + 'abstract' => 'abstract', + 'description' => 'description', + 'canonical' => 'canonical', + 'og:title' => 'title', + 'og:description' => 'description', + 'twitter:title' => 'title', + 'twitter:description' => 'description', + ); + + $entity_type = $quick['entity_type']; + $entity_id = $quick['entity_id']; + $revision_id = $quick['revision_id']; + $langcode = $quick['language']; + + // Fallback to entity language if no field language is set. + if (LANGUAGE_NONE == $langcode) { + $entities = entity_load($entity_type, array($entity_id)); + if (!empty($entities[$entity_id])) { + $langcode = entity_language($entity_type, $entities[$entity_id]); + } + } + + // Check for an existing record. + $data = metatag_metatags_load($entity_type, $entity_id); + + // Drop back one level because the results will be keyed by revision_id. + if (!empty($data)) { + $data = reset($data); + } + + // Map the Quick meta tags. + foreach ($tag_map as $dest => $source) { + if (!empty($quick['fields'][$source]['value'])) { + $data[$langcode][$dest] = array('value' => $quick['fields'][$source]['value']); + // Add the default suffix to the page title. + if ($dest == 'title') { + $data[$langcode][$dest]['value'] .= ' | [site:name]'; + } + } + } + + // Create or update the {metatag} record. + if (!empty($data)) { + metatag_metatags_save($entity_type, $entity_id, $revision_id, $data); + } + + metatag_importer_delete_quick_data($quick); + + // Reset the entity cache. If entity cache module is used, this also resets + // its permanent cache. + entity_get_controller($entity_type)->resetCache(array($entity_id)); + +} + +/** + * Get all fields from Metatags Quick. + * + * @return array + * Array of field names, keyed by name. + */ +function metatag_importer_get_quick_fields() { + // Get a list of all entities that use a Metatags Quick field. + $fields = array(); + foreach (field_info_instances() as $entity_type => $bundles) { + // Skip the custon entity type provided by Metatags Quick. + if ($entity_type == 'metatags_path_based') { + continue; + } + foreach ($bundles as $bundle_name => $bundle) { + foreach ($bundle as $field_name => $field) { + if ($field['widget']['module'] == 'metatags_quick') { + $fields[$field_name] = $field_name; + } + } + } + } + return $fields; +} + +/** + * Get metatags_quick data from the database. + * + * @param array $fields + * Array of field names. + * + * @return array + * Array + */ +function metatag_importer_get_quick_data($fields) { + + $data = array(); + foreach ($fields as $field_name) { + $meta_tag = str_replace('meta_', '', str_replace('field_', '', $field_name)); + $results = db_select('field_data_' . $field_name, 'f') + ->fields('f', array( + 'entity_type', + 'bundle', + 'entity_id', + 'revision_id', + 'language', + $field_name . '_metatags_quick' + )) + ->condition('f.entity_type', array('metatags_path_based'), '<>') + ->orderBy('f.entity_type', 'ASC') + ->orderBy('f.entity_id', 'ASC') + ->orderBy('f.revision_id', 'ASC') + ->execute(); + + foreach ($results as $result) { + $id = implode(':', array( + $result->entity_type, + $result->entity_id, + $result->revision_id, + $result->language + )); + + if (!isset($data[$id])) { + $data[$id] = array( + 'entity_type' => $result->entity_type, + 'bundle' => $result->bundle, + 'entity_id' => $result->entity_id, + 'revision_id' => $result->revision_id, + 'language' => $result->language, + 'fields' => array(), + ); + } + $data[$id]['fields'][$meta_tag] = array( + 'field_name' => $field_name, + 'value' => $result->{$field_name . '_metatags_quick'}, + 'meta_tag' => $meta_tag, + ); + } + + } + return $data; +} + +/** + * Delete the old metatags_quick records. + * + * @param array $quick + */ +function metatag_importer_delete_quick_data($quick) { + foreach ($quick['fields'] as $field) { + db_delete('field_data_' . $field['field_name']) + ->condition('entity_type', $quick['entity_type']) + ->condition('entity_id', $quick['entity_id']) + ->condition('language', $quick['language']) + ->execute(); + + db_delete('field_revision_' . $field['field_name']) + ->condition('entity_type', $quick['entity_type']) + ->condition('entity_id', $quick['entity_id']) + ->condition('language', $quick['language']) + ->execute(); + } +}