path.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for path.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsNodeProcessor::getMappingTargets().
  10. */
  11. function path_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  12. switch ($entity_type) {
  13. case 'node':
  14. case 'taxonomy_term':
  15. case 'user':
  16. $targets['path_alias'] = array(
  17. 'name' => t('Path alias'),
  18. 'description' => t('URL path alias of the node.'),
  19. 'callback' => 'path_feeds_set_target',
  20. 'summary_callback' => 'path_feeds_summary_callback',
  21. 'form_callback' => 'path_feeds_form_callback',
  22. );
  23. break;
  24. }
  25. }
  26. /**
  27. * Callback for mapping. Here is where the actual mapping happens.
  28. *
  29. * When the callback is invoked, $target contains the name of the field the
  30. * user has decided to map to and $value contains the value of the feed item
  31. * element the user has picked as a source.
  32. */
  33. function path_feeds_set_target($source, $entity, $target, $value, $mapping) {
  34. if (empty($value)) {
  35. $value = '';
  36. }
  37. // Path alias cannot be multi-valued, so use the first value.
  38. if (is_array($value)) {
  39. $value = $value[0];
  40. }
  41. $entity->path = array();
  42. $entity_type = $source->importer->processor->entityType();
  43. list($id, , ) = entity_extract_ids($entity_type, $entity);
  44. if ($id) {
  45. $uri = entity_uri($entity_type, $entity);
  46. // Check for existing aliases.
  47. if ($path = path_load($uri['path'])) {
  48. $entity->path = $path;
  49. }
  50. }
  51. $entity->path['pathauto'] = FALSE;
  52. // Allow pathauto to set the path alias if the option is set, and this value
  53. // is empty.
  54. if (!empty($mapping['pathauto_override']) && !$value) {
  55. $entity->path['pathauto'] = TRUE;
  56. }
  57. else {
  58. $entity->path['alias'] = ltrim($value, '/');
  59. }
  60. }
  61. /**
  62. * Mapping configuration summary for path.module.
  63. *
  64. * @param $mapping
  65. * Associative array of the mapping settings.
  66. * @param $target
  67. * Array of target settings, as defined by the processor or
  68. * hook_feeds_processor_targets_alter().
  69. * @param $form
  70. * The whole mapping form.
  71. * @param $form_state
  72. * The form state of the mapping form.
  73. *
  74. * @return
  75. * Returns, as a string that may contain HTML, the summary to display while
  76. * the full form isn't visible.
  77. * If the return value is empty, no summary and no option to view the form
  78. * will be displayed.
  79. */
  80. function path_feeds_summary_callback($mapping, $target, $form, $form_state) {
  81. if (!module_exists('pathauto')) {
  82. return;
  83. }
  84. if (empty($mapping['pathauto_override'])) {
  85. return t('Do not allow Pathauto if empty.');
  86. }
  87. else {
  88. return t('Allow Pathauto if empty.');
  89. }
  90. }
  91. /**
  92. * Settings form callback.
  93. *
  94. * @return
  95. * The per mapping configuration form. Once the form is saved, $mapping will
  96. * be populated with the form values.
  97. */
  98. function path_feeds_form_callback($mapping, $target, $form, $form_state) {
  99. return array(
  100. 'pathauto_override' => array(
  101. '#type' => 'checkbox',
  102. '#title' => t('Allow Pathauto to set the alias if the value is empty.'),
  103. '#default_value' => !empty($mapping['pathauto_override']),
  104. ),
  105. );
  106. }