text.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for text.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets().
  8. */
  9. function text_feeds_processor_targets($entity_type, $bundle_name) {
  10. $targets = array();
  11. $text_types = array(
  12. 'text',
  13. 'text_long',
  14. 'text_with_summary',
  15. );
  16. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  17. $info = field_info_field($name);
  18. if (in_array($info['type'], $text_types)) {
  19. $targets[$name] = array(
  20. 'name' => check_plain($instance['label']),
  21. 'callback' => 'text_feeds_set_target',
  22. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  23. );
  24. if ($info['type'] == 'text_with_summary') {
  25. // Allow mapping to summary.
  26. $targets[$name . ':summary'] = array(
  27. 'name' => t('@name: Summary', array('@name' => $instance['label'])),
  28. 'callback' => 'text_feeds_set_target',
  29. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  30. 'real_target' => $name,
  31. );
  32. }
  33. }
  34. if (!empty($instance['settings']['text_processing'])) {
  35. $targets[$name]['summary_callbacks'] = array('text_feeds_summary_callback');
  36. $targets[$name]['form_callbacks'] = array('text_feeds_form_callback');
  37. }
  38. }
  39. return $targets;
  40. }
  41. /**
  42. * Callback for mapping text fields.
  43. */
  44. function text_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  45. $language = $mapping['language'];
  46. list($field_name, $column) = explode(':', $target . ':value');
  47. if ($column === 'value' && isset($source->importer->processor->config['input_format'])) {
  48. $format = $source->importer->processor->config['input_format'];
  49. // Add in default values.
  50. $mapping += array(
  51. 'format' => $format,
  52. );
  53. }
  54. $field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
  55. // Iterate over all values.
  56. $delta = 0;
  57. foreach ($values as $value) {
  58. if (is_object($value) && $value instanceof FeedsElement) {
  59. $value = $value->getValue();
  60. }
  61. if (is_scalar($value) && strlen($value)) {
  62. $field[$language][$delta][$column] = (string) $value;
  63. if (isset($mapping['format'])) {
  64. $field[$language][$delta]['format'] = $mapping['format'];
  65. }
  66. }
  67. $delta++;
  68. }
  69. $entity->$field_name = $field;
  70. }
  71. /**
  72. * Summary callback for text field targets.
  73. *
  74. * Displays which text format will be used for the text field target.
  75. *
  76. * @see text_feeds_processor_targets()
  77. * @see text_feeds_form_callback()
  78. */
  79. function text_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
  80. global $user;
  81. $formats = filter_formats($user);
  82. // Processor-wide input format setting.
  83. $importer = feeds_importer($form['#importer']);
  84. $default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
  85. $mapping += array(
  86. 'format' => $default_format,
  87. );
  88. return t('Text format: %format', array('%format' => $formats[$mapping['format']]->name));
  89. }
  90. /**
  91. * Form callback for text field targets.
  92. *
  93. * Allows to select a text format for the text field target.
  94. *
  95. * @see text_feeds_processor_targets()
  96. * @see text_feeds_summary_callback()
  97. */
  98. function text_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
  99. global $user;
  100. $formats_options = array();
  101. $formats = filter_formats($user);
  102. foreach ($formats as $id => $format) {
  103. $formats_options[$id] = $format->name;
  104. }
  105. // Processor-wide text format setting.
  106. $importer = feeds_importer($form['#importer']);
  107. $default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
  108. $mapping += array(
  109. 'format' => $default_format,
  110. );
  111. return array(
  112. 'format' => array(
  113. '#type' => 'select',
  114. '#title' => t('Text format'),
  115. '#options' => $formats_options,
  116. '#default_value' => $mapping['format'],
  117. ),
  118. );
  119. }