metatag.migrate.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @file
  4. * Metatag support for Migrate.
  5. */
  6. if (!class_exists('MigrateDestinationHandler')) {
  7. return;
  8. }
  9. /**
  10. * Basic usage of the Migrate integration.
  11. *
  12. * This example assumes the custom module's name is "example_migrate".
  13. *
  14. * example_migrate.inc:
  15. *
  16. * class MetatagTestMigration extends DynamicMigration {
  17. *
  18. * public function __construct() {
  19. * parent::__construct();
  20. *
  21. * $this->description = t('Migrate test.');
  22. *
  23. * $this->map = new MigrateSQLMap(
  24. * $this->machineName,
  25. * array(
  26. * 'id' => array(
  27. * 'type' => 'varchar',
  28. * 'not null' => TRUE,
  29. * 'length' => 254,
  30. * 'description' => 'ID of record.',
  31. * ),
  32. * ),
  33. * MigrateDestinationNode::getKeySchema()
  34. * );
  35. *
  36. * $this->source = new MigrateSourceCSV(
  37. * drupal_get_path('module', 'example_migrate') . '/sample.csv',
  38. * array(),
  39. * array('header_rows' => TRUE)
  40. * );
  41. *
  42. * $this->destination = new MigrateDestinationNode('article');
  43. *
  44. * $this->addFieldMapping('metatag_description', 'description');
  45. * $this->addFieldMapping('metatag_keywords', 'keywords');
  46. * }
  47. * }
  48. *
  49. * example_migrate.migrate.inc:
  50. *
  51. * /**
  52. * * Implements hook_migrate_api().
  53. * * /
  54. * function example_migrate_migrate_api() {
  55. * $api = array(
  56. * 'api' => 2,
  57. * 'migrations' => array(
  58. * 'MetatagTest' => array('class_name' => 'MetatagTestMigration'),
  59. * ),
  60. * );
  61. *
  62. * return $api;
  63. * }
  64. */
  65. /**
  66. * Implements hook_migrate_api().
  67. */
  68. function metatag_migrate_api() {
  69. $api = array(
  70. 'api' => 2,
  71. 'destination handlers' => array(
  72. 'MigrateMetatagHandler',
  73. ),
  74. );
  75. return $api;
  76. }
  77. /**
  78. * Metatag destination handler.
  79. */
  80. class MigrateMetatagHandler extends MigrateDestinationHandler {
  81. /**
  82. * Identify a list of supported entity types.
  83. */
  84. public function __construct() {
  85. $entity_types = metatag_entity_supports_metatags();
  86. $entity_types = array_filter($entity_types);
  87. $entity_types = array_keys($entity_types);
  88. $this->registerTypes($entity_types);
  89. }
  90. /**
  91. * Implements MigrateDestinationHandler::fields().
  92. */
  93. public function fields() {
  94. $fields = array();
  95. $elements = metatag_get_info();
  96. foreach ($elements['tags'] as $value) {
  97. $metatag_field = 'metatag_' . $value['name'];
  98. $fields[$metatag_field] = $value['description'];
  99. }
  100. return $fields;
  101. }
  102. /**
  103. * Implements MigrateDestinationHandler::prepare().
  104. */
  105. public function prepare($entity, stdClass $row) {
  106. $elements = metatag_get_info();
  107. foreach ($elements['tags'] as $value) {
  108. $metatag_field = 'metatag_' . $value['name'];
  109. if (isset($entity->$metatag_field)) {
  110. $language = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
  111. $entity->metatags[$language][$value['name']]['value'] = $entity->$metatag_field;
  112. unset($entity->$metatag_field);
  113. }
  114. }
  115. }
  116. }