1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* On behalf implementation of Feeds mapping API for path.module.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
41cc1b08
|
Assos Assos
|
* Implements hook_feeds_processor_targets().
|
10 |
85ad3d82
|
Assos Assos
|
*/
|
11 |
41cc1b08
|
Assos Assos
|
function path_feeds_processor_targets($entity_type, $bundle_name) {
|
12 |
|
|
$targets = array();
|
13 |
|
|
|
14 |
85ad3d82
|
Assos Assos
|
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 |
41cc1b08
|
Assos Assos
|
'summary_callbacks' => array('path_feeds_summary_callback'),
|
23 |
|
|
'form_callbacks' => array('path_feeds_form_callback'),
|
24 |
85ad3d82
|
Assos Assos
|
);
|
25 |
|
|
break;
|
26 |
|
|
}
|
27 |
41cc1b08
|
Assos Assos
|
|
28 |
|
|
return $targets;
|
29 |
85ad3d82
|
Assos Assos
|
}
|
30 |
|
|
|
31 |
|
|
/**
|
32 |
41cc1b08
|
Assos Assos
|
* Callback for mapping path aliases.
|
33 |
85ad3d82
|
Assos Assos
|
*/
|
34 |
41cc1b08
|
Assos Assos
|
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 |
85ad3d82
|
Assos Assos
|
}
|
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 |
41cc1b08
|
Assos Assos
|
// 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 |
85ad3d82
|
Assos Assos
|
}
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* Mapping configuration summary for path.module.
|
69 |
|
|
*/
|
70 |
41cc1b08
|
Assos Assos
|
function path_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
|
71 |
85ad3d82
|
Assos Assos
|
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 |
41cc1b08
|
Assos Assos
|
function path_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
|
88 |
85ad3d82
|
Assos Assos
|
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 |
|
|
} |