diff --git a/feeds.module b/feeds.module old mode 100644 new mode 100755 index 4dc7c34..f36ec30 --- a/feeds.module +++ b/feeds.module @@ -875,6 +875,70 @@ function feeds_item_info_save($entity, $entity_id) { } /** + * Adds an entity translation if the entity_translation module is enabled. + */ +function feeds_mapper_add_translation(&$entity) { + + // Add translations if the entity_translation module exists and if + // this field is in a different language + if (module_exists('entity_translation')) { + // Determine the default language + $default_langcode = language_default('language'); + if ($langcode != $default_langcode) { + $handler = entity_translation_get_handler($entity->feeds_item->entity_type, $entity); + $translation = array( + 'translate' => 0, + 'status' => 1, + 'language' => $langcode, + 'source' => $default_langcode, + ); + $handler->setTranslation($translation, $entity); + } + } +} + +/** + * Returns the language summary text for use in a mapper target summary_callback + * + * @see mappers/text.inc + */ +function feeds_mapper_summary_language($mapping, $target, $form, $form_state) { + if (module_exists('locale')) { + $info = field_info_field($mapping['target']); + if ($info['translatable']) { + $language_options = array(LANGUAGE_NONE => t('All languages')) + locale_language_list('name'); + if (empty($mapping['language'])) { + return t('Language: @search', array('@search' => $language_options[LANGUAGE_NONE])); + } + return t('Language: @search', array('@search' => $language_options[$mapping['language']])); + } + } + return FALSE; +} + + +/** + * Adds a language field to a form. For use in a mapper target form_callback + * + * @see mappers/text.inc + */ +function feeds_mapper_form_language($mapping, $form = array()) { + if (module_exists('locale')) { + $info = field_info_field($mapping['target']); + if ($info['translatable']) { + $language_options = array(LANGUAGE_NONE => t('All languages')) + locale_language_list('name'); + $form['language'] = array( + '#type' => 'select', + '#title' => t('Language'), + '#options' => $language_options, + '#default_value' => !empty($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE, + ); + } + } + return $form; +} + +/** * @} */ diff --git a/mappers/file.inc b/mappers/file.inc old mode 100644 new mode 100755 index 646d055..1c342da --- a/mappers/file.inc +++ b/mappers/file.inc @@ -24,6 +24,8 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'name' => check_plain($instance['label']), 'callback' => 'file_feeds_set_target', 'description' => t('The @label field of the node.', array('@label' => $instance['label'])), + 'summary_callback' => 'file_feeds_summary_callback', + 'form_callback' => 'file_feeds_form_callback', ); } } @@ -60,6 +62,9 @@ function file_feeds_set_target($source, $entity, $target, $value) { return; } + // Set the language of the field depending on the mapping configuration. + $langcode = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE; + $entity_type = $source->importer->processor->entityType(); // Determine file destination. @@ -83,8 +88,8 @@ function file_feeds_set_target($source, $entity, $target, $value) { watchdog_exception('Feeds', $e, nl2br(check_plain($e))); } if ($file) { - $field['und'][$i] = (array)$file; - $field['und'][$i]['display'] = 1; // @todo: Figure out how to properly populate this field. + $field[$langcode][$i] = (array)$file; + $field[$langcode][$i]['display'] = 1; // @todo: Figure out how to properly populate this field. if ($info['cardinality'] == 1) { break; } @@ -93,3 +98,38 @@ function file_feeds_set_target($source, $entity, $target, $value) { } $entity->{$target} = $field; } + +/** + * Mapping configuration summary for text fields + * + * @param array $mapping + * Associative array of the mapping settings. + * @param array $target + * Array of target settings, as defined by the processor or + * hook_feeds_processor_targets_alter(). + * @param array $form + * The whole mapping form. + * @param array $form_state + * The form state of the mapping form. + * + * @return string + * Returns, as a string that may contain HTML, the summary to display while + * the full form isn't visible. + * If the return value is empty, no summary and no option to view the form + * will be displayed. + */ +function file_feeds_summary_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_summary_language($mapping, $target, $form, $form_state); +} + + +/** + * Settings form callback. + * + * @return array + * The per mapping configuration form. Once the form is saved, $mapping will + * be populated with the form values. + */ +function file_feeds_form_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_form_language($mapping); +} diff --git a/mappers/link.inc b/mappers/link.inc old mode 100644 new mode 100755 index 45a86f7..821aa34 --- a/mappers/link.inc +++ b/mappers/link.inc @@ -20,6 +20,8 @@ function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'callback' => 'link_feeds_set_target', 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), 'real_target' => $name, + 'summary_callback' => 'link_feeds_summary_callback', + 'form_callback' => 'link_feeds_form_callback', ); } if (array_key_exists('title', $info['columns'])) { @@ -28,6 +30,8 @@ function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'callback' => 'link_feeds_set_target', 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), 'real_target' => $name, + 'summary_callback' => 'link_feeds_summary_callback', + 'form_callback' => 'link_feeds_form_callback', ); } } @@ -41,7 +45,7 @@ function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam * user has decided to map to and $value contains the value of the feed item * element the user has picked as a source. */ -function link_feeds_set_target($source, $entity, $target, $value) { +function link_feeds_set_target($source, $entity, $target, $value, $mapping) { if (empty($value)) { return; } @@ -58,6 +62,9 @@ function link_feeds_set_target($source, $entity, $target, $value) { $field = isset($entity->$field_name) ? $entity->$field_name : array(); $delta = 0; + // Set the language of the field depending on the mapping configuration. + $langcode = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE; + foreach ($value as $v) { if ($info['cardinality'] == $delta) { break; @@ -68,9 +75,47 @@ function link_feeds_set_target($source, $entity, $target, $value) { } if (is_scalar($v)) { - $field['und'][$delta][$column] = $v; + $field[$langcode][$delta][$column] = $v; $delta++; } } + + feeds_mapper_add_translation($entity); + $entity->$field_name = $field; } + +/** + * Mapping configuration summary for text fields + * + * @param array $mapping + * Associative array of the mapping settings. + * @param array $target + * Array of target settings, as defined by the processor or + * hook_feeds_processor_targets_alter(). + * @param array $form + * The whole mapping form. + * @param array $form_state + * The form state of the mapping form. + * + * @return string + * Returns, as a string that may contain HTML, the summary to display while + * the full form isn't visible. + * If the return value is empty, no summary and no option to view the form + * will be displayed. + */ +function link_feeds_summary_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_summary_language($mapping, $target, $form, $form_state); +} + + +/** + * Settings form callback. + * + * @return array + * The per mapping configuration form. Once the form is saved, $mapping will + * be populated with the form values. + */ +function link_feeds_form_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_form_language($mapping); +} diff --git a/mappers/number.inc b/mappers/number.inc old mode 100644 new mode 100755 index b64c4ee..d8d090a --- a/mappers/number.inc +++ b/mappers/number.inc @@ -27,6 +27,8 @@ function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n 'name' => check_plain($instance['label']), 'callback' => 'number_feeds_set_target', 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), + 'summary_callback' => 'number_feeds_summary_callback', + 'form_callback' => 'number_feeds_form_callback', ); } } @@ -37,7 +39,7 @@ function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n * * Ensure that $value is a numeric to avoid database errors. */ -function number_feeds_set_target($source, $entity, $target, $value) { +function number_feeds_set_target($source, $entity, $target, $value, $mapping) { // Do not perform the regular empty() check here. 0 is a valid value. That's // really just a performance thing anyway. @@ -48,11 +50,14 @@ function number_feeds_set_target($source, $entity, $target, $value) { $info = field_info_field($target); + // Set the language of the field depending on the mapping configuration. + $langcode = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE; + // Iterate over all values. - $field = isset($entity->$target) ? $entity->$target : array('und' => array()); + $field = isset($entity->$target) ? $entity->$target : array($langcode => array()); // Allow for multiple mappings to the same target. - $delta = count($field['und']); + $delta = count($field[$langcode]); foreach ($value as $v) { @@ -65,10 +70,47 @@ function number_feeds_set_target($source, $entity, $target, $value) { } if (is_numeric($v)) { - $field['und'][$delta]['value'] = $v; + $field[$langcode][$delta]['value'] = $v; $delta++; } } + feeds_mapper_add_translation($entity); + $entity->$target = $field; } + +/** + * Mapping configuration summary for text fields + * + * @param array $mapping + * Associative array of the mapping settings. + * @param array $target + * Array of target settings, as defined by the processor or + * hook_feeds_processor_targets_alter(). + * @param array $form + * The whole mapping form. + * @param array $form_state + * The form state of the mapping form. + * + * @return string + * Returns, as a string that may contain HTML, the summary to display while + * the full form isn't visible. + * If the return value is empty, no summary and no option to view the form + * will be displayed. + */ +function number_feeds_summary_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_summary_language($mapping, $target, $form, $form_state); +} + + +/** + * Settings form callback. + * + * @return array + * The per mapping configuration form. Once the form is saved, $mapping will + * be populated with the form values. + */ +function number_feeds_form_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_form_language($mapping); +} diff --git a/mappers/taxonomy.inc b/mappers/taxonomy.inc old mode 100644 new mode 100755 index 02ac54e..e7e8ca2 --- a/mappers/taxonomy.inc +++ b/mappers/taxonomy.inc @@ -93,10 +93,14 @@ function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping = $mapping += array( 'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_NAME, 'autocreate' => FALSE, + 'language' => LANGUAGE_NONE, ); + // Grab the language for convenience + $langcode = $mapping['language']; $info = field_info_field($target); + $cache = &drupal_static(__FUNCTION__); if (!isset($cache['allowed_values'][$target])) { $cache['allowed_values'][$target] = taxonomy_allowed_values($info); @@ -116,10 +120,10 @@ function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping = ->range(0, 1); - $field = isset($entity->$target) ? $entity->$target : array('und' => array()); + $field = isset($entity->$target) ? $entity->$target : array($langcode => array()); // Allow for multiple mappings to the same target. - $delta = count($field['und']); + $delta = count($field[$langcode]); // Iterate over all values. foreach ($terms as $term) { @@ -171,11 +175,13 @@ function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping = } if ($tid && isset($cache['allowed_values'][$target][$tid])) { - $field['und'][$delta]['tid'] = $tid; + $field[$langcode][$delta]['tid'] = $tid; $delta++; } } + feeds_mapper_add_translation($entity); + $entity->$target = $field; } @@ -263,10 +269,17 @@ function taxonomy_feeds_term_lookup_term_by_guid($guid) { */ function taxonomy_feeds_summary_callback($mapping, $target, $form, $form_state) { $options = _taxonomy_feeds_form_callback_options(); + $output = ''; if (empty($mapping['term_search'])) { - return t('Search taxonomy terms by: @search', array('@search' => $options[FEEDS_TAXONOMY_SEARCH_TERM_NAME])); + $output = t('Search taxonomy terms by: @search', array('@search' => $options[FEEDS_TAXONOMY_SEARCH_TERM_NAME])); + } + else { + $output = t('Search taxonomy terms by: @search', array('@search' => $options[$mapping['term_search']])); } - return t('Search taxonomy terms by: @search', array('@search' => $options[$mapping['term_search']])); + if ($language_summary = feeds_mapper_summary_language($mapping, $target, $form, $form_state)) { + $output .= '
' . $language_summary; + } + return $output; } /** @@ -277,7 +290,8 @@ function taxonomy_feeds_summary_callback($mapping, $target, $form, $form_state) * be populated with the form values. */ function taxonomy_feeds_form_callback($mapping, $target, $form, $form_state) { - return array( + $return = feeds_mapper_form_language($mapping); + $return += array( 'term_search' => array( '#type' => 'select', '#title' => t('Search taxonomy terms by'), @@ -296,6 +310,7 @@ function taxonomy_feeds_form_callback($mapping, $target, $form, $form_state) { ), ), ); + return $return; } /** diff --git a/mappers/text.inc b/mappers/text.inc old mode 100644 new mode 100755 index 48447d7..7ec9463 --- a/mappers/text.inc +++ b/mappers/text.inc @@ -25,6 +25,8 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'name' => check_plain($instance['label']), 'callback' => 'text_feeds_set_target', 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), + 'summary_callback' => 'text_feeds_summary_callback', + 'form_callback' => 'text_feeds_form_callback', ); } } @@ -33,7 +35,7 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam /** * Callback for mapping text fields. */ -function text_feeds_set_target($source, $entity, $target, $value) { +function text_feeds_set_target($source, $entity, $target, $value, $mapping) { if (empty($value)) { return; } @@ -48,11 +50,14 @@ function text_feeds_set_target($source, $entity, $target, $value) { $info = field_info_field($target); + // Set the language of the field depending on the mapping configuration. + $langcode = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE; + // Iterate over all values. - $field = isset($entity->$target) ? $entity->$target : array('und' => array()); + $field = isset($entity->$target) ? $entity->$target : array($langcode => array()); // Allow for multiple mappings to the same target. - $delta = count($field['und']); + $delta = count($field[$langcode]); foreach ($value as $v) { @@ -65,15 +70,52 @@ function text_feeds_set_target($source, $entity, $target, $value) { } if (is_scalar($v)) { - $field['und'][$delta]['value'] = $v; + $field[$langcode][$delta]['value'] = $v; if (isset($format)) { - $field['und'][$delta]['format'] = $format; + $field[$langcode][$delta]['format'] = $format; } $delta++; } } + feeds_mapper_add_translation($entity); + $entity->$target = $field; } + +/** + * Mapping configuration summary for text fields + * + * @param array $mapping + * Associative array of the mapping settings. + * @param array $target + * Array of target settings, as defined by the processor or + * hook_feeds_processor_targets_alter(). + * @param array $form + * The whole mapping form. + * @param array $form_state + * The form state of the mapping form. + * + * @return string + * Returns, as a string that may contain HTML, the summary to display while + * the full form isn't visible. + * If the return value is empty, no summary and no option to view the form + * will be displayed. + */ +function text_feeds_summary_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_summary_language($mapping, $target, $form, $form_state); +} + + +/** + * Settings form callback. + * + * @return array + * The per mapping configuration form. Once the form is saved, $mapping will + * be populated with the form values. + */ +function text_feeds_form_callback($mapping, $target, $form, $form_state) { + return feeds_mapper_form_language($mapping); +}