| 1 |
<?php
|
| 2 |
// $Id: rolereference.module,v 1.2.4.4 2009/02/19 14:34:08 cyu Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Defines a field type for referencing a role. Based almost entirely on nodereference and userreference modules.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_menu().
|
| 11 |
*/
|
| 12 |
function rolereference_menu() {
|
| 13 |
$items = array();
|
| 14 |
$items['rolereference/autocomplete'] = array(
|
| 15 |
'title' => 'Rolereference autocomplete',
|
| 16 |
'page callback' => 'rolereference_autocomplete',
|
| 17 |
'access arguments' => array('access content'),
|
| 18 |
'type' => MENU_CALLBACK
|
| 19 |
);
|
| 20 |
return $items;
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_views_api.
|
| 25 |
*/
|
| 26 |
function rolereference_views_api() {
|
| 27 |
return array(
|
| 28 |
'api' => 2,
|
| 29 |
'path' => drupal_get_path('module', 'rolereference') .'/views',
|
| 30 |
);
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_theme().
|
| 35 |
*/
|
| 36 |
function rolereference_theme() {
|
| 37 |
return array(
|
| 38 |
'rolereference_select' => array(
|
| 39 |
'arguments' => array('element' => NULL),
|
| 40 |
),
|
| 41 |
'rolereference_buttons' => array(
|
| 42 |
'arguments' => array('element' => NULL),
|
| 43 |
),
|
| 44 |
'rolereference_autocomplete' => array(
|
| 45 |
'arguments' => array('element' => NULL),
|
| 46 |
),
|
| 47 |
'rolereference_formatter_default' => array(
|
| 48 |
'arguments' => array('element'),
|
| 49 |
),
|
| 50 |
);
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Implementation of hook_field_info().
|
| 55 |
*/
|
| 56 |
function rolereference_field_info() {
|
| 57 |
return array(
|
| 58 |
'rolereference' => array(
|
| 59 |
'label' => t('Role reference'),
|
| 60 |
'description' => t('Store the ID of a role as an integer value.'),
|
| 61 |
),
|
| 62 |
);
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Implementation of hook_field_settings().
|
| 67 |
*/
|
| 68 |
function rolereference_field_settings($op, $field) {
|
| 69 |
switch ($op) {
|
| 70 |
case 'form':
|
| 71 |
$form = array();
|
| 72 |
$form['referenceable_types'] = array(
|
| 73 |
'#type' => 'checkboxes',
|
| 74 |
'#title' => t('Roles that can be referenced'),
|
| 75 |
'#description' => t('Leaving all boxes unchecked will make all roles referenceable.'),
|
| 76 |
'#multiple' => TRUE,
|
| 77 |
'#default_value' => is_array($field['referenceable_types']) ? $field['referenceable_types'] : array(),
|
| 78 |
'#options' => array_map('check_plain', user_roles(TRUE)),
|
| 79 |
);
|
| 80 |
if (module_exists('views')) {
|
| 81 |
$views = array('--' => '--');
|
| 82 |
$all_views = views_get_all_views();
|
| 83 |
foreach ($all_views as $view) {
|
| 84 |
// Only 'users' views that have fields will work for our purpose.
|
| 85 |
if ($view->base_table == 'role' && !empty($view->display['default']->display_options['fields'])) {
|
| 86 |
if ($view->type == 'Default') {
|
| 87 |
$views[t('Default Views')][$view->name] = $view->name;
|
| 88 |
}
|
| 89 |
else {
|
| 90 |
$views[t('Existing Views')][$view->name] = $view->name;
|
| 91 |
}
|
| 92 |
}
|
| 93 |
}
|
| 94 |
|
| 95 |
$form['advanced'] = array(
|
| 96 |
'#type' => 'fieldset',
|
| 97 |
'#title' => t('Advanced - Roles that can be referenced (View)'),
|
| 98 |
'#collapsible' => TRUE,
|
| 99 |
'#collapsed' => !isset($field['advanced_view']) || $field['advanced_view'] == '--',
|
| 100 |
);
|
| 101 |
if (count($views) > 1) {
|
| 102 |
$form['advanced']['advanced_view'] = array(
|
| 103 |
'#type' => 'select',
|
| 104 |
'#title' => t('View used to select the roles'),
|
| 105 |
'#options' => $views,
|
| 106 |
'#default_value' => isset($field['advanced_view']) ? $field['advanced_view'] : '--',
|
| 107 |
'#description' => t('<p>Choose the "Views module" view that selects the roles that can be referenced.<br />Note:</p>') .
|
| 108 |
t('<ul><li>Only views that have fields will work for this purpose.</li><li>This will discard the "Referenceable Roles" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate users on user creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate users will be displayed.</li></ul>'),
|
| 109 |
);
|
| 110 |
$form['advanced']['advanced_view_args'] = array(
|
| 111 |
'#type' => 'textfield',
|
| 112 |
'#title' => t('View arguments'),
|
| 113 |
'#default_value' => isset($field['advanced_view_args']) ? $field['advanced_view_args'] : '',
|
| 114 |
'#required' => FALSE,
|
| 115 |
'#description' => t('Provide a comma separated list of arguments to pass to the view.'),
|
| 116 |
);
|
| 117 |
}
|
| 118 |
else {
|
| 119 |
$form['advanced']['no_view_help'] = array(
|
| 120 |
'#value' => t('<p>The list of roles that can be referenced can be based on a "Views module" view but no appropriate views were found. <br />Note:</p>') .
|
| 121 |
t('<ul><li>Only views that have fields will work for this purpose.</li><li>This will discard the "Referenceable Roles" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate users on user creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate users will be displayed.</li></ul>'),
|
| 122 |
);
|
| 123 |
}
|
| 124 |
}
|
| 125 |
return $form;
|
| 126 |
|
| 127 |
case 'save':
|
| 128 |
$settings = array('referenceable_types');
|
| 129 |
if (module_exists('views')) {
|
| 130 |
$settings[] = 'advanced_view';
|
| 131 |
$settings[] = 'advanced_view_args';
|
| 132 |
}
|
| 133 |
return $settings;
|
| 134 |
|
| 135 |
case 'database columns':
|
| 136 |
$columns = array(
|
| 137 |
'rid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE),
|
| 138 |
);
|
| 139 |
return $columns;
|
| 140 |
|
| 141 |
case 'views data':
|
| 142 |
$data = content_views_field_views_data($field);
|
| 143 |
$db_info = content_database_info($field);
|
| 144 |
$table_alias = content_views_tablename($field);
|
| 145 |
// Filter : swap the handler to the 'in' operator.
|
| 146 |
$data[$table_alias][$field['field_name'] .'_rid']['filter']['handler'] = 'content_handler_filter_many_to_one';
|
| 147 |
// Argument: get the role name for summaries.
|
| 148 |
// We need to join a new instance of the role table.
|
| 149 |
$data["role_$table_alias"]['table']['join']['node'] = array(
|
| 150 |
'table' => 'role',
|
| 151 |
'field' => 'rid',
|
| 152 |
'left_table' => $table_alias,
|
| 153 |
'left_field' => $field['field_name'] .'_rid',
|
| 154 |
);
|
| 155 |
$data[$table_alias][$field['field_name'] .'_rid']['argument']['name table'] = "role_$table_alias";
|
| 156 |
$data[$table_alias][$field['field_name'] .'_rid']['argument']['name field'] = 'name';
|
| 157 |
// Relationship: Add a relationship for related role.
|
| 158 |
$data[$table_alias][$field['field_name'] .'_rid']['relationship'] = array(
|
| 159 |
'base' => 'role',
|
| 160 |
'field' => $db_info['columns']['rid']['column'],
|
| 161 |
'handler' => 'content_handler_relationship',
|
| 162 |
'label' => t($field['widget']['label']),
|
| 163 |
'content_field_name' => $field['field_name'],
|
| 164 |
);
|
| 165 |
|
| 166 |
$data["role_$table_alias"][$field['field_name'] .'_name'] = $data["role_$table_alias"][$field['field_name'] .'_rid'];
|
| 167 |
$data["role_$table_alias"][$field['field_name'] .'_name'] = array(
|
| 168 |
'group' => 'Content',
|
| 169 |
'title' => $data[$table_alias][$field['field_name'] . '_rid']['title'],
|
| 170 |
'field' => array(
|
| 171 |
'table' => 'role',
|
| 172 |
'field' => 'name',
|
| 173 |
'additional fields' => array('name' => 'name')
|
| 174 |
)
|
| 175 |
);
|
| 176 |
$data["role_$table_alias"][$field['field_name'] .'_name']['filter']['handler'] = 'content_handler_filter_many_to_one';
|
| 177 |
$data["role_$table_alias"][$field['field_name'] .'_name']['relationship'] = array(
|
| 178 |
'base' => 'role',
|
| 179 |
'field' => $db_info['columns']['name']['column'],
|
| 180 |
'handler' => 'content_handler_relationship',
|
| 181 |
'label' => t($field['widget']['label']),
|
| 182 |
'content_field_name' => $field['field_name'],
|
| 183 |
);
|
| 184 |
return $data;
|
| 185 |
}
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Implementation of hook_content_is_empty().
|
| 190 |
*/
|
| 191 |
function rolereference_content_is_empty($item, $field) {
|
| 192 |
if (empty($item['rid'])) {
|
| 193 |
return TRUE;
|
| 194 |
}
|
| 195 |
return FALSE;
|
| 196 |
}
|
| 197 |
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Implementation of hook_field_formatter_info().
|
| 201 |
*/
|
| 202 |
function rolereference_field_formatter_info() {
|
| 203 |
return array(
|
| 204 |
'default' => array(
|
| 205 |
'label' => t('Title (no link)'),
|
| 206 |
'field types' => array('rolereference'),
|
| 207 |
'multiple values' => CONTENT_HANDLE_CORE,
|
| 208 |
),
|
| 209 |
);
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Theme function for 'default' rolereference field formatter.
|
| 214 |
*/
|
| 215 |
function theme_rolereference_formatter_default($element) {
|
| 216 |
$output = '';
|
| 217 |
if (!empty($element['#item']['rid']) && is_numeric($element['#item']['rid']) && ($name = _rolereference_names($element['#item']['rid']))) {
|
| 218 |
$output = check_plain($name);
|
| 219 |
}
|
| 220 |
return $output;
|
| 221 |
}
|
| 222 |
|
| 223 |
/**
|
| 224 |
* Helper function for formatters.
|
| 225 |
*
|
| 226 |
* Store role names collected in the current request.
|
| 227 |
*/
|
| 228 |
function _rolereference_names($rid) {
|
| 229 |
static $names = array();
|
| 230 |
if (!isset($names[$rid])) {
|
| 231 |
$name = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $rid));
|
| 232 |
$names[$rid] = $name ? $name : '';
|
| 233 |
}
|
| 234 |
return $names[$rid];
|
| 235 |
}
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Implementation of hook_widget_info().
|
| 239 |
*
|
| 240 |
* We need custom handling of multiple values for the rolereference_select
|
| 241 |
* widget because we need to combine them into a options list rather
|
| 242 |
* than display multiple elements.
|
| 243 |
*
|
| 244 |
* We will use the content module's default handling for default value.
|
| 245 |
*
|
| 246 |
* Callbacks can be omitted if default handing is used.
|
| 247 |
* They're included here just so this module can be used
|
| 248 |
* as an example for custom modules that might do things
|
| 249 |
* differently.
|
| 250 |
*/
|
| 251 |
function rolereference_widget_info() {
|
| 252 |
return array(
|
| 253 |
'rolereference_select' => array(
|
| 254 |
'label' => t('Select list'),
|
| 255 |
'field types' => array('rolereference'),
|
| 256 |
'multiple values' => CONTENT_HANDLE_MODULE,
|
| 257 |
'callbacks' => array(
|
| 258 |
'default value' => CONTENT_CALLBACK_DEFAULT,
|
| 259 |
),
|
| 260 |
),
|
| 261 |
'rolereference_buttons' => array(
|
| 262 |
'label' => t('Check boxes/radio buttons'),
|
| 263 |
'field types' => array('rolereference'),
|
| 264 |
'multiple values' => CONTENT_HANDLE_MODULE,
|
| 265 |
'callbacks' => array(
|
| 266 |
'default value' => CONTENT_CALLBACK_DEFAULT,
|
| 267 |
),
|
| 268 |
),
|
| 269 |
'rolereference_autocomplete' => array(
|
| 270 |
'label' => t('Autocomplete text field'),
|
| 271 |
'field types' => array('rolereference'),
|
| 272 |
'multiple values' => CONTENT_HANDLE_CORE,
|
| 273 |
'callbacks' => array(
|
| 274 |
'default value' => CONTENT_CALLBACK_DEFAULT,
|
| 275 |
),
|
| 276 |
),
|
| 277 |
);
|
| 278 |
}
|
| 279 |
|
| 280 |
/**
|
| 281 |
* Implementation of FAPI hook_elements().
|
| 282 |
*
|
| 283 |
* Any FAPI callbacks needed for individual widgets can be declared here,
|
| 284 |
* and the element will be passed to those callbacks for processing.
|
| 285 |
*
|
| 286 |
* Drupal will automatically theme the element using a theme with
|
| 287 |
* the same name as the hook_elements key.
|
| 288 |
*
|
| 289 |
* Autocomplete_path is not used by text_widget but other widgets can use it
|
| 290 |
* (see nodereference and userreference and rolereference).
|
| 291 |
*/
|
| 292 |
function rolereference_elements() {
|
| 293 |
return array(
|
| 294 |
'rolereference_select' => array(
|
| 295 |
'#input' => TRUE,
|
| 296 |
'#columns' => array('uid'), '#delta' => 0,
|
| 297 |
'#process' => array('rolereference_select_process'),
|
| 298 |
),
|
| 299 |
'rolereference_buttons' => array(
|
| 300 |
'#input' => TRUE,
|
| 301 |
'#columns' => array('uid'), '#delta' => 0,
|
| 302 |
'#process' => array('rolereference_buttons_process'),
|
| 303 |
),
|
| 304 |
'rolereference_autocomplete' => array(
|
| 305 |
'#input' => TRUE,
|
| 306 |
'#columns' => array('name'), '#delta' => 0,
|
| 307 |
'#process' => array('rolereference_autocomplete_process'),
|
| 308 |
'#autocomplete_path' => FALSE,
|
| 309 |
),
|
| 310 |
);
|
| 311 |
}
|
| 312 |
|
| 313 |
/**
|
| 314 |
* Implementation of hook_widget_settings().
|
| 315 |
*/
|
| 316 |
function rolereference_widget_settings($op, $widget) {
|
| 317 |
switch ($op) {
|
| 318 |
case 'form':
|
| 319 |
$form = array();
|
| 320 |
$match = isset($widget['autocomplete_match']) ? $widget['autocomplete_match'] : 'contains';
|
| 321 |
if ($widget['type'] == 'rolereference_autocomplete') {
|
| 322 |
$form['autocomplete_match'] = array(
|
| 323 |
'#type' => 'select',
|
| 324 |
'#title' => t('Autocomplete matching'),
|
| 325 |
'#default_value' => $match,
|
| 326 |
'#options' => array(
|
| 327 |
'starts_with' => t('Starts with'),
|
| 328 |
'contains' => t('Contains'),
|
| 329 |
),
|
| 330 |
'#description' => t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of roles.'),
|
| 331 |
);
|
| 332 |
}
|
| 333 |
else {
|
| 334 |
$form['autocomplete_match'] = array('#type' => 'hidden', '#value' => $match);
|
| 335 |
}
|
| 336 |
return $form;
|
| 337 |
|
| 338 |
case 'save':
|
| 339 |
return array('autocomplete_match');
|
| 340 |
}
|
| 341 |
}
|
| 342 |
|
| 343 |
/**
|
| 344 |
* Implementation of hook_widget().
|
| 345 |
*
|
| 346 |
* Attach a single form element to the form. It will be built out and
|
| 347 |
* validated in the callback(s) listed in hook_elements. We build it
|
| 348 |
* out in the callbacks rather than here in hook_widget so it can be
|
| 349 |
* plugged into any module that can provide it with valid
|
| 350 |
* $field information.
|
| 351 |
*
|
| 352 |
* Content module will set the weight, field name and delta values
|
| 353 |
* for each form element. This is a change from earlier CCK versions
|
| 354 |
* where the widget managed its own multiple values.
|
| 355 |
*
|
| 356 |
* If there are multiple values for this field, the content module will
|
| 357 |
* call this function as many times as needed.
|
| 358 |
*
|
| 359 |
* @param $form
|
| 360 |
* the entire form array, $form['#node'] holds node information
|
| 361 |
* @param $form_state
|
| 362 |
* the form_state, $form_state['values'][$field['field_name']]
|
| 363 |
* holds the field's form values.
|
| 364 |
* @param $field
|
| 365 |
* the field array
|
| 366 |
* @param $items
|
| 367 |
* array of default values for this field
|
| 368 |
* @param $delta
|
| 369 |
* the order of this item in the array of subelements (0, 1, 2, etc)
|
| 370 |
*
|
| 371 |
* @return
|
| 372 |
* the form item for a single element for this field
|
| 373 |
*/
|
| 374 |
function rolereference_widget(&$form, &$form_state, $field, $items, $delta = 0) {
|
| 375 |
switch ($field['widget']['type']) {
|
| 376 |
case 'rolereference_select':
|
| 377 |
$element = array(
|
| 378 |
'#type' => 'rolereference_select',
|
| 379 |
'#default_value' => $items,
|
| 380 |
);
|
| 381 |
break;
|
| 382 |
|
| 383 |
case 'rolereference_buttons':
|
| 384 |
$element = array(
|
| 385 |
'#type' => 'rolereference_buttons',
|
| 386 |
'#default_value' => $items,
|
| 387 |
);
|
| 388 |
break;
|
| 389 |
|
| 390 |
case 'rolereference_autocomplete':
|
| 391 |
$element = array(
|
| 392 |
'#type' => 'rolereference_autocomplete',
|
| 393 |
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
|
| 394 |
'#value_callback' => 'rolereference_autocomplete_value',
|
| 395 |
);
|
| 396 |
break;
|
| 397 |
}
|
| 398 |
return $element;
|
| 399 |
}
|
| 400 |
|
| 401 |
/**
|
| 402 |
* Value for a rolereference autocomplete element.
|
| 403 |
*
|
| 404 |
* Substitute in the role name for the role rid.
|
| 405 |
*/
|
| 406 |
function rolereference_autocomplete_value($element, $edit = FALSE) {
|
| 407 |
$field_key = $element['#columns'][0];
|
| 408 |
if (!empty($element['#default_value'][$field_key])) {
|
| 409 |
$rid = $element['#default_value'][$field_key];
|
| 410 |
$value = db_result(db_query(db_rewrite_sql('SELECT name FROM {role} WHERE rid = %d'), $rid));
|
| 411 |
$value .= ' [rid:'. $rid .']';
|
| 412 |
return array($field_key => $value);
|
| 413 |
}
|
| 414 |
return array($field_key => NULL);
|
| 415 |
}
|
| 416 |
|
| 417 |
/**
|
| 418 |
* Process an individual element.
|
| 419 |
*
|
| 420 |
* Build the form element. When creating a form using FAPI #process,
|
| 421 |
* note that $element['#value'] is already set.
|
| 422 |
*
|
| 423 |
* The $fields array is in $form['#field_info'][$element['#field_name']].
|
| 424 |
*/
|
| 425 |
function rolereference_select_process($element, $edit, $form_state, $form) {
|
| 426 |
// The rolereference_select widget doesn't need to create its own
|
| 427 |
// element, it can wrap around the optionwidgets_select element.
|
| 428 |
// This will create a new, nested instance of the field.
|
| 429 |
// Add a validation step where the value can be unwrapped.
|
| 430 |
$field_key = $element['#columns'][0];
|
| 431 |
$element[$field_key] = array(
|
| 432 |
'#type' => 'optionwidgets_select',
|
| 433 |
'#default_value' => isset($element['#value']) ? $element['#value'] : '',
|
| 434 |
// The following values were set by the content module and need
|
| 435 |
// to be passed down to the nested element.
|
| 436 |
'#title' => $element['#title'],
|
| 437 |
'#required' => $element['#required'],
|
| 438 |
'#description' => $element['#description'],
|
| 439 |
'#field_name' => $element['#field_name'],
|
| 440 |
'#type_name' => $element['#type_name'],
|
| 441 |
'#delta' => $element['#delta'],
|
| 442 |
'#columns' => $element['#columns'],
|
| 443 |
);
|
| 444 |
if (empty($element[$field_key]['#element_validate'])) {
|
| 445 |
$element[$field_key]['#element_validate'] = array();
|
| 446 |
}
|
| 447 |
array_unshift($element[$field_key]['#element_validate'], 'rolereference_optionwidgets_validate');
|
| 448 |
return $element;
|
| 449 |
}
|
| 450 |
|
| 451 |
/**
|
| 452 |
* Process an individual element.
|
| 453 |
*
|
| 454 |
* Build the form element. When creating a form using FAPI #process,
|
| 455 |
* note that $element['#value'] is already set.
|
| 456 |
*
|
| 457 |
* The $fields array is in $form['#field_info'][$element['#field_name']].
|
| 458 |
*/
|
| 459 |
function rolereference_buttons_process($element, $edit, $form_state, $form) {
|
| 460 |
// The rolereference_select widget doesn't need to create its own
|
| 461 |
// element, it can wrap around the optionwidgets_select element.
|
| 462 |
// This will create a new, nested instance of the field.
|
| 463 |
// Add a validation step where the value can be unwrapped.
|
| 464 |
$field_key = $element['#columns'][0];
|
| 465 |
$element[$field_key] = array(
|
| 466 |
'#type' => 'optionwidgets_buttons',
|
| 467 |
'#default_value' => isset($element['#value']) ? $element['#value'] : '',
|
| 468 |
// The following values were set by the content module and need
|
| 469 |
// to be passed down to the nested element.
|
| 470 |
'#title' => $element['#title'],
|
| 471 |
'#required' => $element['#required'],
|
| 472 |
'#description' => $element['#description'],
|
| 473 |
'#field_name' => $element['#field_name'],
|
| 474 |
'#type_name' => $element['#type_name'],
|
| 475 |
'#delta' => $element['#delta'],
|
| 476 |
'#columns' => $element['#columns'],
|
| 477 |
);
|
| 478 |
if (empty($element[$field_key]['#element_validate'])) {
|
| 479 |
$element[$field_key]['#element_validate'] = array();
|
| 480 |
}
|
| 481 |
array_unshift($element[$field_key]['#element_validate'], 'rolereference_optionwidgets_validate');
|
| 482 |
return $element;
|
| 483 |
}
|
| 484 |
|
| 485 |
/**
|
| 486 |
* Process an individual element.
|
| 487 |
*
|
| 488 |
* Build the form element. When creating a form using FAPI #process,
|
| 489 |
* note that $element['#value'] is already set.
|
| 490 |
*
|
| 491 |
*/
|
| 492 |
function rolereference_autocomplete_process($element, $edit, $form_state, $form) {
|
| 493 |
|
| 494 |
// The rolereference autocomplete widget doesn't need to create its own
|
| 495 |
// element, it can wrap around the text_textfield element and add an autocomplete
|
| 496 |
// path and some extra processing to it.
|
| 497 |
// Add a validation step where the value can be unwrapped.
|
| 498 |
$field_key = $element['#columns'][0];
|
| 499 |
|
| 500 |
$element[$field_key] = array(
|
| 501 |
'#type' => 'text_textfield',
|
| 502 |
'#default_value' => isset($element['#value']) ? $element['#value'] : '',
|
| 503 |
'#autocomplete_path' => 'rolereference/autocomplete/'. $element['#field_name'],
|
| 504 |
// The following values were set by the content module and need
|
| 505 |
// to be passed down to the nested element.
|
| 506 |
'#title' => $element['#title'],
|
| 507 |
'#required' => $element['#required'],
|
| 508 |
'#description' => $element['#description'],
|
| 509 |
'#field_name' => $element['#field_name'],
|
| 510 |
'#type_name' => $element['#type_name'],
|
| 511 |
'#delta' => $element['#delta'],
|
| 512 |
'#columns' => $element['#columns'],
|
| 513 |
);
|
| 514 |
if (empty($element[$field_key]['#element_validate'])) {
|
| 515 |
$element[$field_key]['#element_validate'] = array();
|
| 516 |
}
|
| 517 |
array_unshift($element[$field_key]['#element_validate'], 'rolereference_autocomplete_validate');
|
| 518 |
|
| 519 |
// Used so that hook_field('validate') knows where to flag an error.
|
| 520 |
$element['_error_element'] = array(
|
| 521 |
'#type' => 'value',
|
| 522 |
// Wrapping the element around a text_textfield element creates a
|
| 523 |
// nested element, so the final id will look like 'field-name-0-rid-rid'.
|
| 524 |
'#value' => implode('][', array_merge($element['#parents'], array($field_key, $field_key))),
|
| 525 |
);
|
| 526 |
return $element;
|
| 527 |
}
|
| 528 |
|
| 529 |
/**
|
| 530 |
* Validate a select/buttons element.
|
| 531 |
*
|
| 532 |
* Remove the wrapper layer and set the right element's value.
|
| 533 |
* We don't know exactly where this element is, so we drill down
|
| 534 |
* through the element until we get to our key.
|
| 535 |
*
|
| 536 |
* We use $form_state['values'] instead of $element['#value']
|
| 537 |
* to be sure we have the most accurate value when other modules
|
| 538 |
* like optionwidgets are using #element_validate to alter the value.
|
| 539 |
*/
|
| 540 |
function rolereference_optionwidgets_validate($element, &$form_state) {
|
| 541 |
$field_key = $element['#columns'][0];
|
| 542 |
|
| 543 |
$value = $form_state['values'];
|
| 544 |
$new_parents = array();
|
| 545 |
foreach ($element['#parents'] as $parent) {
|
| 546 |
$value = $value[$parent];
|
| 547 |
// Use === to be sure we get right results if parent is a zero (delta) value.
|
| 548 |
if ($parent === $field_key) {
|
| 549 |
$element['#parents'] = $new_parents;
|
| 550 |
form_set_value($element, $value, $form_state);
|
| 551 |
break;
|
| 552 |
}
|
| 553 |
$new_parents[] = $parent;
|
| 554 |
}
|
| 555 |
}
|
| 556 |
|
| 557 |
/**
|
| 558 |
* Validate an autocomplete element.
|
| 559 |
*
|
| 560 |
* Remove the wrapper layer and set the right element's value.
|
| 561 |
* This will move the nested value at 'field-name-0-rid-rid'
|
| 562 |
* back to its original location, 'field-name-0-rid'.
|
| 563 |
*/
|
| 564 |
function rolereference_autocomplete_validate($element, &$form_state) {
|
| 565 |
$field_name = $element['#field_name'];
|
| 566 |
$type_name = $element['#type_name'];
|
| 567 |
$field = content_fields($field_name, $type_name);
|
| 568 |
$field_key = $element['#columns'][0];
|
| 569 |
$delta = $element['#delta'];
|
| 570 |
$value = $element['#value'][$field_key];
|
| 571 |
$rid = NULL;
|
| 572 |
if (!empty($value)) {
|
| 573 |
preg_match('/^(?:\s*|(.*) )?\[\s*rid\s*:\s*(\d+)\s*\]$/', $value, $matches);
|
| 574 |
if (!empty($matches)) {
|
| 575 |
// Explicit [rid:n].
|
| 576 |
list(, $name, $rid) = $matches;
|
| 577 |
if (!empty($name) && ($r = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $rid))) && $name != $r) {
|
| 578 |
form_error($element[$field_key], t('%name: name mismatch. Please check your selection.', array('%name' => t($field['widget']['label']))));
|
| 579 |
}
|
| 580 |
}
|
| 581 |
else {
|
| 582 |
// No explicit rid.
|
| 583 |
$reference = _rolereference_potential_references($field);
|
| 584 |
if (empty($reference)) {
|
| 585 |
form_error($element[$field_key], t('%name: found no valid role with that name.', array('%name' => t($field['widget']['label']))));
|
| 586 |
}
|
| 587 |
else {
|
| 588 |
// TODO:
|
| 589 |
// the best thing would be to present the user with an additional form,
|
| 590 |
// allowing the user to choose between valid candidates with the same title
|
| 591 |
// ATM, we pick the first matching candidate...
|
| 592 |
$rid = key($reference);
|
| 593 |
}
|
| 594 |
}
|
| 595 |
}
|
| 596 |
form_set_value($element, $rid, $form_state);
|
| 597 |
}
|
| 598 |
|
| 599 |
/**
|
| 600 |
* Implementation of hook_allowed_values().
|
| 601 |
*/
|
| 602 |
function rolereference_allowed_values($field) {
|
| 603 |
$references = _rolereference_potential_references($field);
|
| 604 |
|
| 605 |
$options = array();
|
| 606 |
foreach ($references as $key => $value) {
|
| 607 |
// Views theming runs check_plain (htmlentities) on the values.
|
| 608 |
// We reverse that with html_entity_decode.
|
| 609 |
$options[$key] = html_entity_decode(strip_tags($value['rendered']), ENT_QUOTES);
|
| 610 |
}
|
| 611 |
return $options;
|
| 612 |
}
|
| 613 |
|
| 614 |
/**
|
| 615 |
* Fetch an array of all candidate referenced roles.
|
| 616 |
*
|
| 617 |
* This info is used in various places (allowed values, autocomplete results,
|
| 618 |
* input validation...). Some of them only need the rids, others rid + names,
|
| 619 |
* others yet rid + names + rendered row (for display in widgets).
|
| 620 |
* The array we return contains all the potentially needed information, and lets
|
| 621 |
* consumers use the parts they actually need.
|
| 622 |
*
|
| 623 |
* @param $field
|
| 624 |
* The field description.
|
| 625 |
* @param $string
|
| 626 |
* Optional string to filter titles on (used by autocomplete).
|
| 627 |
* @param $match
|
| 628 |
* Operator to match filtered name against, can be any of:
|
| 629 |
* 'contains', 'equals', 'starts_with'
|
| 630 |
* @param $ids
|
| 631 |
* Optional role ids to lookup (the $string and $match arguments will be
|
| 632 |
* ignored).
|
| 633 |
* @param $limit
|
| 634 |
* If non-zero, limit the size of the result set.
|
| 635 |
*
|
| 636 |
* @return
|
| 637 |
* An array of valid roles in the form:
|
| 638 |
* array(
|
| 639 |
* rid => array(
|
| 640 |
* 'name' => The role name,
|
| 641 |
* 'rendered' => The text to display in widgets (can be HTML)
|
| 642 |
* ),
|
| 643 |
* ...
|
| 644 |
* )
|
| 645 |
*/
|
| 646 |
function _rolereference_potential_references($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
|
| 647 |
static $results = array();
|
| 648 |
|
| 649 |
// Create unique id for static cache.
|
| 650 |
$cid = $field['field_name'] .':'. $match .':'. ($string !== '' ? $string : implode('-', $ids)) .':'. $limit;
|
| 651 |
if (!isset($results[$cid])) {
|
| 652 |
$references = FALSE;
|
| 653 |
if (module_exists('views') && !empty($field['advanced_view']) && $field['advanced_view'] != '--') {
|
| 654 |
$references = _rolereference_potential_references_views($field, $string, $match, $ids, $limit);
|
| 655 |
}
|
| 656 |
// If the view doesn't exist, we got FALSE, and fallback to the regular 'standard mode'.
|
| 657 |
|
| 658 |
if ($references === FALSE) {
|
| 659 |
$references = _rolereference_potential_references_standard($field, $string, $match, $ids, $limit);
|
| 660 |
}
|
| 661 |
|
| 662 |
// Store the results.
|
| 663 |
$results[$cid] = !empty($references) ? $references : array();
|
| 664 |
}
|
| 665 |
|
| 666 |
return $results[$cid];
|
| 667 |
}
|
| 668 |
|
| 669 |
/**
|
| 670 |
* Helper function for _nodereference_potential_references():
|
| 671 |
* case of Views-defined referenceable nodes.
|
| 672 |
*/
|
| 673 |
function _rolereference_potential_references_views($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
|
| 674 |
$view_name = $field['advanced_view'];
|
| 675 |
|
| 676 |
if ($view = views_get_view($view_name)) {
|
| 677 |
// We add a display, and let it derive from the 'default' display.
|
| 678 |
// TODO: We should let the user pick a display in the fields settings - sort of requires AHAH...
|
| 679 |
$display = $view->add_display('content_references');
|
| 680 |
$view->set_display($display);
|
| 681 |
|
| 682 |
// TODO from merlinofchaos on IRC : arguments using summary view can defeat the style setting.
|
| 683 |
// We might also need to check if there's an argument, and set *its* style_plugin as well.
|
| 684 |
$view->display_handler->set_option('style_plugin', 'content_php_array_autocomplete');
|
| 685 |
$view->display_handler->set_option('row_plugin', 'fields');
|
| 686 |
// Used in content_plugin_style_php_array::render(), to get
|
| 687 |
// the 'field' to be used as title.
|
| 688 |
$view->display_handler->set_option('content_title_field', 'name');
|
| 689 |
|
| 690 |
// Additional options to let content_plugin_display_references::query()
|
| 691 |
// narrow the results.
|
| 692 |
$options = array(
|
| 693 |
'table' => 'role',
|
| 694 |
'field_string' => 'name',
|
| 695 |
'string' => $string,
|
| 696 |
'match' => $match,
|
| 697 |
'field_id' => 'rid',
|
| 698 |
'ids' => $ids,
|
| 699 |
);
|
| 700 |
$view->display_handler->set_option('content_options', $options);
|
| 701 |
|
| 702 |
// TODO : for consistency, a fair amount of what's below
|
| 703 |
// should be moved to content_plugin_display_references
|
| 704 |
|
| 705 |
// Limit result set size.
|
| 706 |
if (isset($limit)) {
|
| 707 |
$view->display_handler->set_option('items_per_page', $limit);
|
| 708 |
}
|
| 709 |
|
| 710 |
// Get arguments for the view.
|
| 711 |
if (!empty($field['advanced_view_args'])) {
|
| 712 |
// TODO: Support Tokens using token.module ?
|
| 713 |
$view_args = array_map('trim', explode(',', $field['advanced_view_args']));
|
| 714 |
}
|
| 715 |
else {
|
| 716 |
$view_args = array();
|
| 717 |
}
|
| 718 |
|
| 719 |
// We do need title field, so add it if not present (unlikely, but...)
|
| 720 |
$fields = $view->get_items('field', $display);
|
| 721 |
if (!isset($fields['title'])) {
|
| 722 |
$view->add_item($display, 'field', 'role', 'name');
|
| 723 |
}
|
| 724 |
|
| 725 |
// If not set, make all fields inline and define a separator.
|
| 726 |
$options = $view->display_handler->get_option('row_options');
|
| 727 |
if (empty($options['inline'])) {
|
| 728 |
$options['inline'] = drupal_map_assoc(array_keys($view->get_items('field', $display)));
|
| 729 |
}
|
| 730 |
if (empty($options['separator'])) {
|
| 731 |
$options['separator'] = '-';
|
| 732 |
}
|
| 733 |
$view->display_handler->set_option('row_options', $options);
|
| 734 |
|
| 735 |
// Make sure the query is not cached
|
| 736 |
$view->is_cacheable = FALSE;
|
| 737 |
|
| 738 |
// Get the results.
|
| 739 |
$result = $view->execute_display($display, $view_args);
|
| 740 |
}
|
| 741 |
else {
|
| 742 |
$result = FALSE;
|
| 743 |
}
|
| 744 |
|
| 745 |
return $result;
|
| 746 |
}
|
| 747 |
|
| 748 |
/**
|
| 749 |
* Helper function for _nodereference_potential_references():
|
| 750 |
* referenceable nodes defined by content types.
|
| 751 |
*/
|
| 752 |
function _rolereference_potential_references_standard($field, $string = '', $match = 'contains', $ids = array(), $limit = NULL) {
|
| 753 |
$related_types = array();
|
| 754 |
$where = array();
|
| 755 |
$args = array();
|
| 756 |
if (is_array($field['referenceable_types'])) {
|
| 757 |
foreach (array_filter($field['referenceable_types']) as $related_type) {
|
| 758 |
$related_types[] = "r.rid = %d";
|
| 759 |
$args[] = $related_type;
|
| 760 |
}
|
| 761 |
}
|
| 762 |
// Default to all roles if no roles selected.
|
| 763 |
if (!count($related_types)) {
|
| 764 |
$roles = db_query('SELECT rid, name FROM {role} ORDER BY name');
|
| 765 |
while ($role = db_fetch_object($roles)) {
|
| 766 |
$related_types[] = "r.rid = %d";
|
| 767 |
$args[] = $role->rid;
|
| 768 |
}
|
| 769 |
}
|
| 770 |
$where[] = implode(' OR ', $related_types);
|
| 771 |
|
| 772 |
if ($string !== '') {
|
| 773 |
$match_operators = array(
|
| 774 |
'contains' => "LIKE '%%%s%%'",
|
| 775 |
'equals' => "= '%s'",
|
| 776 |
'starts_with' => "LIKE '%s%%'",
|
| 777 |
);
|
| 778 |
$where[] = 'r.name '. (isset($match_operators[$match]) ? $match_operators[$match] : $match_operators['contains']);
|
| 779 |
$args[] = $string;
|
| 780 |
}
|
| 781 |
elseif ($ids) {
|
| 782 |
$where[] = 'r.rid IN (' . db_placeholders($ids) . ')';
|
| 783 |
$args = array_merge($args, $ids);
|
| 784 |
}
|
| 785 |
|
| 786 |
$references = array();
|
| 787 |
$where_clause = $where ? 'WHERE ('. implode(') AND (', $where) .')' : '';
|
| 788 |
$result = db_query('SELECT r.rid, r.name FROM {role} r '." $where_clause ORDER BY r.name ASC", $args);
|
| 789 |
while ($reference = db_fetch_object($result)) {
|
| 790 |
$references[$reference->rid] = array(
|
| 791 |
'title' => $reference->name,
|
| 792 |
'rendered' => check_plain($reference->name),
|
| 793 |
);
|
| 794 |
}
|
| 795 |
return $references;
|
| 796 |
}
|
| 797 |
|
| 798 |
/**
|
| 799 |
* Menu callback; Retrieve a pipe delimited string of autocomplete suggestions for existing roles
|
| 800 |
*/
|
| 801 |
function rolereference_autocomplete($field_name, $string = '') {
|
| 802 |
$fields = content_fields();
|
| 803 |
$field = $fields[$field_name];
|
| 804 |
$match = isset($field['widget']['autocomplete_match']) ? $field['widget']['autocomplete_match'] : 'contains';
|
| 805 |
$matches = array();
|
| 806 |
|
| 807 |
$references = _rolereference_potential_references($field, $string, $match, array(), 10);
|
| 808 |
foreach ($references as $id => $row) {
|
| 809 |
// Add a class wrapper for a few required CSS overrides.
|
| 810 |
$matches[$row['title'] ." [rid:$id]"] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';
|
| 811 |
}
|
| 812 |
drupal_json($matches);
|
| 813 |
}
|
| 814 |
|
| 815 |
/**
|
| 816 |
* FAPI theme for an individual elements.
|
| 817 |
*
|
| 818 |
* The textfield or select is already rendered by the
|
| 819 |
* textfield or select themes and the html output
|
| 820 |
* lives in $element['#children']. Override this theme to
|
| 821 |
* make custom changes to the output.
|
| 822 |
*
|
| 823 |
* $element['#field_name'] contains the field name
|
| 824 |
* $element['#delta] is the position of this element in the group
|
| 825 |
*/
|
| 826 |
function theme_rolereference_select($element) {
|
| 827 |
return $element['#children'];
|
| 828 |
}
|
| 829 |
|
| 830 |
function theme_rolereference_buttons($element) {
|
| 831 |
return $element['#children'];
|
| 832 |
}
|
| 833 |
|
| 834 |
function theme_rolereference_autocomplete($element) {
|
| 835 |
return $element['#children'];
|
| 836 |
}
|
| 837 |
|
| 838 |
/**
|
| 839 |
* Implementation of hook_content_multigroup_allowed_widgets().
|
| 840 |
*/
|
| 841 |
function rolereference_content_multigroup_allowed_widgets() {
|
| 842 |
return array('rolereference_select', 'rolereference_buttons', 'rolereference_autocomplete');
|
| 843 |
}
|
| 844 |
|
| 845 |
/**
|
| 846 |
* Implementation of hook_content_multigroup_no_remove_widgets().
|
| 847 |
*/
|
| 848 |
function rolereference_content_multigroup_no_remove_widgets() {
|
| 849 |
return array('rolereference_select', 'rolereference_buttons', 'rolereference_autocomplete');
|
| 850 |
}
|