path.inc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for path.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets().
  8. */
  9. function path_feeds_processor_targets($entity_type, $bundle_name) {
  10. $targets = array();
  11. switch ($entity_type) {
  12. case 'node':
  13. case 'taxonomy_term':
  14. case 'user':
  15. $targets['path_alias'] = array(
  16. 'name' => t('Path alias'),
  17. 'description' => t('URL path alias of the node.'),
  18. 'callback' => 'path_feeds_set_target',
  19. 'summary_callbacks' => array('path_feeds_summary_callback'),
  20. 'form_callbacks' => array('path_feeds_form_callback'),
  21. );
  22. break;
  23. }
  24. return $targets;
  25. }
  26. /**
  27. * Callback for mapping path aliases.
  28. */
  29. function path_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  30. $alias = FALSE;
  31. // Path alias cannot be multi-valued, so use the first non-empty value.
  32. foreach ($values as $value) {
  33. $value = ltrim(trim($value), '/');
  34. if (strlen($value)) {
  35. $alias = $value;
  36. break;
  37. }
  38. }
  39. $entity->path = array();
  40. $entity_type = $source->importer->processor->entityType();
  41. list($id, , ) = entity_extract_ids($entity_type, $entity);
  42. if ($id) {
  43. $uri = entity_uri($entity_type, $entity);
  44. // Check for existing aliases.
  45. if ($path = path_load($uri['path'])) {
  46. $entity->path = $path;
  47. }
  48. }
  49. // Allow pathauto to set the path alias if the option is set, and the value is
  50. // empty.
  51. $entity->path['pathauto'] = !empty($mapping['pathauto_override']) && $alias === FALSE;
  52. $entity->path['alias'] = (string) $alias;
  53. }
  54. /**
  55. * Mapping configuration summary for path.module.
  56. */
  57. function path_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
  58. if (!module_exists('pathauto')) {
  59. return;
  60. }
  61. if (empty($mapping['pathauto_override'])) {
  62. return t('Do not allow Pathauto if empty.');
  63. }
  64. else {
  65. return t('Allow Pathauto if empty.');
  66. }
  67. }
  68. /**
  69. * Settings form callback.
  70. */
  71. function path_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
  72. return array(
  73. 'pathauto_override' => array(
  74. '#type' => 'checkbox',
  75. '#title' => t('Allow Pathauto to set the alias if the value is empty.'),
  76. '#default_value' => !empty($mapping['pathauto_override']),
  77. ),
  78. );
  79. }