textauthor.migratefieldhandler.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. class MigrateTextAuthorFieldHandler extends MigrateFieldHandler {
  3. public function __construct() {
  4. $this->registerTypes(array('textauthor'));
  5. }
  6. static function arguments($author = NULL, $format = NULL, $language = NULL) {
  7. $arguments = array();
  8. if (!is_null($author)) {
  9. $arguments['author'] = $author;
  10. }
  11. if (!is_null($format)) {
  12. $arguments['format'] = $format;
  13. }
  14. if (!is_null($language)) {
  15. $arguments['language'] = $language;
  16. }
  17. return $arguments;
  18. }
  19. public function fields($type) {
  20. $fields = array();
  21. $fields = array(
  22. 'author' => t('Subfield: Text author'),
  23. 'format' => t('Subfield: Text format for the field'),
  24. 'language' => t('Subfield: Language for the field'),
  25. );
  26. return $fields;
  27. }
  28. public function prepare($entity, array $field_info, array $instance, array $values) {
  29. if (isset($values['arguments'])) {
  30. $arguments = $values['arguments'];
  31. unset($values['arguments']);
  32. }
  33. else {
  34. $arguments = array();
  35. }
  36. $migration = Migration::currentMigration();
  37. $destination = $migration->getDestination();
  38. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  39. $max_length = isset($field_info['settings']['max_length']) ?
  40. $field_info['settings']['max_length'] : 0;
  41. // Setup the standard Field API array for saving.
  42. $delta = 0;
  43. foreach ($values as $value) {
  44. $item = array();
  45. if (isset($arguments['author'])) {
  46. if (is_array($arguments['author'])) {
  47. $item['author'] = $arguments['author'][$delta];
  48. }
  49. else {
  50. $item['author'] = $arguments['author'];
  51. }
  52. }
  53. if (isset($arguments['format'])) {
  54. if (is_array($arguments['format'])) {
  55. $format = $arguments['format'][$delta];
  56. }
  57. else {
  58. $format = $arguments['format'];
  59. }
  60. }
  61. else {
  62. $format = $destination->getTextFormat();
  63. }
  64. $item['format'] = $item['value_format'] = $format;
  65. // Make sure the value will fit
  66. if ($max_length) {
  67. $item['value'] = drupal_substr($value, 0, $max_length);
  68. if (!empty($arguments['track_overflow'])) {
  69. $value_length = drupal_strlen($value);
  70. if ($value_length > $max_length) {
  71. $migration->saveMessage(
  72. t('Value for field !field exceeds max length of !max_length, actual length is !length',
  73. array('!field' => $instance['field_name'], '!max_length' => $max_length,
  74. '!length' => $value_length)),
  75. Migration::MESSAGE_INFORMATIONAL);
  76. }
  77. }
  78. }
  79. else {
  80. $item['value'] = $value;
  81. }
  82. if (is_array($language)) {
  83. $current_language = $language[$delta];
  84. }
  85. else {
  86. $current_language = $language;
  87. }
  88. $return[$current_language][] = $item;
  89. $delta++;
  90. }
  91. return isset($return) ? $return : NULL;
  92. }
  93. }