Projet

Général

Profil

Paste
Télécharger (8,99 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / pathauto / pathauto.api.php @ be58a50c

1
<?php
2
/**
3
 * @file
4
 * Documentation for pathauto API.
5
 *
6
 * It may be helpful to review some examples of integration from
7
 * pathauto.pathauto.inc.
8
 *
9
 * Pathauto works by using tokens in path patterns.  Thus the simplest
10
 * integration is just to provide tokens.  Token support is provided by Drupal
11
 * core. To provide additional token from your module, implement the following
12
 * hooks:
13
 *
14
 * hook_tokens() - http://api.drupal.org/api/function/hook_tokens
15
 * hook_token_info() - http://api.drupal.org/api/function/hook_token_info
16
 *
17
 * If you wish to provide pathauto integration for custom paths provided by your
18
 * module, there are a few steps involved.
19
 *
20
 * 1. hook_pathauto()
21
 *    Provide information required by pathauto for the settings form as well as
22
 *    bulk generation.  See the documentation for hook_pathauto() for more
23
 *    details.
24
 *
25
 * 2. pathauto_create_alias()
26
 *    At the appropriate time (usually when a new item is being created for
27
 *    which a generated alias is desired), call pathauto_create_alias() with the
28
 *    appropriate parameters to generate and create the alias. See the user,
29
 *    taxonomy, and node hook implementations in pathauto.module for examples.
30
 *    Also see the documentation for pathauto_create_alias().
31
 *
32
 * 3. pathauto_path_delete_all()
33
 *    At the appropriate time (usually when an item is being deleted), call
34
 *    pathauto_path_delete_all() to remove any aliases that were created for the
35
 *    content being removed.  See the documentation for
36
 *    pathauto_path_delete_all() for more details.
37
 *
38
 * 4. hook_path_alias_types()
39
 *    For modules that create new types of content that can be aliased with
40
 *    pathauto, a hook implementation is needed to allow the user to delete them
41
 *    all at once.  See the documentation for hook_path_alias_types() below for
42
 *    more information.
43
 *
44
 * There are other integration points with pathauto, namely alter hooks that
45
 * allow you to change the data used by pathauto at various points in the
46
 * process.  See the below hook documentation for details.
47
 */
48

    
49
/**
50
 * Used primarily by the bulk delete form.  This hooks provides pathauto the
51
 * information needed to bulk delete aliases created by your module.  The keys
52
 * of the return array are used by pathauto as the system path prefix to delete
53
 * from the url_aliases table.  The corresponding value is simply used as the
54
 * label for each type of path on the bulk delete form.
55
 *
56
 * @return
57
 *   An array whose keys match the beginning of the source paths
58
 *   (e.g.: "node/", "user/", etc.) and whose values describe the type of page
59
 *   (e.g.: "Content", "Users"). Like all displayed strings, these descriptions
60
 *   should be localized with t(). Use % to match interior pieces of a path,
61
 *   like "user/%/track". This is a database wildcard (meaning "user/%/track"
62
 *   matches "user/1/track" as well as "user/1/view/track").
63
 */
64
function hook_path_alias_types() {
65
  $objects['user/'] = t('Users');
66
  $objects['node/'] = t('Content');
67
  return $objects;
68
}
69

    
70
/**
71
 * Provide information about the way your module's aliases will be built.
72
 *
73
 * The information you provide here is used to build the form
74
 * on search/path/patterns. File pathauto.pathauto.inc provides example
75
 * implementations for system modules.
76
 *
77
 * @see node_pathauto
78
 *
79
 * @param $op
80
 *   At the moment this will always be 'settings'.
81
 *
82
 * @return object|null
83
 *   An object, or array of objects (if providing multiple groups of path
84
 *   patterns).  Each object should have the following members:
85
 *   - 'module': The module or entity type.
86
 *   - 'token_type': Which token type should be allowed in the patterns form.
87
 *   - 'groupheader': Translated label for the settings group
88
 *   - 'patterndescr': The translated label for the default pattern (e.g.,
89
 *      t('Default path pattern (applies to all content types with blank
90
 *      patterns below)')
91
 *   - 'patterndefault': Default pattern  (e.g. 'content/[node:title]'
92
 *   - 'batch_update_callback': The name of function that should be ran for
93
 *      bulk update. @see node_pathauto_bulk_update_batch_process for example
94
 *   - 'batch_file': The name of the file with the bulk update function.
95
 *   - 'patternitems': Optional. An array of descritpions keyed by bundles.
96
 */
97
function hook_pathauto($op) {
98
  switch ($op) {
99
    case 'settings':
100
      $settings = array();
101
      $settings['module'] = 'file';
102
      $settings['token_type'] = 'file';
103
      $settings['groupheader'] = t('File paths');
104
      $settings['patterndescr'] = t('Default path pattern (applies to all file types with blank patterns below)');
105
      $settings['patterndefault'] = 'files/[file:name]';
106
      $settings['batch_update_callback'] = 'file_entity_pathauto_bulk_update_batch_process';
107
      $settings['batch_file'] = drupal_get_path('module', 'file_entity') . '/file_entity.pathauto.inc';
108

    
109
      foreach (file_type_get_enabled_types() as $file_type => $type) {
110
        $settings['patternitems'][$file_type] = t('Pattern for all @file_type paths.', array('@file_type' => $type->label));
111
      }
112
      return (object) $settings;
113

    
114
    default:
115
      break;
116
  }
117
}
118

    
119
/**
120
 * Determine if a possible URL alias would conflict with any existing paths.
121
 *
122
 * Returning TRUE from this function will trigger pathauto_alias_uniquify() to
123
 * generate a similar URL alias with a suffix to avoid conflicts.
124
 *
125
 * @param string $alias
126
 *   The potential URL alias.
127
 * @param string $source
128
 *   The source path for the alias (e.g. 'node/1').
129
 * @param string $langcode
130
 *   The language code for the alias (e.g. 'en').
131
 *
132
 * @return bool
133
 *   TRUE if $alias conflicts with an existing, reserved path, or FALSE/NULL if
134
 *   it does not match any reserved paths.
135
 *
136
 * @see pathauto_alias_uniquify()
137
 */
138
function hook_pathauto_is_alias_reserved($alias, $source, $langcode) {
139
  // Check our module's list of paths and return TRUE if $alias matches any of
140
  // them.
141
  return (bool) db_query("SELECT 1 FROM {mytable} WHERE path = :path", array(':path' => $alias))->fetchField();
142
}
143

    
144
/**
145
 * Alter the pattern to be used before an alias is generated by Pathauto.
146
 *
147
 * This hook will only be called if a default pattern is configured (on
148
 * admin/config/search/path/patterns).
149
 *
150
 * @param string $pattern
151
 *   The alias pattern for Pathauto to pass to token_replace() to generate the
152
 *   URL alias.
153
 * @param array $context
154
 *   An associative array of additional options, with the following elements:
155
 *   - 'module': The module or entity type being aliased.
156
 *   - 'op': A string with the operation being performed on the object being
157
 *     aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
158
 *   - 'source': A string of the source path for the alias (e.g. 'node/1').
159
 *   - 'data': An array of keyed objects to pass to token_replace().
160
 *   - 'type': The sub-type or bundle of the object being aliased.
161
 *   - 'language': A string of the language code for the alias (e.g. 'en').
162
 *     This can be altered by reference.
163
 */
164
function hook_pathauto_pattern_alter(&$pattern, array $context) {
165
  // Switch out any [node:created:*] tokens with [node:updated:*] on update.
166
  if ($context['module'] == 'node' && ($context['op'] == 'update')) {
167
    $pattern = preg_replace('/\[node:created(\:[^]]*)?\]/', '[node:updated$1]', $pattern);
168
  }
169
}
170

    
171
/**
172
 * Alter Pathauto-generated aliases before saving.
173
 *
174
 * @param string $alias
175
 *   The automatic alias after token replacement and strings cleaned.
176
 * @param array $context
177
 *   An associative array of additional options, with the following elements:
178
 *   - 'module': The module or entity type being aliased.
179
 *   - 'op': A string with the operation being performed on the object being
180
 *     aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
181
 *   - 'source': A string of the source path for the alias (e.g. 'node/1').
182
 *     This can be altered by reference.
183
 *   - 'data': An array of keyed objects to pass to token_replace().
184
 *   - 'type': The sub-type or bundle of the object being aliased.
185
 *   - 'language': A string of the language code for the alias (e.g. 'en').
186
 *     This can be altered by reference.
187
 *   - 'pattern': A string of the pattern used for aliasing the object.
188
 */
189
function hook_pathauto_alias_alter(&$alias, array &$context) {
190
  // Add a suffix so that all aliases get saved as 'content/my-title.html'
191
  $alias .= '.html';
192

    
193
  // Force all aliases to be saved as language neutral.
194
  $context['language'] = LANGUAGE_NONE;
195
}
196

    
197
/**
198
 * Alter the list of punctuation characters for Pathauto control.
199
 *
200
 * @param $punctuation
201
 *   An array of punctuation to be controlled by Pathauto during replacement
202
 *   keyed by punctuation name. Each punctuation record should be an array
203
 *   with the following key/value pairs:
204
 *   - value: The raw value of the punctuation mark.
205
 *   - name: The human-readable name of the punctuation mark. This must be
206
 *     translated using t() already.
207
 */
208
function hook_pathauto_punctuation_chars_alter(array &$punctuation) {
209
  // Add the trademark symbol.
210
  $punctuation['trademark'] = array('value' => '', 'name' => t('Trademark symbol'));
211

    
212
  // Remove the dollar sign.
213
  unset($punctuation['dollar']);
214
}