| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Hook implementations for view alias module integration.
|
| 6 |
*
|
| 7 |
* @ingroup pathauto
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_pathauto().
|
| 12 |
*/
|
| 13 |
function view_alias_pathauto($op) {
|
| 14 |
switch ($op) {
|
| 15 |
case 'settings':
|
| 16 |
$settings = array();
|
| 17 |
$settings['module'] = 'view_alias';
|
| 18 |
$settings['token_type'] = 'view_alias';
|
| 19 |
$settings['groupheader'] = t('View Alias settings');
|
| 20 |
$settings['patterndescr'] = t('Default View Alias pattern (applies to all views with a term argument and page display)');
|
| 21 |
$settings['patterndefault'] = t('');
|
| 22 |
$settings['bulkname'] = t('Bulk generate aliases for views with term arguments that are not aliased');
|
| 23 |
$settings['bulkdescr'] = t('Generate aliases for all existing views with term arguments which do not already have aliases.');
|
| 24 |
|
| 25 |
// left out patterns since we dont use them.
|
| 26 |
$settings['placeholders'] = array();
|
| 27 |
|
| 28 |
return (object) $settings;
|
| 29 |
|
| 30 |
default:
|
| 31 |
break;
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
*
|
| 37 |
* Do the bulk updating for view aliases
|
| 38 |
*
|
| 39 |
*/
|
| 40 |
function view_alias_pathauto_bulkupdate() {
|
| 41 |
|
| 42 |
$aliasable = _get_aliasable_displays();
|
| 43 |
|
| 44 |
foreach($aliasable as $alias) {
|
| 45 |
if(variable_get("pathauto_view_alias_{$alias->path}", FALSE)) {
|
| 46 |
$terms = taxonomy_get_tree($alias->varg);
|
| 47 |
|
| 48 |
foreach($terms as $term) {
|
| 49 |
$clean_term_name = pathauto_cleanstring($term->name);
|
| 50 |
path_set_alias("$alias->path/$term->tid","$alias->path/$clean_term_name");
|
| 51 |
drupal_set_message("Alias $alias->path/$term->tid to $alias->path/$clean_term_name");
|
| 52 |
}
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
*
|
| 60 |
* Implementation of hook_path_alais_types from pathauto
|
| 61 |
* allows me to hook into the bulk delete
|
| 62 |
*
|
| 63 |
*/
|
| 64 |
function view_alias_path_alias_types() {
|
| 65 |
$aliasable = _get_aliasable_displays();
|
| 66 |
foreach($aliasable as $alias) {
|
| 67 |
$objects["$alias->path/%"] = t("View page displays having a path of '$alias->path'");
|
| 68 |
}
|
| 69 |
return $objects;
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
*
|
| 74 |
* Implementation of hook_form_alter
|
| 75 |
* remove the default form settings and add our own since view alias are different from the regular
|
| 76 |
* alaises
|
| 77 |
*
|
| 78 |
*/
|
| 79 |
function view_alias_form_alter(&$form, $form_state, $form_id) {
|
| 80 |
if($form_id == 'pathauto_admin_settings') {
|
| 81 |
|
| 82 |
unset($form['view_alias']['pathauto_view_alias_pattern']);
|
| 83 |
unset($form['view_alias']['token_help']);
|
| 84 |
$form['view_alias']['views_to_alias'] = array(
|
| 85 |
'#type' => 'fieldset',
|
| 86 |
'#title' => t('Views available for aliasing'),
|
| 87 |
'#description' => t('Check the views for which aliases should be bulk generated.'),
|
| 88 |
'#weight' => -1,
|
| 89 |
);
|
| 90 |
|
| 91 |
$aliasable = _get_aliasable_displays();
|
| 92 |
foreach($aliasable as $alias) {
|
| 93 |
$voc = taxonomy_vocabulary_load($alias->varg);
|
| 94 |
$form['view_alias']['views_to_alias']["pathauto_view_alias_{$alias->path}"] = array(
|
| 95 |
'#type' => 'checkbox',
|
| 96 |
'#title' => t("View $alias->view_name, display $alias->display_name, on path $alias->path, with $voc->name arguments."),
|
| 97 |
'#weight' => -1,
|
| 98 |
'#default_value' => variable_get("pathauto_view_alias_{$alias->path}", 0),
|
| 99 |
);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
|
| 105 |
/**
|
| 106 |
*
|
| 107 |
* find the views that can be aliased.
|
| 108 |
* that means have a path url and use a term id as an argument
|
| 109 |
* build and array of objects, keyed with the view name, having the view path, and the vocab id for the terms used
|
| 110 |
* array(
|
| 111 |
* 0 =>
|
| 112 |
* object (
|
| 113 |
* 'view_name' -> 'viewname'
|
| 114 |
* 'display_name' -> 'display name'
|
| 115 |
* 'path' -> 'view url path'
|
| 116 |
* 'varg' -> 'vocabulary id'
|
| 117 |
* )
|
| 118 |
* )
|
| 119 |
*/
|
| 120 |
function _get_aliasable_displays() {
|
| 121 |
$aliasable_views = array();
|
| 122 |
|
| 123 |
$sql = "SELECT DISTINCT(vv.name) FROM drupal_views_view vv JOIN drupal_views_display vd ";
|
| 124 |
$sql .= "ON vv.vid = vd.vid where vd.display_plugin = 'page';";
|
| 125 |
|
| 126 |
$result = db_query($sql);
|
| 127 |
while($row = db_fetch_object($result)) {
|
| 128 |
$aview = views_get_view($row->name);
|
| 129 |
$alias = NULL;
|
| 130 |
$alias->view_name = $row->name;
|
| 131 |
$default_vocab_arg;
|
| 132 |
|
| 133 |
foreach($aview->display as $key => $display) {
|
| 134 |
// check default for args and save for later
|
| 135 |
$alias->display_name = $key;
|
| 136 |
|
| 137 |
if($key == 'default') {
|
| 138 |
$alias->varg = _find_view_arguments($display);
|
| 139 |
continue;
|
| 140 |
}
|
| 141 |
// its some sort of page cause the query says so.
|
| 142 |
else {
|
| 143 |
//dpr($display);
|
| 144 |
// add the page and replace overridden args.
|
| 145 |
$alias->path = $display->display_options['path'];
|
| 146 |
$overriden = _find_view_arguments($display);
|
| 147 |
if($overridden) {
|
| 148 |
$alias->varg = $overridden;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
if(!empty($alias->path) && !empty($alias->varg))
|
| 152 |
$aliasable_views[] = drupal_clone($alias);
|
| 153 |
}
|
| 154 |
|
| 155 |
}
|
| 156 |
|
| 157 |
return $aliasable_views;
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* helper to dig out the view arguments.
|
| 162 |
*/
|
| 163 |
function _find_view_arguments($display) {
|
| 164 |
$dis_args = $display->display_options['arguments']['tid'];
|
| 165 |
// array_sum is used since if no vocabularies are selected, the sum of the argumets will be 0
|
| 166 |
// and we need to make sure there is a vocabular set.
|
| 167 |
|
| 168 |
if($dis_args['validate_argument_type'] == 'tid' && array_sum($dis_args['validate_argument_vocabulary']) > 0) {
|
| 169 |
// remove zero entries
|
| 170 |
$no_zeroes = array_diff($dis_args['validate_argument_vocabulary'], array(0));
|
| 171 |
return implode(",", $no_zeroes);
|
| 172 |
}
|
| 173 |
return FALSE;
|
| 174 |
}
|