Skip to content

Commit 00e508c

Browse files
committed
Added module for generating path aliases for library sub sites.
1 parent 297c4a5 commit 00e508c

File tree

3 files changed

+247
-0
lines changed

3 files changed

+247
-0
lines changed

ding_path_alias/ding_path_alias.info

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
core = "6.x"
2+
dependencies[] = "pathauto"
3+
dependencies[] = "strongarm"
4+
dependencies[] = "transliteration"
5+
description = "Provides path alias configuration for Ding!"
6+
name = "Ding! path alias"
7+
package = "Ding!"
8+
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* @file
4+
* Path alias configuration for Ding!
5+
*/
6+
7+
/**
8+
* Implements hook_path_alias_types().
9+
*/
10+
function ding_path_alias_path_alias_types() {
11+
$types = array();
12+
13+
if (module_exists('ding_library')) {
14+
$types['node/%/bookpc'] = t('Library book PC pages');
15+
$types['node/%/faciliteter'] = t('Library facilities pages');
16+
$types['node/%/lokalt'] = t('Library local pages');
17+
$types['node/%/om'] = t('Library about page');
18+
19+
if (module_exists('ding_content')) {
20+
$types['node/%/blog'] = t('Library news pages');
21+
}
22+
23+
if (module_exists('ding_event')) {
24+
$types['node/%/arrangementer'] = t('Library event pages');
25+
}
26+
}
27+
28+
return $types;
29+
}
30+
31+
/**
32+
* Implements hook_pathauto().
33+
*/
34+
function ding_path_alias_pathauto($op) {
35+
switch ($op) {
36+
case 'settings':
37+
// Pathauto module isn't created with the kind of functionality we have in
38+
// mind: Create a bunch of different path aliases with different url
39+
// patterns. We need to create a setting for each pattern type we want to
40+
// use as the batch callback needs to know what the internal url will be.
41+
// Since pathauto will key the form items on the module value, we need to
42+
// create a unique module defition per url pattern.
43+
$settings = array();
44+
45+
// Settings
46+
$defaults = array(
47+
'token_type' => 'node',
48+
'batch_file' => drupal_get_path('module', 'ding_path_alias') . '/ding_path_alias.pathauto.inc',
49+
);
50+
51+
if (module_exists('ding_library')) {
52+
$settings['library_bookpc_page'] = (object) array_merge($defaults, array(
53+
'module' => 'library_bookpc_page',
54+
'patterndescr' => t('Library bookPC page'),
55+
'groupheader' => t('Library bookPC page'),
56+
'patterndefault' => '[panels_subsites_prefix]/[panels_subsites_slug]/bookpc',
57+
'batch_update_callback' => 'ding_path_alias_library_bookpc_page_pathauto_bulk_update_batch_process',
58+
));
59+
60+
$settings['library_facilities_page'] = (object) array_merge($defaults, array(
61+
'module' => 'library_facilities_page',
62+
'patterndescr' => t('Library facilities page'),
63+
'groupheader' => t('Library facilities page'),
64+
'patterndefault' => '[panels_subsites_prefix]/[panels_subsites_slug]/faciliteter',
65+
'batch_update_callback' => 'ding_path_alias_library_facilities_page_pathauto_bulk_update_batch_process',
66+
));
67+
68+
$settings['library_lokalt_page'] = (object) array_merge($defaults, array(
69+
'module' => 'library_local_page',
70+
'patterndescr' => t('Local page'),
71+
'groupheader' => t('Library local page'),
72+
'patterndefault' => '[panels_subsites_prefix]/[panels_subsites_slug]/lokalt',
73+
'batch_update_callback' => 'ding_path_alias_library_local_page_pathauto_bulk_update_batch_process',
74+
));
75+
76+
$settings['library_about_page'] = (object) array_merge($defaults, array(
77+
'module' => 'library_about_page',
78+
'patterndescr' => t('Library about page'),
79+
'groupheader' => t('Library about page'),
80+
'patterndefault' => '[panels_subsites_prefix]/[panels_subsites_slug]/om',
81+
'batch_update_callback' => 'ding_path_alias_library_about_page_pathauto_bulk_update_batch_process',
82+
));
83+
84+
if (module_exists('ding_content')) {
85+
$settings['library_blog_page'] = (object) array_merge($defaults, array(
86+
'module' => 'library_blog_page',
87+
'patterndescr' => t('Library blog page'),
88+
'groupheader' => t('Library blog page'),
89+
'patterndefault' => '[panels_subsites_prefix]/[panels_subsites_slug]/blog',
90+
'batch_update_callback' => 'ding_path_alias_library_blog_page_pathauto_bulk_update_batch_process',
91+
));
92+
}
93+
94+
if (module_exists('ding_event')) {
95+
$settings['library_event_page'] = (object) array_merge($defaults, array(
96+
'module' => 'library_event_page',
97+
'patterndescr' => t('Library event page'),
98+
'groupheader' => t('Library event page'),
99+
'patterndefault' => '[panels_subsites_prefix]/[panels_subsites_slug]/arrangementer',
100+
'batch_update_callback' => 'ding_path_alias_library_event_page_pathauto_bulk_update_batch_process',
101+
));
102+
}
103+
}
104+
105+
return $settings;
106+
}
107+
}
108+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
function ding_path_alias_library_bookpc_page_pathauto_bulk_update_batch_process(&$context) {
4+
_ding_path_alias_bulk_update_batch_process($context, 'bookpc', 'bookpc');
5+
}
6+
7+
function ding_path_alias_library_facilities_page_pathauto_bulk_update_batch_process(&$context) {
8+
_ding_path_alias_bulk_update_batch_process($context, 'facilities', 'faciliteter');
9+
}
10+
11+
function ding_path_alias_library_local_page_pathauto_bulk_update_batch_process(&$context) {
12+
_ding_path_alias_bulk_update_batch_process($context, 'local', 'lokalt');
13+
}
14+
15+
function ding_path_alias_library_about_page_pathauto_bulk_update_batch_process(&$context) {
16+
_ding_path_alias_bulk_update_batch_process($context, 'about', 'om');
17+
}
18+
19+
function ding_path_alias_library_blog_page_pathauto_bulk_update_batch_process(&$context) {
20+
_ding_path_alias_bulk_update_batch_process($context, 'blog', 'blog');
21+
}
22+
23+
function ding_path_alias_library_event_page_pathauto_bulk_update_batch_process(&$context) {
24+
_ding_path_alias_bulk_update_batch_process($context, 'event', 'arrangementer');
25+
}
26+
27+
function _ding_path_alias_bulk_update_batch_process(&$context, $name, $url_suffix) {
28+
if (!isset($context['sandbox']['current'])) {
29+
$context['sandbox']['count'] = 0;
30+
$context['sandbox']['current'] = 0;
31+
}
32+
33+
$concat = _pathauto_sql_concat("'node/'", 'n.nid', "'/$url_suffix'");
34+
35+
$sql = "
36+
SELECT n.nid FROM {node} AS n
37+
LEFT JOIN {url_alias} AS ua ON $concat = ua.src
38+
WHERE ua.src IS NULL AND n.type = 'library' AND n.nid > %d
39+
ORDER BY n.nid
40+
";
41+
42+
$args = array($context['sandbox']['current']);
43+
44+
// Get the total amount of items to process.
45+
if (!isset($context['sandbox']['total'])) {
46+
$context['sandbox']['total'] = db_result(db_query(_pathauto_sql_count($sql), $args));
47+
48+
// If there are no nodes to update, the stop immediately.
49+
if (empty($context['sandbox']['total'])) {
50+
$context['finished'] = 1;
51+
return;
52+
}
53+
}
54+
55+
$query = db_query_range($sql, $args, 0, 25);
56+
$nids = array();
57+
while ($nid = db_result($query)) {
58+
$nids[] = $nid;
59+
}
60+
61+
// Update the alias for each selected node.
62+
ding_path_alias_update_alias_multiple($nids, 'bulkupdate', array(
63+
'name' => $name,
64+
'url_suffix' => $url_suffix,
65+
));
66+
67+
$context['sandbox']['count'] += count($nids);
68+
$context['sandbox']['current'] = max($nids);
69+
$context['message'] = t('Updated @type alias for library @nid.', array('@type' => $url_suffix, '@nid' => end($nids)));
70+
71+
if ($context['sandbox']['count'] != $context['sandbox']['total']) {
72+
$context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
73+
}
74+
}
75+
76+
/**
77+
* Update the URL aliases for an individual node.
78+
*
79+
* @param $node
80+
* A node object.
81+
* @param $op
82+
* Operation being performed on the node ('insert', 'update' or 'bulkupdate').
83+
* @param $options
84+
* An optional array of additional options.
85+
*/
86+
function ding_path_alias_update_alias($node, $op, $options = array()) {
87+
// Skip processing if the user has disabled pathauto for the node.
88+
if (isset($node->pathauto_perform_alias) && empty($node->pathauto_perform_alias)) {
89+
return;
90+
}
91+
92+
$options += array(
93+
'language' => isset($node->language) ? $node->language : '',
94+
);
95+
96+
// Skip processing if the node has no pattern.
97+
if (!pathauto_pattern_load_by_entity('library_' . $options['name'] . '_page', '', $options['language'])) {
98+
return;
99+
}
100+
101+
module_load_include('inc', 'pathauto');
102+
if ($alias = pathauto_create_alias('library_' . $options['name'] . '_page', $op, 'node/' . $node->nid . '/' . $options['url_suffix'], array('node' => $node), $node->nid, $node->type, $options['language'])) {
103+
$node->path = $alias;
104+
}
105+
}
106+
107+
/**
108+
* Update the URL aliases for multiple nodes.
109+
*
110+
* @param $nids
111+
* An array of node IDs.
112+
* @param $op
113+
* Operation being performed on the nodes ('insert', 'update' or
114+
* 'bulkupdate').
115+
* @param $options
116+
* An optional array of additional options.
117+
*/
118+
function ding_path_alias_update_alias_multiple($nids, $op, $options = array()) {
119+
$options += array('message' => FALSE);
120+
121+
foreach ($nids as $nid) {
122+
if ($node = node_load($nid, NULL, TRUE)) {
123+
ding_path_alias_update_alias($node, $op, $options);
124+
}
125+
}
126+
127+
if (!empty($options['message'])) {
128+
drupal_set_message(format_plural(count($nids), 'Updated URL alias for 1 node.', 'Updated URL aliases for @count nodes.'));
129+
}
130+
}
131+

0 commit comments

Comments
 (0)