Skip to content
Snippets Groups Projects
Commit fdf5eeb1 authored by Damien McKenna's avatar Damien McKenna
Browse files

Issue #1282806 by DamienMcKenna, iamEAP, osopolar, victoriachan, malcomio,...

Issue #1282806 by DamienMcKenna, iamEAP, osopolar, victoriachan, malcomio, HyperGlide, Murz, dandaman, klokie: Upgrade script for Meta Tags Quick.
parent 304aecc6
No related branches found
No related tags found
1 merge request!56Issue #2143979 by bpleduc, drunken monkey, DamienMcKenna, thirdender,...
......@@ -22,6 +22,8 @@ Metatag 7.x-1.x-dev, xxxx-xx-xx
fatal.
#2872446 by jenlampton, DamienMcKenna: Complete the drush command for importing
nodewords meta tags.
#1282806 by DamienMcKenna, iamEAP, osopolar, victoriachan, malcomio, HyperGlide,
Murz, dandaman, klokie: Upgrade script for Meta Tags Quick.
Metatag 7.x-1.22, 2017-07-03
......
......@@ -3,23 +3,32 @@ Metatag: Importer
This module imports data from other modules. An administrative interface is
provided (admin/config/search/metatags/importer), as well as a series of Drush
commands:
* metatag-convert-page-title - Convert data from the Page Title module.
* metatag-convert-metatags-quick / mtcmq
Convert data from the Metatags Quick module.
* metatag-convert-nodewords / mtcnw
Convert data from the Nodewords module.
* metatag-convert-page-title / mtcpt
Convert data from the Page Title module.
Known Issues
--------------------------------------------------------------------------------
- The admin page (currently) only supports migrating data from Nodewords.
- The Drush commands (currently) only supports migrating data from Page Title.
- The admin page (currently) only supports migrating data from Nodewords and
Page Title.
- Only entities are currently supported, other configuration types will be
supported soon.
Credits / Contact
--------------------------------------------------------------------------------
Currently maintained by Damien McKenna [1]. Originally developed by jantoine [2]
with contributions by drupalninja99 [3], stuart.crouch [4], subhojit777 [5],
KarlShea [6], stefan.r [7], HyperGlide [8] and jenlampton [9].
Currently maintained by Damien McKenna [1]. Originally developed by Jon Antoine
[2] with contributions by Jay Callicott [3], Stuart Crouch [4], Subhojit Paul
[5], Karl Shea [6], Stefan Ruijsenaars [7], HyperGlide [8], Jen Lampton [9],
Eric Peterson [10], Lars Schröter [11], Victoria Chan [12], Malcolm Young [13],
Alexey Korepov [14], klokie [15] and Dan Ficker [16].
References
......@@ -33,3 +42,10 @@ References
7: https://www.drupal.org/u/stefan.r
8: https://www.drupal.org/u/hyperglide
9: https://www.drupal.org/u/jenlampton
10: https://www.drupal.org/u/iameap
11: https://www.drupal.org/u/osopolar
12: https://www.drupal.org/u/victoriachan
13: https://www.drupal.org/u/malcomio
14: https://www.drupal.org/u/murz
15: https://www.drupal.org/u/klokie
16: https://www.drupal.org/u/dandaman
<?php
/**
* @file
* Drush integration for the Metatag Importer module.
......@@ -8,19 +9,39 @@
* Implements hook_drush_command().
*/
function metatag_importer_drush_command() {
$items['metatag-convert-metatags-quick'] = array(
'description' => dt('Convert data from Metatags Quick into Metatag'),
'drupal dependencies' => array('metatag', 'metatag_importer'),
'aliases' => array('mtcmq'),
);
$items['metatag-convert-nodewords'] = array(
'description' => dt('Convert data from Nodewords into Metatag.'),
'drupal dependencies' => array('metatag'),
'drupal dependencies' => array('metatag', 'metatag_importer'),
'aliases' => array('mtcnw'),
);
$items['metatag-convert-page-title'] = array(
'description' => dt('Convert data from Page Title into Metatag.'),
'drupal dependencies' => array('metatag'),
'drupal dependencies' => array('metatag', 'metatag_importer'),
'aliases' => array('mtcpt'),
);
return $items;
}
/**
* 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();
}
/**
* Callback to convert all Nodewords data.
*/
......
<?php
/**
* @file
* Convert data from Metatags Quick to Metatag.
*/
/**
* Import all data from Metatags Quick and delete it.
*/
function metatag_importer_metatags_quick_import() {
// Get a list of Metatags Quick fields.
$fields = metatag_importer_get_quick_fields();
$message = 'Converting records from @count Metatags Quick field(s).';
$vars = array('@count' => count($data));
if (drupal_is_cli() && function_exists('drush_print')) {
drush_print(dt($message, $vars));
}
else {
drupal_set_message(t($message, $vars));
}
// Get all of the data for these fields.
$data = metatag_importer_get_quick_data($fields);
// Process the data.
foreach ($data as $quick) {
$message = 'Converting @count records from Metatags Quick..';
$vars = array('@count' => count($quick));
if (drupal_is_cli() && function_exists('drush_print')) {
drush_print(dt($message, $vars));
}
else {
drupal_set_message(t($message, $vars));
}
metatag_importer_metatags_quick_process($quick);
}
if (drupal_is_cli() && function_exists('drush_print')) {
drush_print(dt('All done!'));
}
else {
drupal_set_message(t('All done!'));
}
}
/**
* Process a row from Metatags Quick.
*
* Imports data into metatag structure, and deletes Metatags Quick data.
*
* @param array $quick
* Row of data from Metatags Quick.
*/
function metatag_importer_metatags_quick_process(array $quick) {
// Identify which Metatag meta tags will be filled by Metatags Quick values.
$tag_map = array(
'title' => 'title',
'keywords' => 'keywords',
'abstract' => 'abstract',
'description' => 'description',
'canonical' => 'canonical',
);
if (module_exists('metatag_opengraph')) {
$tag_map['og:title'] = 'title';
$tag_map['og:description'] = 'description';
}
if (module_exists('metatag_twitter_cards')) {
$tag_map['twitter:title'] = 'title';
$tag_map['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);
}
if (!empty($quick['fields'])) {
metatag_importer_delete_quick_data($quick['fields']);
}
// Reset the entity cache. If entitycache 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() {
$fields = array();
// Get a list of all entities that use a Metatags Quick field.
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
* Metadata
*/
function metatag_importer_get_quick_data(array $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
* Records to delete.
*/
function metatag_importer_delete_quick_data(array $quick) {
foreach ($quick 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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment