| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
require(DRUPAL_ROOT . '/modules/field/field.crud.inc');
|
| 14 |
require(DRUPAL_ROOT . '/modules/field/field.info.inc');
|
| 15 |
require(DRUPAL_ROOT . '/modules/field/field.attach.inc');
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
define('FIELD_CARDINALITY_UNLIMITED', -1);
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
define('FIELD_BEHAVIOR_NONE', 0x0001);
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
define('FIELD_BEHAVIOR_DEFAULT', 0x0002);
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
define('FIELD_BEHAVIOR_CUSTOM', 0x0004);
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
define('FIELD_LOAD_CURRENT', 'FIELD_LOAD_CURRENT');
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
define('FIELD_LOAD_REVISION', 'FIELD_LOAD_REVISION');
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
define('FIELD_QUERY_NO_LIMIT', 'FIELD_QUERY_NO_LIMIT');
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
define('FIELD_QUERY_COMPLETE', 'FIELD_QUERY_COMPLETE');
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
class FieldException extends Exception {}
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
function field_flush_caches() {
|
| 128 |
return array('cache_field');
|
| 129 |
}
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
function field_help($path, $arg) {
|
| 135 |
|
| 136 |
case 'admin/help#field':
|
| 137 |
$output = '<p>' . t('The Field API allows custom data fields to be attached to Drupal objects and takes care of storing, loading, editing, and rendering field data. Any object type (node, user, etc.) can use the Field API to make itself "fieldable" and thus allow fields to be attached to it.') . '</p>';
|
| 138 |
$output .= '<p>' . t('The Field API provides no user interface on its own. Use the Content Construction Kit (CCK) contrib module to manage custom fields via a web browser.') . '</p>';
|
| 139 |
return $output;
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
function field_init() {
|
| 147 |
drupal_add_css(drupal_get_path('module', 'field') . '/theme/field.css');
|
| 148 |
}
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
function field_menu() {
|
| 154 |
$items = array();
|
| 155 |
|
| 156 |
|
| 157 |
$items['field/js_add_more'] = array(
|
| 158 |
'page callback' => 'field_add_more_js',
|
| 159 |
'access arguments' => array('access content'),
|
| 160 |
'type' => MENU_CALLBACK,
|
| 161 |
|
| 162 |
|
| 163 |
return $items;
|
| 164 |
}
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
function field_theme() {
|
| 170 |
$path = drupal_get_path('module', 'field') . '/theme';
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
'template' => 'field',
|
| 175 |
'arguments' => array('element' => NULL),
|
| 176 |
'path' => $path,
|
| 177 |
),
|
| 178 |
|
| 179 |
'arguments' => array('element' => NULL),
|
| 180 |
),
|
| 181 |
);
|
| 182 |
}
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
function field_cron() {
|
| 190 |
$limit = variable_get('field_purge_batch_size', 10);
|
| 191 |
field_purge_batch($limit);
|
| 192 |
}
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
function field_modules_installed($modules) {
|
| 198 |
field_cache_clear();
|
| 199 |
}
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
function field_modules_uninstalled($modules) {
|
| 205 |
module_load_include('inc', 'field', 'field.crud');
|
| 206 |
foreach ($modules as $module) {
|
| 207 |
|
| 208 |
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
function field_modules_enabled($modules) {
|
| 216 |
foreach ($modules as $module) {
|
| 217 |
field_associate_fields($module);
|
| 218 |
}
|
| 219 |
field_cache_clear();
|
| 220 |
}
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
function field_modules_disabled($modules) {
|
| 226 |
foreach ($modules as $module) {
|
| 227 |
db_update('field_config')
|
| 228 |
->fields(array('active' => 0))
|
| 229 |
->condition('module', $module)
|
| 230 |
->execute();
|
| 231 |
db_update('field_config_instance')
|
| 232 |
->fields(array('widget_active' => 0))
|
| 233 |
->condition('widget_module', $module)
|
| 234 |
->execute();
|
| 235 |
field_cache_clear(TRUE);
|
| 236 |
}
|
| 237 |
}
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
function field_associate_fields($module) {
|
| 246 |
$module_fields = module_invoke($module, 'field_info');
|
| 247 |
if ($module_fields) {
|
| 248 |
foreach ($module_fields as $name => $field_info) {
|
| 249 |
watchdog('field', 'Updating field type %type with module %module.', array('%type' => $name, '%module' => $module));
|
| 250 |
db_update('field_config')
|
| 251 |
->fields(array('module' => $module, 'active' => 1))
|
| 252 |
->condition('type', $name)
|
| 253 |
->execute();
|
| 254 |
}
|
| 255 |
}
|
| 256 |
$module_widgets = module_invoke($module, 'widget_info');
|
| 257 |
if ($module_widgets) {
|
| 258 |
foreach ($module_widgets as $name => $widget_info) {
|
| 259 |
watchdog('field', 'Updating widget type %type with module %module.', array('%type' => $name, '%module' => $module));
|
| 260 |
db_update('field_config_instance')
|
| 261 |
->fields(array('widget_module' => $module, 'widget_active' => 1))
|
| 262 |
->condition('widget_type', $name)
|
| 263 |
->execute();
|
| 264 |
}
|
| 265 |
}
|
| 266 |
}
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
function field_set_empty($field, $items) {
|
| 282 |
|
| 283 |
$filtered = array();
|
| 284 |
$function = $field['module'] . '_field_is_empty';
|
| 285 |
foreach ((array) $items as $delta => $item) {
|
| 286 |
if (!$function($item, $field)) {
|
| 287 |
$filtered[] = $item;
|
| 288 |
}
|
| 289 |
}
|
| 290 |
return $filtered;
|
| 291 |
}
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
function _field_sort_items($field, $items) {
|
| 298 |
if (($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) && isset($items[0]['_weight'])) {
|
| 299 |
usort($items, '_field_sort_items_helper');
|
| 300 |
foreach ($items as $delta => $item) {
|
| 301 |
if (is_array($items[$delta])) {
|
| 302 |
unset($items[$delta]['_weight']);
|
| 303 |
}
|
| 304 |
}
|
| 305 |
}
|
| 306 |
return $items;
|
| 307 |
}
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
function _field_sort_items_helper($a, $b) {
|
| 314 |
$a_weight = (is_array($a) && isset($a['_weight'])) ? $a['_weight'] : 0;
|
| 315 |
$b_weight = (is_array($b) && isset($b['_weight'])) ? $b['_weight'] : 0;
|
| 316 |
if ($a_weight == $b_weight) {
|
| 317 |
return 0;
|
| 318 |
}
|
| 319 |
return ($a_weight < $b_weight) ? -1 : 1;
|
| 320 |
}
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
function _field_sort_items_value_helper($a, $b) {
|
| 326 |
$a_weight = (is_array($a) && isset($a['_weight']['#value'])) ? $a['_weight']['#value'] : 0;
|
| 327 |
$b_weight = (is_array($b) && isset($b['_weight']['#value'])) ? $b['_weight']['#value'] : 0;
|
| 328 |
if ($a_weight == $b_weight) {
|
| 329 |
return 0;
|
| 330 |
}
|
| 331 |
return ($a_weight < $b_weight) ? -1 : 1;
|
| 332 |
}
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
function field_build_modes($obj_type) {
|
| 338 |
static $info;
|
| 339 |
|
| 340 |
if (!isset($info[$obj_type])) {
|
| 341 |
$info[$obj_type] = module_invoke_all('field_build_modes', $obj_type);
|
| 342 |
}
|
| 343 |
return $info[$obj_type];
|
| 344 |
}
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
function field_cache_clear($rebuild_schema = FALSE) {
|
| 351 |
cache_clear_all('*', 'cache_field', TRUE);
|
| 352 |
|
| 353 |
module_load_include('inc', 'field', 'field.info');
|
| 354 |
_field_info_cache_clear();
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
if ($rebuild_schema) {
|
| 360 |
$schema = drupal_get_schema(NULL, TRUE);
|
| 361 |
}
|
| 362 |
}
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
function field_filter_xss($string) {
|
| 372 |
return filter_xss($string, _field_filter_xss_allowed_tags());
|
| 373 |
}
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
function _field_filter_xss_allowed_tags() {
|
| 379 |
return array('a', 'b', 'big', 'code', 'del', 'em', 'i', 'ins', 'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img');
|
| 380 |
}
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
function _field_filter_xss_display_allowed_tags() {
|
| 386 |
return '<' . implode('> <', _field_filter_xss_allowed_tags()) . '>';
|
| 387 |
}
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
function field_format($obj_type, $object, $field, $item, $formatter_type = NULL, $formatter_settings = array()) {
|
| 419 |
if (!is_array($field)) {
|
| 420 |
$field = field_info_field($field);
|
| 421 |
}
|
| 422 |
|
| 423 |
if (field_access('view', $field)) {
|
| 424 |
$field_type = field_info_field_types($field['type']);
|
| 425 |
|
| 426 |
|
| 427 |
list(, , $bundle) = field_attach_extract_ids($obj_type, $object);
|
| 428 |
$instance = field_info_instance($field['field_name'], $bundle);
|
| 429 |
|
| 430 |
|
| 431 |
'type' => $formatter_type ? $formatter_type : $field_type['default_formatter'],
|
| 432 |
'settings' => $formatter_settings,
|
| 433 |
);
|
| 434 |
$display['settings'] += field_info_formatter_settings($display['type']);
|
| 435 |
|
| 436 |
if ($display['type'] !== 'hidden') {
|
| 437 |
$theme = $formatter['module'] . '_formatter_' . $display['type'];
|
| 438 |
|
| 439 |
|
| 440 |
'#theme' => $theme,
|
| 441 |
'#field_name' => $field['field_name'],
|
| 442 |
'#bundle' => $bundle,
|
| 443 |
'#formatter' => $display['type'],
|
| 444 |
'#settings' => $display['settings'],
|
| 445 |
'#object' => $object,
|
| 446 |
'#object_type' => $obj_type,
|
| 447 |
'#delta' => isset($item['#delta']) ? $item['#delta'] : NULL,
|
| 448 |
);
|
| 449 |
|
| 450 |
if (field_behaviors_formatter('multiple values', $display) == FIELD_BEHAVIOR_DEFAULT) {
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
$items = array($item);
|
| 455 |
$function = $field['module'] . '_field_sanitize';
|
| 456 |
if (function_exists($function)) {
|
| 457 |
$function($obj_type, $object, $field, $instance, $items);
|
| 458 |
}
|
| 459 |
|
| 460 |
$element['#item'] = $items[0];
|
| 461 |
}
|
| 462 |
|
| 463 |
|
| 464 |
$items = $item;
|
| 465 |
$function = $field['module'] . '_field_sanitize';
|
| 466 |
if (function_exists($function)) {
|
| 467 |
$function($obj_type, $object, $field, $instance, $items);
|
| 468 |
}
|
| 469 |
|
| 470 |
foreach ($items as $delta => $item) {
|
| 471 |
$element[$delta] = array(
|
| 472 |
'#item' => $item,
|
| 473 |
'#weight' => $delta,
|
| 474 |
|
| 475 |
}
|
| 476 |
|
| 477 |
|
| 478 |
return theme($theme, $element);
|
| 479 |
}
|
| 480 |
}
|
| 481 |
}
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
function field_view_field($obj_type, $object, $field, $instance, $build_mode = 'full') {
|
| 507 |
$output = '';
|
| 508 |
if (isset($object->$field['field_name'])) {
|
| 509 |
$items = $object->$field['field_name'];
|
| 510 |
|
| 511 |
|
| 512 |
$function = $field['module'] . '_field_sanitize';
|
| 513 |
if (drupal_function_exists($function)) {
|
| 514 |
$function($obj_type, $object, $field, $instance, $items);
|
| 515 |
$object->$field['field_name'] = $items;
|
| 516 |
}
|
| 517 |
|
| 518 |
$view = field_default_view($obj_type, $object, $field, $instance, $items, $build_mode);
|
| 519 |
|
| 520 |
|
| 521 |
$output = $view[$field['field_name']];
|
| 522 |
}
|
| 523 |
return $output;
|
| 524 |
}
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
function field_access($op, $field, $account = NULL) {
|
| 542 |
global $user;
|
| 543 |
|
| 544 |
if (is_null($account)) {
|
| 545 |
$account = $user;
|
| 546 |
}
|
| 547 |
|
| 548 |
$field_access = module_invoke_all('field_access', $op, $field, $account);
|
| 549 |
foreach ($field_access as $value) {
|
| 550 |
if ($value === FALSE) {
|
| 551 |
return FALSE;
|
| 552 |
}
|
| 553 |
}
|
| 554 |
return TRUE;
|
| 555 |
}
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
function template_preprocess_field(&$variables) {
|
| 570 |
$element = $variables['element'];
|
| 571 |
list(, , $bundle) = field_attach_extract_ids($element['#object_type'], $element['#object']);
|
| 572 |
$instance = field_info_instance($element['#field_name'], $bundle);
|
| 573 |
$field = field_info_field($element['#field_name']);
|
| 574 |
|
| 575 |
$variables['object'] = $element['#object'];
|
| 576 |
$variables['field'] = $field;
|
| 577 |
$variables['instance'] = $instance;
|
| 578 |
$variables['items'] = array();
|
| 579 |
|
| 580 |
if ($element['#single']) {
|
| 581 |
|
| 582 |
foreach (element_children($element['items']) as $delta) {
|
| 583 |
$variables['items'][$delta] = $element['items'][$delta]['#item'];
|
| 584 |
$variables['items'][$delta]['view'] = drupal_render_children($element['items'], array($delta));
|
| 585 |
}
|
| 586 |
}
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
foreach (element_children($element['items']) as $delta) {
|
| 593 |
$variables['items'][$delta] = $element['items'][$delta]['#item'];
|
| 594 |
}
|
| 595 |
$variables['items'][0]['view'] = drupal_render_children($element, array('items'));
|
| 596 |
|
| 597 |
|
| 598 |
$variables['build_mode'] = $element['#build_mode'];
|
| 599 |
$variables['page'] = (bool)menu_get_object();
|
| 600 |
|
| 601 |
$field_empty = TRUE;
|
| 602 |
|
| 603 |
foreach ($variables['items'] as $delta => $item) {
|
| 604 |
if (!isset($item['view']) || (empty($item['view']) && (string)$item['view'] !== '0')) {
|
| 605 |
$variables['items'][$delta]['empty'] = TRUE;
|
| 606 |
}
|
| 607 |
|
| 608 |
$field_empty = FALSE;
|
| 609 |
$variables['items'][$delta]['empty'] = FALSE;
|
| 610 |
|
| 611 |
}
|
| 612 |
|
| 613 |
|
| 614 |
'field_type' => $field['type'],
|
| 615 |
'field_name' => $field['field_name'],
|
| 616 |
'field_type_css' => strtr($field['type'], '_', '-'),
|
| 617 |
'field_name_css' => strtr($field['field_name'], '_', '-'),
|
| 618 |
'label' => check_plain(t($instance['label'])),
|
| 619 |
'label_display' => $element['#label_display'],
|
| 620 |
'field_empty' => $field_empty,
|
| 621 |
|
| 622 |
'field',
|
| 623 |
'field-' . $element['#field_name'],
|
| 624 |
'field-' . $bundle,
|
| 625 |
'field-' . $element['#field_name'] . '-' . $bundle,
|
| 626 |
),
|
| 627 |
);
|
| 628 |
$variables = array_merge($variables, $additions);
|
| 629 |
}
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
*/ |