Projet

Général

Profil

Paste
Télécharger (2,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / mappers / path.inc @ 41cc1b08

1
<?php
2

    
3
/**
4
 * @file
5
 * On behalf implementation of Feeds mapping API for path.module.
6
 */
7

    
8
/**
9
 * Implements hook_feeds_processor_targets().
10
 */
11
function path_feeds_processor_targets($entity_type, $bundle_name) {
12
  $targets = array();
13

    
14
  switch ($entity_type) {
15
    case 'node':
16
    case 'taxonomy_term':
17
    case 'user':
18
      $targets['path_alias'] = array(
19
        'name' => t('Path alias'),
20
        'description' => t('URL path alias of the node.'),
21
        'callback' => 'path_feeds_set_target',
22
        'summary_callbacks' => array('path_feeds_summary_callback'),
23
        'form_callbacks' => array('path_feeds_form_callback'),
24
      );
25
      break;
26
  }
27

    
28
  return $targets;
29
}
30

    
31
/**
32
 * Callback for mapping path aliases.
33
 */
34
function path_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
35
  $alias = FALSE;
36
  // Path alias cannot be multi-valued, so use the first non-empty value.
37
  foreach ($values as $value) {
38
    $value = ltrim(trim($value), '/');
39
    if (strlen($value)) {
40
      $alias = $value;
41
      break;
42
    }
43
  }
44

    
45
  $entity->path = array();
46

    
47
  $entity_type = $source->importer->processor->entityType();
48

    
49
  list($id, , ) = entity_extract_ids($entity_type, $entity);
50

    
51
  if ($id) {
52
    $uri = entity_uri($entity_type, $entity);
53

    
54
    // Check for existing aliases.
55
    if ($path = path_load($uri['path'])) {
56
      $entity->path = $path;
57
    }
58
  }
59

    
60
  // Allow pathauto to set the path alias if the option is set, and the value is
61
  // empty.
62
  $entity->path['pathauto'] = !empty($mapping['pathauto_override']) && $alias === FALSE;
63

    
64
  $entity->path['alias'] = (string) $alias;
65
}
66

    
67
/**
68
 * Mapping configuration summary for path.module.
69
 */
70
function path_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
71
  if (!module_exists('pathauto')) {
72
    return;
73
  }
74

    
75
  if (empty($mapping['pathauto_override'])) {
76
    return t('Do not allow Pathauto if empty.');
77
  }
78

    
79
  else {
80
    return t('Allow Pathauto if empty.');
81
  }
82
}
83

    
84
/**
85
 * Settings form callback.
86
 */
87
function path_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
88
  return array(
89
    'pathauto_override' => array(
90
      '#type' => 'checkbox',
91
      '#title' => t('Allow Pathauto to set the alias if the value is empty.'),
92
      '#default_value' => !empty($mapping['pathauto_override']),
93
    ),
94
  );
95
}