metatag_importer.metatags_quick.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * @file
  4. * Convert data from Metatags Quick to Metatag.
  5. */
  6. /**
  7. * Import all data from Metatags Quick and delete it.
  8. */
  9. function metatag_importer_metatags_quick_import() {
  10. // Get a list of Metatags Quick fields.
  11. $fields = metatag_importer_get_quick_fields();
  12. $message = 'Converting records from @count Metatags Quick field(s).';
  13. $vars = array('@count' => count($data));
  14. if (drupal_is_cli() && function_exists('drush_print')) {
  15. drush_print(dt($message, $vars));
  16. }
  17. else {
  18. drupal_set_message(t($message, $vars));
  19. }
  20. // Get all of the data for these fields.
  21. $data = metatag_importer_get_quick_data($fields);
  22. // Process the data.
  23. foreach ($data as $quick) {
  24. $message = 'Converting @count records from Metatags Quick..';
  25. $vars = array('@count' => count($quick));
  26. if (drupal_is_cli() && function_exists('drush_print')) {
  27. drush_print(dt($message, $vars));
  28. }
  29. else {
  30. drupal_set_message(t($message, $vars));
  31. }
  32. metatag_importer_metatags_quick_process($quick);
  33. }
  34. if (drupal_is_cli() && function_exists('drush_print')) {
  35. drush_print(dt('All done!'));
  36. }
  37. else {
  38. drupal_set_message(t('All done!'));
  39. }
  40. }
  41. /**
  42. * Process a row from Metatags Quick.
  43. *
  44. * Imports data into metatag structure, and deletes Metatags Quick data.
  45. *
  46. * @param array $quick
  47. * Row of data from Metatags Quick.
  48. */
  49. function metatag_importer_metatags_quick_process(array $quick) {
  50. // Identify which Metatag meta tags will be filled by Metatags Quick values.
  51. $tag_map = array(
  52. 'title' => 'title',
  53. 'keywords' => 'keywords',
  54. 'abstract' => 'abstract',
  55. 'description' => 'description',
  56. 'canonical' => 'canonical',
  57. );
  58. if (module_exists('metatag_opengraph')) {
  59. $tag_map['og:title'] = 'title';
  60. $tag_map['og:description'] = 'description';
  61. }
  62. if (module_exists('metatag_twitter_cards')) {
  63. $tag_map['twitter:title'] = 'title';
  64. $tag_map['twitter:description'] = 'description';
  65. }
  66. $entity_type = $quick['entity_type'];
  67. $entity_id = $quick['entity_id'];
  68. $revision_id = $quick['revision_id'];
  69. $langcode = $quick['language'];
  70. // Fallback to entity language if no field language is set.
  71. if (LANGUAGE_NONE == $langcode) {
  72. $entities = entity_load($entity_type, array($entity_id));
  73. if (!empty($entities[$entity_id])) {
  74. $langcode = entity_language($entity_type, $entities[$entity_id]);
  75. }
  76. }
  77. // Check for an existing record.
  78. $data = metatag_metatags_load($entity_type, $entity_id);
  79. // Drop back one level because the results will be keyed by revision_id.
  80. if (!empty($data)) {
  81. $data = reset($data);
  82. }
  83. // Map the Quick meta tags.
  84. foreach ($tag_map as $dest => $source) {
  85. if (!empty($quick['fields'][$source]['value'])) {
  86. $data[$langcode][$dest] = array('value' => $quick['fields'][$source]['value']);
  87. // Add the default suffix to the page title.
  88. if ($dest == 'title') {
  89. $data[$langcode][$dest]['value'] .= ' | [site:name]';
  90. }
  91. }
  92. }
  93. // Create or update the {metatag} record.
  94. if (!empty($data)) {
  95. metatag_metatags_save($entity_type, $entity_id, $revision_id, $data);
  96. }
  97. if (!empty($quick['fields'])) {
  98. metatag_importer_delete_quick_data($quick['fields']);
  99. }
  100. // Reset the entity cache. If entitycache module is used, this also resets
  101. // its permanent cache.
  102. entity_get_controller($entity_type)->resetCache(array($entity_id));
  103. }
  104. /**
  105. * Get all fields from Metatags Quick.
  106. *
  107. * @return array
  108. * Array of field names, keyed by name.
  109. */
  110. function metatag_importer_get_quick_fields() {
  111. $fields = array();
  112. // Get a list of all entities that use a Metatags Quick field.
  113. foreach (field_info_instances() as $entity_type => $bundles) {
  114. // Skip the custon entity type provided by Metatags Quick.
  115. if ($entity_type == 'metatags_path_based') {
  116. continue;
  117. }
  118. foreach ($bundles as $bundle_name => $bundle) {
  119. foreach ($bundle as $field_name => $field) {
  120. if ($field['widget']['module'] == 'metatags_quick') {
  121. $fields[$field_name] = $field_name;
  122. }
  123. }
  124. }
  125. }
  126. return $fields;
  127. }
  128. /**
  129. * Get metatags_quick data from the database.
  130. *
  131. * @param array $fields
  132. * Array of field names.
  133. *
  134. * @return array
  135. * Metadata
  136. */
  137. function metatag_importer_get_quick_data(array $fields) {
  138. $data = array();
  139. foreach ($fields as $field_name) {
  140. $meta_tag = str_replace('meta_', '', str_replace('field_', '', $field_name));
  141. $results = db_select('field_data_' . $field_name, 'f')
  142. ->fields('f', array(
  143. 'entity_type',
  144. 'bundle',
  145. 'entity_id',
  146. 'revision_id',
  147. 'language',
  148. $field_name . '_metatags_quick',
  149. ))
  150. ->condition('f.entity_type', array('metatags_path_based'), '<>')
  151. ->orderBy('f.entity_type', 'ASC')
  152. ->orderBy('f.entity_id', 'ASC')
  153. ->orderBy('f.revision_id', 'ASC')
  154. ->execute();
  155. foreach ($results as $result) {
  156. $id = implode(':', array(
  157. $result->entity_type,
  158. $result->entity_id,
  159. $result->revision_id,
  160. $result->language,
  161. ));
  162. if (!isset($data[$id])) {
  163. $data[$id] = array(
  164. 'entity_type' => $result->entity_type,
  165. 'bundle' => $result->bundle,
  166. 'entity_id' => $result->entity_id,
  167. 'revision_id' => $result->revision_id,
  168. 'language' => $result->language,
  169. 'fields' => array(),
  170. );
  171. }
  172. $data[$id]['fields'][$meta_tag] = array(
  173. 'field_name' => $field_name,
  174. 'value' => $result->{$field_name . '_metatags_quick'},
  175. 'meta_tag' => $meta_tag,
  176. );
  177. }
  178. }
  179. return $data;
  180. }
  181. /**
  182. * Delete the old metatags_quick records.
  183. *
  184. * @param array $quick
  185. * Records to delete.
  186. */
  187. function metatag_importer_delete_quick_data(array $quick) {
  188. foreach ($quick as $field) {
  189. db_delete('field_data_' . $field['field_name'])
  190. ->condition('entity_type', $quick['entity_type'])
  191. ->condition('entity_id', $quick['entity_id'])
  192. ->condition('language', $quick['language'])
  193. ->execute();
  194. db_delete('field_revision_' . $field['field_name'])
  195. ->condition('entity_type', $quick['entity_type'])
  196. ->condition('entity_id', $quick['entity_id'])
  197. ->condition('language', $quick['language'])
  198. ->execute();
  199. }
  200. }