diff --git a/feeds.module b/feeds.module index f581d3f..5fb234e 100644 --- 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, $langcode) { + + // 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 index aa967b9..2f79125 100644 --- a/mappers/file.inc +++ b/mappers/file.inc @@ -21,6 +21,8 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'callback' => 'file_feeds_set_target', 'description' => t('The URI of the @label field.', array('@label' => $instance['label'])), 'real_target' => $name, + 'summary_callback' => 'file_feeds_summary_callback', + 'form_callback' => 'file_feeds_form_callback', ); if ($info['type'] == 'image') { @@ -29,12 +31,16 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam 'callback' => 'file_feeds_set_target', 'description' => t('The alt tag of the @label field.', array('@label' => $instance['label'])), 'real_target' => $name, + 'summary_callback' => 'file_feeds_summary_callback', + 'form_callback' => 'file_feeds_form_callback', ); $targets[$name . ':title'] = array( 'name' => t('@label: Title', array('@label' => $instance['label'])), 'callback' => 'file_feeds_set_target', 'description' => t('The title of the @label field.', array('@label' => $instance['label'])), 'real_target' => $name, + 'summary_callback' => 'file_feeds_summary_callback', + 'form_callback' => 'file_feeds_form_callback', ); } } @@ -62,6 +68,9 @@ function file_feeds_set_target($source, $entity, $target, $value) { list($field_name, $sub_field) = explode(':', $target . ':uri'); $info = field_info_field($field_name); + // Set the language of the field depending on the mapping configuration. + $langcode = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE; + if ($sub_field == 'uri') { foreach ($value as $k => $v) { @@ -96,29 +105,29 @@ function file_feeds_set_target($source, $entity, $target, $value) { } // Populate entity. - $field = isset($entity->$field_name) ? $entity->$field_name : array(LANGUAGE_NONE => array()); + $field = isset($entity->$field_name) ? $entity->$field_name : array($langcode => array()); $delta = 0; foreach ($value as $v) { if ($info['cardinality'] == $delta) { break; } - if (!isset($field[LANGUAGE_NONE][$delta])) { - $field[LANGUAGE_NONE][$delta] = array(); + if (!isset($field[$langcode][$delta])) { + $field[$langcode][$delta] = array(); } switch ($sub_field) { case 'alt': case 'title': - $field[LANGUAGE_NONE][$delta][$sub_field] = $v; + $field[$langcode][$delta][$sub_field] = $v; break; case 'uri': try { $file = $v->getFile($destination); - $field[LANGUAGE_NONE][$delta] += (array) $file; + $field[$langcode][$delta] += (array) $file; // @todo: Figure out how to properly populate this field. - $field[LANGUAGE_NONE][$delta]['display'] = 1; + $field[$langcode][$delta]['display'] = 1; } catch (Exception $e) { watchdog_exception('Feeds', $e, nl2br(check_plain($e))); @@ -129,5 +138,41 @@ function file_feeds_set_target($source, $entity, $target, $value) { $delta++; } + feeds_mapper_add_translation($entity, $langcode); + $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 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..062412a --- 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, $langcode); + $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..041a299 --- 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,19 @@ 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()); + + // If it's a new language we need to add it to the field + if (!isset($field[$langcode])) { + $field[$langcode] = array(); + } // Allow for multiple mappings to the same target. - $delta = count($field['und']); + $delta = count($field[$langcode]); foreach ($value as $v) { @@ -65,10 +75,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, $langcode); + $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..2d09fb0 --- a/mappers/taxonomy.inc +++ b/mappers/taxonomy.inc @@ -93,7 +93,10 @@ 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); @@ -116,10 +119,15 @@ 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()); + + // If it's a new language we need to add it to the field + if (!isset($field[$langcode])) { + $field[$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 +179,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, $langcode); + $entity->$target = $field; } @@ -263,10 +273,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])); } - return t('Search taxonomy terms by: @search', array('@search' => $options[$mapping['term_search']])); + else { + $output = 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 +294,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 +314,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..9511890 --- 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,19 @@ 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()); + + // If it's a new language we need to add it to the field + if (!isset($field[$langcode])) { + $field[$langcode] = array(); + } // Allow for multiple mappings to the same target. - $delta = count($field['und']); + $delta = count($field[$langcode]); foreach ($value as $v) { @@ -65,15 +75,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, $langcode); + $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); +}