metatag.migrate.inc 2.9 KB

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