locale.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for locale.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. */
  9. function locale_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle_name) {
  10. foreach (array_keys($targets) as $target) {
  11. $targets[$target]['preprocess_callbacks'][] = 'locale_feeds_preprocess_callback';
  12. $targets[$target]['summary_callbacks'][] = 'locale_feeds_summary_callback';
  13. $targets[$target]['form_callbacks'][] = 'locale_feeds_form_callback';
  14. }
  15. }
  16. /**
  17. * Preprocess callback that sets the configured mapping language.
  18. */
  19. function locale_feeds_preprocess_callback(array $target, array &$mapping) {
  20. if (empty($mapping['field_language'])) {
  21. return;
  22. }
  23. $mapping['language'] = $mapping['field_language'];
  24. }
  25. /**
  26. * Summary callback.
  27. */
  28. function locale_feeds_summary_callback(array $mapping, array $target, array $form, array $form_state) {
  29. $entity_type = $form_state['build_info']['args'][0]->processor->entityType();
  30. $translatable = _locale_feeds_target_is_translatable($entity_type, $mapping['target']);
  31. $mapping += array('field_language' => LANGUAGE_NONE);
  32. $language_options = array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name');
  33. $error = NULL;
  34. if ($mapping['field_language'] !== LANGUAGE_NONE && !$translatable) {
  35. // This is an invalid configuration that can come from disabling
  36. // entity_translation.
  37. $error = t('Field not translatable');
  38. }
  39. if (!isset($language_options[$mapping['field_language']])) {
  40. // This is an invalid configuration that can be caused by disabling or
  41. // removing the language in question.
  42. $error = t('Language \'@lang\' not available', array('@lang' => $mapping['field_language']));
  43. }
  44. // Nothing to see here.
  45. if (!$error && !$translatable) {
  46. return;
  47. }
  48. if ($error) {
  49. return t('Language: <strong>Error: @error</strong>', array('@error' => $error));
  50. }
  51. return t('Language: %lang', array('%lang' => $language_options[$mapping['field_language']]));
  52. }
  53. /**
  54. * Form callback.
  55. */
  56. function locale_feeds_form_callback(array $mapping, array $target, array $form, array $form_state) {
  57. $form = array();
  58. $entity_type = $form_state['build_info']['args'][0]->processor->entityType();
  59. $translatable = _locale_feeds_target_is_translatable($entity_type, $mapping['target']);
  60. $mapping += array('field_language' => LANGUAGE_NONE);
  61. // This is an invalid configuration that can come from disabling
  62. // entity_translation.
  63. $error = $mapping['field_language'] !== LANGUAGE_NONE && !$translatable;
  64. // Nothing to see here.
  65. if (!$error && !$translatable) {
  66. return $form;
  67. }
  68. $language_options = array(LANGUAGE_NONE => t('Language neutral'));
  69. if (!$error) {
  70. $language_options += locale_language_list('name');
  71. }
  72. $form['field_language'] = array(
  73. '#type' => 'select',
  74. '#title' => t('Language'),
  75. '#options' => $language_options,
  76. '#default_value' => $mapping['field_language'],
  77. );
  78. return $form;
  79. }
  80. /**
  81. * Determines if a target is translatable.
  82. *
  83. * @param string $entity_type
  84. * The entity type.
  85. * @param string $target
  86. * The target.
  87. *
  88. * @return bool
  89. * Returns true if the target is translatable, false if not.
  90. */
  91. function _locale_feeds_target_is_translatable($entity_type, $target) {
  92. list($field_name) = explode(':', $target, 2);
  93. $info = field_info_field($field_name);
  94. return !empty($info) && field_is_translatable($entity_type, $info);
  95. }