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