| 1 |
<?php
|
| 2 |
// $Id: field.api.php,v 1.46 2009/10/23 22:24:13 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @ingroup field_fieldable_type
|
| 6 |
* @{
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Expose "pseudo-field" components on fieldable objects.
|
| 11 |
*
|
| 12 |
* Field UI's 'Manage fields' page lets users re-order fields, but also
|
| 13 |
* non-field components. For nodes, that would be title, menu settings, or
|
| 14 |
* other elements exposed by contributed modules through hook_form() or
|
| 15 |
* hook_form_alter().
|
| 16 |
*
|
| 17 |
* Fieldable entities or contributed modules that want to have their components
|
| 18 |
* supported should expose them using this hook, and use
|
| 19 |
* field_attach_extra_weight() to retrieve the user-defined weight when
|
| 20 |
* inserting the component.
|
| 21 |
*
|
| 22 |
* @param $bundle
|
| 23 |
* The name of the bundle being considered.
|
| 24 |
* @return
|
| 25 |
* An array of 'pseudo-field' components. The keys are the name of the element
|
| 26 |
* as it appears in the form structure. The values are arrays with the
|
| 27 |
* following key/value pairs:
|
| 28 |
* - label: The human readable name of the component.
|
| 29 |
* - description: A short description of the component contents.
|
| 30 |
* - weight: The default weight of the element.
|
| 31 |
* - view: (optional) The name of the element as it appears in the rendered
|
| 32 |
* structure, if different from the name in the form.
|
| 33 |
*/
|
| 34 |
function hook_field_extra_fields($bundle) {
|
| 35 |
$extra = array();
|
| 36 |
|
| 37 |
if ($type = node_type_get_type($bundle)) {
|
| 38 |
if ($type->has_title) {
|
| 39 |
$extra['title'] = array(
|
| 40 |
'label' => $type->title_label,
|
| 41 |
'description' => t('Node module element.'),
|
| 42 |
'weight' => -5,
|
| 43 |
);
|
| 44 |
}
|
| 45 |
if ($bundle == 'poll' && module_exists('poll')) {
|
| 46 |
$extra['title'] = array(
|
| 47 |
'label' => t('Poll title'),
|
| 48 |
'description' => t('Poll module title.'),
|
| 49 |
'weight' => -5,
|
| 50 |
);
|
| 51 |
$extra['choice_wrapper'] = array(
|
| 52 |
'label' => t('Poll choices'),
|
| 53 |
'description' => t('Poll module choices.'),
|
| 54 |
'weight' => -4,
|
| 55 |
);
|
| 56 |
$extra['settings'] = array(
|
| 57 |
'label' => t('Poll settings'),
|
| 58 |
'description' => t('Poll module settings.'),
|
| 59 |
'weight' => -3,
|
| 60 |
);
|
| 61 |
}
|
| 62 |
}
|
| 63 |
return $extra;
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
* @} End of "ingroup field_fieldable_type"
|
| 68 |
*/
|
| 69 |
|
| 70 |
/**
|
| 71 |
* @defgroup field_types Field Types API
|
| 72 |
* @{
|
| 73 |
* Define field types, widget types, display formatter types, storage types.
|
| 74 |
*
|
| 75 |
* The bulk of the Field Types API are related to field types. A field type
|
| 76 |
* represents a particular type of data (integer, string, date, etc.) that
|
| 77 |
* can be attached to a fieldable object. hook_field_info() defines the basic
|
| 78 |
* properties of a field type, and a variety of other field hooks are called by
|
| 79 |
* the Field Attach API to perform field-type-specific actions.
|
| 80 |
* @see hook_field_info().
|
| 81 |
* @see hook_field_info_alter().
|
| 82 |
* @see hook_field_schema().
|
| 83 |
* @see hook_field_load().
|
| 84 |
* @see hook_field_validate().
|
| 85 |
* @see hook_field_presave().
|
| 86 |
* @see hook_field_insert().
|
| 87 |
* @see hook_field_update().
|
| 88 |
* @see hook_field_delete().
|
| 89 |
* @see hook_field_delete_revision().
|
| 90 |
* @see hook_field_sanitize().
|
| 91 |
* @see hook_field_is_empty().
|
| 92 |
*
|
| 93 |
* The Field Types API also defines two kinds of pluggable handlers: widgets
|
| 94 |
* and formatters, which specify how the field appears in edit forms and in
|
| 95 |
* displayed objects. Widgets and formatters can be implemented by a field-type
|
| 96 |
* module for it's own field types, or by a third-party module to extend the
|
| 97 |
* behavior of existing field types.
|
| 98 |
* @see hook_field_widget_info().
|
| 99 |
* @see hook_field_formatter_info().
|
| 100 |
*
|
| 101 |
* A third kind of pluggable handlers, storage backends, is defined by the
|
| 102 |
* @link field_storage Field Storage API @endlink.
|
| 103 |
*/
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Define Field API field types.
|
| 107 |
*
|
| 108 |
* @return
|
| 109 |
* An array whose keys are field type names and whose values are arrays
|
| 110 |
* describing the field type, with the following key/value pairs:
|
| 111 |
* - label: The human-readable name of the field type.
|
| 112 |
* - description: A short description for the field type.
|
| 113 |
* - settings: An array whose keys are the names of the settings available
|
| 114 |
* for the field type, and whose values are the default values for those
|
| 115 |
* settings.
|
| 116 |
* - instance_settings: An array whose keys are the names of the settings
|
| 117 |
* available for instances of the field type, and whose values are the
|
| 118 |
* default values for those settings.
|
| 119 |
* Instance-level settings can have different values on each field
|
| 120 |
* instance, and thus allow greater flexibility than field-level settings.
|
| 121 |
* It is recommended to put settings at the instance level whenever
|
| 122 |
* possible. Notable exceptions: settings acting on the schema definition,
|
| 123 |
* or settings that Views needs to use across field instances (e.g. list of
|
| 124 |
* allowed values).
|
| 125 |
* - default_widget: The machine name of the default widget to be used by
|
| 126 |
* instances of this field type, when no widget is specified in the
|
| 127 |
* instance definition. This widget must be available whenever the field
|
| 128 |
* type is available (i.e. provided by the field type module, or by a module
|
| 129 |
* the field type module depends on).
|
| 130 |
* - default_formatter: The machine name of the default formatter to be used
|
| 131 |
* by instances of this field type, when no formatter is specified in the
|
| 132 |
* instance definition. This formatter must be available whenever the field
|
| 133 |
* type is available (i.e. provided by the field type module, or by a module
|
| 134 |
* the field type module depends on).
|
| 135 |
*/
|
| 136 |
function hook_field_info() {
|
| 137 |
return array(
|
| 138 |
'text' => array(
|
| 139 |
'label' => t('Text'),
|
| 140 |
'description' => t('This field stores varchar text in the database.'),
|
| 141 |
'settings' => array('max_length' => 255),
|
| 142 |
'instance_settings' => array('text_processing' => 0),
|
| 143 |
'default_widget' => 'text_textfield',
|
| 144 |
'default_formatter' => 'text_default',
|
| 145 |
),
|
| 146 |
'text_long' => array(
|
| 147 |
'label' => t('Long text'),
|
| 148 |
'description' => t('This field stores long text in the database.'),
|
| 149 |
'settings' => array('max_length' => ''),
|
| 150 |
'instance_settings' => array('text_processing' => 0),
|
| 151 |
'default_widget' => 'text_textarea',
|
| 152 |
'default_formatter' => 'text_default',
|
| 153 |
),
|
| 154 |
'text_with_summary' => array(
|
| 155 |
'label' => t('Long text and summary'),
|
| 156 |
'description' => t('This field stores long text in the database along with optional summary text.'),
|
| 157 |
'settings' => array('max_length' => ''),
|
| 158 |
'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
|
| 159 |
'default_widget' => 'text_textarea_with_summary',
|
| 160 |
'default_formatter' => 'text_summary_or_trimmed',
|
| 161 |
),
|
| 162 |
);
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Perform alterations on Field API field types.
|
| 167 |
*
|
| 168 |
* @param $info
|
| 169 |
* Array of informations on widget types exposed by hook_field_info()
|
| 170 |
* implementations.
|
| 171 |
*/
|
| 172 |
function hook_field_info_alter(&$info) {
|
| 173 |
// Add a setting to all field types.
|
| 174 |
foreach ($info as $field_type => $field_type_info) {
|
| 175 |
$info[$field_type]['settings'] += array(
|
| 176 |
'mymodule_additional_setting' => 'default value',
|
| 177 |
);
|
| 178 |
}
|
| 179 |
|
| 180 |
// Change the default widget for fields of type 'foo'.
|
| 181 |
if (isset($info['foo'])) {
|
| 182 |
$info['foo']['default widget'] = 'mymodule_widget';
|
| 183 |
}
|
| 184 |
}
|
| 185 |
|
| 186 |
/**
|
| 187 |
* Define the Field API schema for a field structure.
|
| 188 |
*
|
| 189 |
* @param $field
|
| 190 |
* A field structure.
|
| 191 |
* @return
|
| 192 |
* An associative array with the following keys:
|
| 193 |
* - columns: An array of Schema API column specifications, keyed by column name.
|
| 194 |
* This specifies what comprises a value for a given field.
|
| 195 |
* For example, a value for a number field is simply 'value', while a
|
| 196 |
* value for a formatted text field is the combination of 'value' and
|
| 197 |
* 'format'.
|
| 198 |
* It is recommended to avoid having the columns definitions depend on
|
| 199 |
* field settings when possible.
|
| 200 |
* No assumptions should be made on how storage engines internally use the
|
| 201 |
* original column name to structure their storage.
|
| 202 |
* - indexes: An array of Schema API indexes definitions. Only columns that
|
| 203 |
* appear in the 'columns' array are allowed.
|
| 204 |
* Those indexes will be used as default indexes. Callers of
|
| 205 |
* field_create_field() can specify additional indexes, or, at their own
|
| 206 |
* risk, modify the default indexes specified by the field-type module.
|
| 207 |
* Some storage engines might not support indexes.
|
| 208 |
*/
|
| 209 |
function hook_field_schema($field) {
|
| 210 |
if ($field['type'] == 'text_long') {
|
| 211 |
$columns = array(
|
| 212 |
'value' => array(
|
| 213 |
'type' => 'text',
|
| 214 |
'size' => 'big',
|
| 215 |
'not null' => FALSE,
|
| 216 |
),
|
| 217 |
);
|
| 218 |
}
|
| 219 |
else {
|
| 220 |
$columns = array(
|
| 221 |
'value' => array(
|
| 222 |
'type' => 'varchar',
|
| 223 |
'length' => $field['settings']['max_length'],
|
| 224 |
'not null' => FALSE,
|
| 225 |
),
|
| 226 |
);
|
| 227 |
}
|
| 228 |
$columns += array(
|
| 229 |
'format' => array(
|
| 230 |
'type' => 'int',
|
| 231 |
'unsigned' => TRUE,
|
| 232 |
'not null' => FALSE,
|
| 233 |
),
|
| 234 |
);
|
| 235 |
return array(
|
| 236 |
'columns' => $columns,
|
| 237 |
'indexes' => array(
|
| 238 |
'format' => array('format'),
|
| 239 |
),
|
| 240 |
);
|
| 241 |
}
|
| 242 |
|
| 243 |
/**
|
| 244 |
* Define custom load behavior for this module's field types.
|
| 245 |
*
|
| 246 |
* Unlike other field hooks, this hook operates on multiple objects. The
|
| 247 |
* $objects, $instances and $items parameters are arrays keyed by object id.
|
| 248 |
* For performance reasons, information for all available objects should be
|
| 249 |
* loaded in a single query where possible.
|
| 250 |
*
|
| 251 |
* Note that the changes made to the field values get cached by the field cache
|
| 252 |
* for subsequent loads. You should never use this hook to load fieldable
|
| 253 |
* entities, since this is likely to cause infinite recursions when
|
| 254 |
* hook_field_load() is run on those as well. Use
|
| 255 |
* hook_field_formatter_prepare_view() instead.
|
| 256 |
*
|
| 257 |
* @param $obj_type
|
| 258 |
* The type of $object.
|
| 259 |
* @param $objects
|
| 260 |
* Array of objects being loaded, keyed by object id.
|
| 261 |
* @param $field
|
| 262 |
* The field structure for the operation.
|
| 263 |
* @param $instances
|
| 264 |
* Array of instance structures for $field for each object, keyed by object id.
|
| 265 |
* @param $langcode
|
| 266 |
* The language associated to $items.
|
| 267 |
* @param $items
|
| 268 |
* Array of field values already loaded for the objects, keyed by object id.
|
| 269 |
* @param $age
|
| 270 |
* FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
|
| 271 |
* FIELD_LOAD_REVISION to load the version indicated by each object.
|
| 272 |
* @return
|
| 273 |
* Changes or additions to field values are done by altering the $items
|
| 274 |
* parameter by reference.
|
| 275 |
*/
|
| 276 |
function hook_field_load($obj_type, $objects, $field, $instances, $langcode, &$items, $age) {
|
| 277 |
foreach ($objects as $id => $object) {
|
| 278 |
foreach ($items[$id] as $delta => $item) {
|
| 279 |
if (!empty($instances[$id]['settings']['text_processing'])) {
|
| 280 |
// Only process items with a cacheable format, the rest will be
|
| 281 |
// handled by hook_field_sanitize().
|
| 282 |
$format = $item['format'];
|
| 283 |
if (filter_format_allowcache($format)) {
|
| 284 |
$items[$id][$delta]['safe'] = isset($item['value']) ? check_markup($item['value'], $format, $langcode) : '';
|
| 285 |
if ($field['type'] == 'text_with_summary') {
|
| 286 |
$items[$id][$delta]['safe_summary'] = isset($item['summary']) ? check_markup($item['summary'], $format, $langcode) : '';
|
| 287 |
}
|
| 288 |
}
|
| 289 |
}
|
| 290 |
else {
|
| 291 |
$items[$id][$delta]['safe'] = check_plain($item['value']);
|
| 292 |
if ($field['type'] == 'text_with_summary') {
|
| 293 |
$items[$id][$delta]['safe_summary'] = check_plain($item['summary']);
|
| 294 |
}
|
| 295 |
}
|
| 296 |
}
|
| 297 |
}
|
| 298 |
}
|
| 299 |
|
| 300 |
/**
|
| 301 |
* Define custom sanitize behavior for this module's field types.
|
| 302 |
*
|
| 303 |
* This hook is invoked just before the field values are handed to formatters
|
| 304 |
* for display. Formatters being essentially theme functions, it is important
|
| 305 |
* that any data sanitization happens outside the theme layer.
|
| 306 |
*
|
| 307 |
* @param $obj_type
|
| 308 |
* The type of $object.
|
| 309 |
* @param $object
|
| 310 |
* The object for the operation.
|
| 311 |
* @param $field
|
| 312 |
* The field structure for the operation.
|
| 313 |
* @param $instance
|
| 314 |
* The instance structure for $field on $object's bundle.
|
| 315 |
* @param $langcode
|
| 316 |
* The language associated to $items.
|
| 317 |
* @param $items
|
| 318 |
* $object->{$field['field_name']}, or an empty array if unset.
|
| 319 |
*/
|
| 320 |
function hook_field_sanitize($obj_type, $object, $field, $instance, $langcode, &$items) {
|
| 321 |
foreach ($items as $delta => $item) {
|
| 322 |
// Only sanitize items which were not already processed inside
|
| 323 |
// hook_field_load(), i.e. items with uncacheable text formats, or coming
|
| 324 |
// from a form preview.
|
| 325 |
if (!isset($items[$delta]['safe'])) {
|
| 326 |
if (!empty($instance['settings']['text_processing'])) {
|
| 327 |
$format = $item['format'];
|
| 328 |
$items[$delta]['safe'] = isset($item['value']) ? check_markup($item['value'], $format, $langcode, TRUE) : '';
|
| 329 |
if ($field['type'] == 'text_with_summary') {
|
| 330 |
$items[$delta]['safe_summary'] = isset($item['summary']) ? check_markup($item['summary'], $format, $langcode, TRUE) : '';
|
| 331 |
}
|
| 332 |
}
|
| 333 |
else {
|
| 334 |
$items[$delta]['safe'] = check_plain($item['value']);
|
| 335 |
if ($field['type'] == 'text_with_summary') {
|
| 336 |
$items[$delta]['safe_summary'] = check_plain($item['summary']);
|
| 337 |
}
|
| 338 |
}
|
| 339 |
}
|
| 340 |
}
|
| 341 |
}
|
| 342 |
|
| 343 |
/**
|
| 344 |
* Define custom validate behavior for this module's field types.
|
| 345 |
*
|
| 346 |
* @param $obj_type
|
| 347 |
* The type of $object.
|
| 348 |
* @param $object
|
| 349 |
* The object for the operation.
|
| 350 |
* Note that this might not be a full-fledged 'object'. When invoked through
|
| 351 |
* field_attach_query(), the $object will only include properties that the
|
| 352 |
* Field API knows about: bundle, id, revision id, and field values (no node
|
| 353 |
* title, user name...).
|
| 354 |
* @param $field
|
| 355 |
* The field structure for the operation.
|
| 356 |
* @param $instance
|
| 357 |
* The instance structure for $field on $object's bundle.
|
| 358 |
* @param $langcode
|
| 359 |
* The language associated to $items.
|
| 360 |
* @param $items
|
| 361 |
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
|
| 362 |
* @param $errors
|
| 363 |
* The array of errors, keyed by field name and by value delta, that have
|
| 364 |
* already been reported for the object. The function should add its errors
|
| 365 |
* to this array. Each error is an associative array, with the following
|
| 366 |
* keys and values:
|
| 367 |
* - 'error': an error code (should be a string, prefixed with the module name)
|
| 368 |
* - 'message': the human readable message to be displayed.
|
| 369 |
*/
|
| 370 |
function hook_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors) {
|
| 371 |
foreach ($items as $delta => $item) {
|
| 372 |
if (!empty($item['value'])) {
|
| 373 |
if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) {
|
| 374 |
$errors[$field['field_name']][$delta][] = array(
|
| 375 |
'error' => 'text_max_length',
|
| 376 |
'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])),
|
| 377 |
);
|
| 378 |
}
|
| 379 |
}
|
| 380 |
}
|
| 381 |
}
|
| 382 |
|
| 383 |
/**
|
| 384 |
* Define custom presave behavior for this module's field types.
|
| 385 |
*
|
| 386 |
* @param $obj_type
|
| 387 |
* The type of $object.
|
| 388 |
* @param $object
|
| 389 |
* The object for the operation.
|
| 390 |
* @param $field
|
| 391 |
* The field structure for the operation.
|
| 392 |
* @param $instance
|
| 393 |
* The instance structure for $field on $object's bundle.
|
| 394 |
* @param $langcode
|
| 395 |
* The language associated to $items.
|
| 396 |
* @param $items
|
| 397 |
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
|
| 398 |
*/
|
| 399 |
function hook_field_presave($obj_type, $object, $field, $instance, $langcode, &$items) {
|
| 400 |
}
|
| 401 |
|
| 402 |
/**
|
| 403 |
* Define custom insert behavior for this module's field types.
|
| 404 |
*
|
| 405 |
* @param $obj_type
|
| 406 |
* The type of $object.
|
| 407 |
* @param $object
|
| 408 |
* The object for the operation.
|
| 409 |
* @param $field
|
| 410 |
* The field structure for the operation.
|
| 411 |
* @param $instance
|
| 412 |
* The instance structure for $field on $object's bundle.
|
| 413 |
* @param $langcode
|
| 414 |
* The language associated to $items.
|
| 415 |
* @param $items
|
| 416 |
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
|
| 417 |
*/
|
| 418 |
function hook_field_insert($obj_type, $object, $field, $instance, $langcode, &$items) {
|
| 419 |
}
|
| 420 |
|
| 421 |
/**
|
| 422 |
* Define custom update behavior for this module's field types.
|
| 423 |
*
|
| 424 |
* @param $obj_type
|
| 425 |
* The type of $object.
|
| 426 |
* @param $object
|
| 427 |
* The object for the operation.
|
| 428 |
* @param $field
|
| 429 |
* The field structure for the operation.
|
| 430 |
* @param $instance
|
| 431 |
* The instance structure for $field on $object's bundle.
|
| 432 |
* @param $langcode
|
| 433 |
* The language associated to $items.
|
| 434 |
* @param $items
|
| 435 |
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
|
| 436 |
*/
|
| 437 |
function hook_field_update($obj_type, $object, $field, $instance, $langcode, &$items) {
|
| 438 |
}
|
| 439 |
|
| 440 |
/**
|
| 441 |
* Define custom delete behavior for this module's field types.
|
| 442 |
*
|
| 443 |
* This hook is invoked just before the data is deleted from field storage.
|
| 444 |
*
|
| 445 |
* @param $obj_type
|
| 446 |
* The type of $object.
|
| 447 |
* @param $object
|
| 448 |
* The object for the operation.
|
| 449 |
* @param $field
|
| 450 |
* The field structure for the operation.
|
| 451 |
* @param $instance
|
| 452 |
* The instance structure for $field on $object's bundle.
|
| 453 |
* @param $langcode
|
| 454 |
* The language associated to $items.
|
| 455 |
* @param $items
|
| 456 |
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
|
| 457 |
*/
|
| 458 |
function hook_field_delete($obj_type, $object, $field, $instance, $langcode, &$items) {
|
| 459 |
}
|
| 460 |
|
| 461 |
/**
|
| 462 |
* Define custom delete_revision behavior for this module's field types.
|
| 463 |
*
|
| 464 |
* This hook is invoked just before the data is deleted from field storage,
|
| 465 |
* and will only be called for fieldable types that are versioned.
|
| 466 |
*
|
| 467 |
* @param $obj_type
|
| 468 |
* The type of $object.
|
| 469 |
* @param $object
|
| 470 |
* The object for the operation.
|
| 471 |
* @param $field
|
| 472 |
* The field structure for the operation.
|
| 473 |
* @param $instance
|
| 474 |
* The instance structure for $field on $object's bundle.
|
| 475 |
* @param $langcode
|
| 476 |
* The language associated to $items.
|
| 477 |
* @param $items
|
| 478 |
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
|
| 479 |
*/
|
| 480 |
function hook_field_delete_revision($obj_type, $object, $field, $instance, $langcode, &$items) {
|
| 481 |
}
|
| 482 |
|
| 483 |
/**
|
| 484 |
* Define custom prepare_translation behavior for this module's field types.
|
| 485 |
*
|
| 486 |
* TODO: This hook may or may not survive in Field API.
|
| 487 |
*
|
| 488 |
* @param $obj_type
|
| 489 |
* The type of $object.
|
| 490 |
* @param $object
|
| 491 |
* The object for the operation.
|
| 492 |
* @param $field
|
| 493 |
* The field structure for the operation.
|
| 494 |
* @param $instance
|
| 495 |
* The instance structure for $field on $object's bundle.
|
| 496 |
* @param $langcode
|
| 497 |
* The language associated to $items.
|
| 498 |
* @param $items
|
| 499 |
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
|
| 500 |
*/
|
| 501 |
function hook_field_prepare_translation($obj_type, $object, $field, $instance, $langcode, &$items) {
|
| 502 |
}
|
| 503 |
|
| 504 |
/**
|
| 505 |
* Define what constitutes an empty item for a field type.
|
| 506 |
*
|
| 507 |
* @param $item
|
| 508 |
* An item that may or may not be empty.
|
| 509 |
* @param $field
|
| 510 |
* The field to which $item belongs.
|
| 511 |
* @return
|
| 512 |
* TRUE if $field's type considers $item not to contain any data;
|
| 513 |
* FALSE otherwise.
|
| 514 |
*/
|
| 515 |
function hook_field_is_empty($item, $field) {
|
| 516 |
if (empty($item['value']) && (string)$item['value'] !== '0') {
|
| 517 |
return TRUE;
|
| 518 |
}
|
| 519 |
return FALSE;
|
| 520 |
}
|
| 521 |
|
| 522 |
/**
|
| 523 |
* Expose Field API widget types.
|
| 524 |
*
|
| 525 |
* Widgets are Form API elements with additional processing capabilities.
|
| 526 |
* Widget hooks are typically called by the Field Attach API during the
|
| 527 |
* creation of the field form structure with field_attach_form().
|
| 528 |
* @see hook_field_widget_info_alter().
|
| 529 |
* @see hook_field_widget().
|
| 530 |
* @see hook_field_widget_error().
|
| 531 |
*
|
| 532 |
* @return
|
| 533 |
* An array describing the widget types implemented by the module.
|
| 534 |
*
|
| 535 |
* The keys are widget type names. To avoid name clashes, widget type
|
| 536 |
* names should be prefixed with the name of the module that exposes them.
|
| 537 |
*
|
| 538 |
* The values are arrays describing the widget type, with the following
|
| 539 |
* key/value pairs:
|
| 540 |
* - label: The human-readable name of the widget type.
|
| 541 |
* - description: A short description for the widget type.
|
| 542 |
* - field types: An array of field types the widget supports.
|
| 543 |
* - settings: An array whose keys are the names of the settings available
|
| 544 |
* for the widget type, and whose values are the default values for those
|
| 545 |
* settings.
|
| 546 |
* - behaviors: (optional) An array describing behaviors of the formatter.
|
| 547 |
* - multiple values:
|
| 548 |
* FIELD_BEHAVIOR_DEFAULT (default) if the widget allows the input of one
|
| 549 |
* single field value (most common case). The widget will be repeated for
|
| 550 |
* each value input.
|
| 551 |
* FIELD_BEHAVIOR_CUSTOM if one single copy of the widget can receive
|
| 552 |
* several field values. Examples: checkboxes, multiple select,
|
| 553 |
* comma-separated textfield...
|
| 554 |
* - default value:
|
| 555 |
* FIELD_BEHAVIOR_DEFAULT (default) if the widget accepts default values.
|
| 556 |
* FIELD_BEHAVIOR_NONE if the widget does not support default values.
|
| 557 |
*/
|
| 558 |
function hook_field_widget_info() {
|
| 559 |
return array(
|
| 560 |
'text_textfield' => array(
|
| 561 |
'label' => t('Text field'),
|
| 562 |
'field types' => array('text'),
|
| 563 |
'settings' => array('size' => 60),
|
| 564 |
'behaviors' => array(
|
| 565 |
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
| 566 |
'default value' => FIELD_BEHAVIOR_DEFAULT,
|
| 567 |
),
|
| 568 |
),
|
| 569 |
'text_textarea' => array(
|
| 570 |
'label' => t('Text area (multiple rows)'),
|
| 571 |
'field types' => array('text_long'),
|
| 572 |
'settings' => array('rows' => 5),
|
| 573 |
'behaviors' => array(
|
| 574 |
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
| 575 |
'default value' => FIELD_BEHAVIOR_DEFAULT,
|
| 576 |
),
|
| 577 |
),
|
| 578 |
'text_textarea_with_summary' => array(
|
| 579 |
'label' => t('Text area with a summary'),
|
| 580 |
'field types' => array('text_with_summary'),
|
| 581 |
'settings' => array('rows' => 20, 'summary_rows' => 5),
|
| 582 |
'behaviors' => array(
|
| 583 |
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
| 584 |
'default value' => FIELD_BEHAVIOR_DEFAULT,
|
| 585 |
),
|
| 586 |
),
|
| 587 |
);
|
| 588 |
}
|
| 589 |
|
| 590 |
|
| 591 |
/**
|
| 592 |
* Perform alterations on Field API widget types.
|
| 593 |
*
|
| 594 |
* @param $info
|
| 595 |
* Array of informations on widget types exposed by hook_field_widget_info()
|
| 596 |
* implementations.
|
| 597 |
*/
|
| 598 |
function hook_field_widget_info_alter(&$info) {
|
| 599 |
// Add a setting to a widget type.
|
| 600 |
$info['text_textfield']['settings'] += array(
|
| 601 |
'mymodule_additional_setting' => 'default value',
|
| 602 |
);
|
| 603 |
|
| 604 |
// Let a new field type re-use an existing widget.
|
| 605 |
$info['options_select']['field types'][] = 'my_field_type';
|
| 606 |
}
|
| 607 |
|
| 608 |
/**
|
| 609 |
* Return a single form element for a form.
|
| 610 |
*
|
| 611 |
* It will be built out and validated in the callback(s) listed in
|
| 612 |
* hook_element_info(). We build it out in the callbacks rather than in
|
| 613 |
* hook_field_widget so it can be plugged into any module that can
|
| 614 |
* provide it with valid $field information.
|
| 615 |
*
|
| 616 |
* Field API will set the weight, field name and delta values for each
|
| 617 |
* form element. If there are multiple values for this field, the
|
| 618 |
* Field API will call this function as many times as needed.
|
| 619 |
*
|
| 620 |
* @param $form
|
| 621 |
* The entire form array.
|
| 622 |
* @param $form_state
|
| 623 |
* The form_state, $form_state['values'][$field['field_name']]
|
| 624 |
* holds the field's form values.
|
| 625 |
* @param $field
|
| 626 |
* The field structure.
|
| 627 |
* @param $instance
|
| 628 |
* The field instance.
|
| 629 |
* @param $langcode
|
| 630 |
* The language associated to $items.
|
| 631 |
* @param $items
|
| 632 |
* Array of default values for this field.
|
| 633 |
* @param $delta
|
| 634 |
* The order of this item in the array of subelements (0, 1, 2, etc).
|
| 635 |
* @param $element
|
| 636 |
* A form element array containing basic properties for the widget: #title,
|
| 637 |
* #description, #required, #field, #field_instance, #field_name, #delta,
|
| 638 |
* #columns.
|
| 639 |
* @return
|
| 640 |
* The form item for a single element for this field.
|
| 641 |
*/
|
| 642 |
function hook_field_widget(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
| 643 |
$element += array(
|
| 644 |
'#type' => $instance['widget']['type'],
|
| 645 |
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
|
| 646 |
);
|
| 647 |
return $element;
|
| 648 |
}
|
| 649 |
|
| 650 |
/**
|
| 651 |
* Flag a field-level validation error.
|
| 652 |
*
|
| 653 |
* @param $element
|
| 654 |
* An array containing the form element for the widget. The error needs to be
|
| 655 |
* flagged on the right sub-element, according to the widget's internal
|
| 656 |
* structure.
|
| 657 |
* @param $error
|
| 658 |
* An associative array with the following key-value pairs, as returned by
|
| 659 |
* hook_field_validate():
|
| 660 |
* - 'error': the error code. Complex widgets might need to report different
|
| 661 |
* errors to different form elements inside the widget.
|
| 662 |
* - 'message': the human readable message to be displayed.
|
| 663 |
*/
|
| 664 |
function hook_field_widget_error($element, $error) {
|
| 665 |
form_error($element['value'], $error['message']);
|
| 666 |
}
|
| 667 |
|
| 668 |
/**
|
| 669 |
* Expose Field API formatter types.
|
| 670 |
*
|
| 671 |
* Formatters are mainly theme functions that handle the output of individual
|
| 672 |
* field values. These theme calls are typically triggered during the execution
|
| 673 |
* of drupal_render() on the render structure built by field_attach_view().
|
| 674 |
*
|
| 675 |
* The name of the theme hook invoked when displaying the values is derived
|
| 676 |
* from formatter type names, using the pattern field_formatter_FORMATTER_NAME.
|
| 677 |
* field.module takes care of exposing the corresponding theme functions
|
| 678 |
* through hook_theme(). Specifically, field.module defines the theme
|
| 679 |
* hook:
|
| 680 |
*
|
| 681 |
* @code
|
| 682 |
* 'field_formatter_FORMATTER_NAME' => array(
|
| 683 |
* 'render_element' => 'element',
|
| 684 |
* )
|
| 685 |
* @code
|
| 686 |
*
|
| 687 |
* If a formatter requires a different theme hook definition,
|
| 688 |
* implement hook_theme_registry_alter().
|
| 689 |
*
|
| 690 |
* @see hook_field_formatter_info().
|
| 691 |
* @see hook_field_formatter_info_alter().
|
| 692 |
* @see theme_field_formatter_FORMATTER_NAME().
|
| 693 |
* @see hook_theme().
|
| 694 |
* @see hook_theme_registry_alter().
|
| 695 |
*
|
| 696 |
* @return
|
| 697 |
* An array describing the formatter types implemented by the module.
|
| 698 |
*
|
| 699 |
* The keys are formatter type names. To avoid name clashes, formatter type
|
| 700 |
* names should be prefixed with the name of the module that exposes them.
|
| 701 |
*
|
| 702 |
* The values are arrays describing the formatter type, with the following
|
| 703 |
* key/value pairs:
|
| 704 |
* - label: The human-readable name of the formatter type.
|
| 705 |
* - description: A short description for the formatter type.
|
| 706 |
* - field types: An array of field types the formatter supports.
|
| 707 |
* - settings: An array whose keys are the names of the settings available
|
| 708 |
* for the formatter type, and whose values are the default values for
|
| 709 |
* those settings.
|
| 710 |
* - behaviors: (optional) An array describing behaviors of the formatter.
|
| 711 |
* - multiple values:
|
| 712 |
* FIELD_BEHAVIOR_DEFAULT (default) if the formatter displays one single
|
| 713 |
* field value (most common case). The formatter theme will be invoked
|
| 714 |
* iteratively on each of the field valies.
|
| 715 |
* FIELD_BEHAVIOR_CUSTOM if one single invocation of the formatter theme
|
| 716 |
* takes care of displays all the field values. Examples: points on
|
| 717 |
* a generated graph picture, a Google map, a single link to a popup...
|
| 718 |
*/
|
| 719 |
function hook_field_formatter_info() {
|
| 720 |
return array(
|
| 721 |
'text_default' => array(
|
| 722 |
'label' => t('Default'),
|
| 723 |
'field types' => array('text', 'text_long', 'text_with_summary'),
|
| 724 |
'behaviors' => array(
|
| 725 |
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
| 726 |
),
|
| 727 |
),
|
| 728 |
'text_plain' => array(
|
| 729 |
'label' => t('Plain text'),
|
| 730 |
'field types' => array('text', 'text_long', 'text_with_summary'),
|
| 731 |
'behaviors' => array(
|
| 732 |
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
| 733 |
),
|
| 734 |
),
|
| 735 |
|
| 736 |
// The text_trimmed formatter displays the trimmed version of the
|
| 737 |
// full element of the field. It is intended to be used with text
|
| 738 |
// and text_long fields. It also works with text_with_summary
|
| 739 |
// fields though the text_summary_or_trimmed formatter makes more
|
| 740 |
// sense for that field type.
|
| 741 |
'text_trimmed' => array(
|
| 742 |
'label' => t('Trimmed'),
|
| 743 |
'field types' => array('text', 'text_long', 'text_with_summary'),
|
| 744 |
'behaviors' => array(
|
| 745 |
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
| 746 |
),
|
| 747 |
),
|
| 748 |
|
| 749 |
// The 'summary or trimmed' field formatter for text_with_summary
|
| 750 |
// fields displays returns the summary element of the field or, if
|
| 751 |
// the summary is empty, the trimmed version of the full element
|
| 752 |
// of the field.
|
| 753 |
'text_summary_or_trimmed' => array(
|
| 754 |
'label' => t('Summary or trimmed'),
|
| 755 |
'field types' => array('text_with_summary'),
|
| 756 |
'behaviors' => array(
|
| 757 |
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
|
| 758 |
),
|
| 759 |
),
|
| 760 |
);
|
| 761 |
}
|
| 762 |
|
| 763 |
|
| 764 |
/**
|
| 765 |
* Perform alterations on Field API formatter types.
|
| 766 |
*
|
| 767 |
* @param $info
|
| 768 |
* Array of informations on formatter types exposed by
|
| 769 |
* hook_field_field_formatter_info() implementations.
|
| 770 |
*/
|
| 771 |
function hook_field_formatter_info_alter(&$info) {
|
| 772 |
// Add a setting to a formatter type.
|
| 773 |
$info['text_default']['settings'] += array(
|
| 774 |
'mymodule_additional_setting' => 'default value',
|
| 775 |
);
|
| 776 |
|
| 777 |
// Let a new field type re-use an existing formatter.
|
| 778 |
$info['text_default']['field types'][] = 'my_field_type';
|
| 779 |
}
|
| 780 |
|
| 781 |
/**
|
| 782 |
* Theme function for a field formatter.
|
| 783 |
*
|
| 784 |
* This is an example of a 'single' formatter, displaying one single field
|
| 785 |
* value (the hook_field_formatter_info() entry uses
|
| 786 |
* 'multiple values' = FIELD_BEHAVIOR_DEFAULT).
|
| 787 |
*
|
| 788 |
* @param $variables
|
| 789 |
* An associative array containing:
|
| 790 |
* - element: A render structure sub-array, containing the following keys:
|
| 791 |
* - #item: The field value being displayed.
|
| 792 |
* - #delta: The index of the value being displayed within the object's
|
| 793 |
* values for the field.
|
| 794 |
* - #field_name: The name of the field being displayed.
|
| 795 |
* - #bundle: The bundle of the object being displayed.
|
| 796 |
* - #object: The object being displayed.
|
| 797 |
* - #object_type: The type of the object being displayed.
|
| 798 |
* - #formatter: The name of the formatter being used.
|
| 799 |
* - #settings: The array of formatter settings.
|
| 800 |
*/
|
| 801 |
function theme_field_formatter_FORMATTER_SINGLE($variables) {
|
| 802 |
// This relies on a 'safe' element being prepared in hook_field_sanitize().
|
| 803 |
return $variables['element']['#item']['safe'];
|
| 804 |
}
|
| 805 |
|
| 806 |
/**
|
| 807 |
* Theme function for a field formatter.
|
| 808 |
*
|
| 809 |
* This is an example of a 'single' formatter, displaying all the field values
|
| 810 |
* (the hook_field_formatter_info() entry uses
|
| 811 |
* 'multiple values' = FIELD_BEHAVIOR_CUSTOM).
|
| 812 |
*
|
| 813 |
* @param $variables
|
| 814 |
* An associative array containing:
|
| 815 |
* - element: A render structure sub-array, containing the following keys:
|
| 816 |
* - #field_name: The name of the field being displayed.
|
| 817 |
* - #bundle: The bundle of the object being displayed.
|
| 818 |
* - #object: The object being displayed.
|
| 819 |
* - #object_type: The type of the object being displayed.
|
| 820 |
* - #formatter: The name of the formatter being used.
|
| 821 |
* - #settings: The array of formatter settings.
|
| 822 |
* - numeric indexes: the field values being displayed.
|
| 823 |
*/
|
| 824 |
function theme_field_formatter_FORMATTER_MULTIPLE($variables) {
|
| 825 |
$element = $variables['element'];
|
| 826 |
|
| 827 |
$items = array();
|
| 828 |
foreach (element_children($element) as $key) {
|
| 829 |
$items[$key] = $key .':'. $element[$key]['#item']['value'];
|
| 830 |
}
|
| 831 |
$output = implode('|', $items);
|
| 832 |
return $output;
|
| 833 |
}
|
| 834 |
|
| 835 |
/**
|
| 836 |
* Allow formatters to load information for multiple objects.
|
| 837 |
*
|
| 838 |
* This should be used when a formatter needs to load additional information
|
| 839 |
* from the database in order to render a field, for example a reference field
|
| 840 |
* which displays properties of the referenced objects such as name or type.
|
| 841 |
*
|
| 842 |
* @param $obj_type
|
| 843 |
* The type of $object.
|
| 844 |
* @param $objects
|
| 845 |
* Array of objects being displayed, keyed by object id.
|
| 846 |
* @param $field
|
| 847 |
* The field structure for the operation.
|
| 848 |
* @param $instances
|
| 849 |
* Array of instance structures for $field for each object, keyed by object id.
|
| 850 |
* @param $langcode
|
| 851 |
* The language the field values are to be shown in. If no language is
|
| 852 |
* provided the current language is used.
|
| 853 |
* @param $items
|
| 854 |
* Array of field values for the objects, keyed by object id.
|
| 855 |
* @return
|
| 856 |
* Changes or additions to field values are done by altering the $items
|
| 857 |
* parameter by reference.
|
| 858 |
*/
|
| 859 |
function hook_field_formatter_prepare_view($obj_type, $objects, $field, $instances, $langcode, &$items, $build_mode) {
|
| 860 |
|
| 861 |
}
|
| 862 |
|
| 863 |
/**
|
| 864 |
* @} End of "ingroup field_type"
|
| 865 |
*/
|
| 866 |
|
| 867 |
/**
|
| 868 |
* @ingroup field_attach
|
| 869 |
* @{
|
| 870 |
*/
|
| 871 |
|
| 872 |
/**
|
| 873 |
* Act on field_attach_form.
|
| 874 |
*
|
| 875 |
* This hook is invoked after the field module has performed the operation.
|
| 876 |
*
|
| 877 |
* See field_attach_form() for details and arguments.
|
| 878 |
*/
|
| 879 |
function hook_field_attach_form($obj_type, $object, &$form, &$form_state, $langcode) {
|
| 880 |
$tids = array();
|
| 881 |
|
| 882 |
// Collect every possible term attached to any of the fieldable entities.
|
| 883 |
foreach ($objects as $id => $object) {
|
| 884 |
foreach ($items[$id] as $delta => $item) {
|
| 885 |
// Force the array key to prevent duplicates.
|
| 886 |
$tids[$item['value']] = $item['value'];
|
| 887 |
}
|
| 888 |
}
|
| 889 |
if ($tids) {
|
| 890 |
$terms = array();
|
| 891 |
|
| 892 |
// Avoid calling taxonomy_term_load_multiple because it could lead to
|
| 893 |
// circular references.
|
| 894 |
$query = db_select('taxonomy_term_data', 't');
|
| 895 |
$query->fields('t');
|
| 896 |
$query->condition('t.tid', $tids, 'IN');
|
| 897 |
$query->addTag('term_access');
|
| 898 |
$terms = $query->execute()->fetchAllAssoc('tid');
|
| 899 |
|
| 900 |
// Iterate through the fieldable entities again to attach the loaded term data.
|
| 901 |
foreach ($objects as $id => $object) {
|
| 902 |
foreach ($items[$id] as $delta => $item) {
|
| 903 |
// Check whether the taxonomy term field instance value could be loaded.
|
| 904 |
if (isset($terms[$item['value']])) {
|
| 905 |
// Replace the instance value with the term data.
|
| 906 |
$items[$id][$delta]['taxonomy_term'] = $terms[$item['value']];
|
| 907 |
}
|
| 908 |
// Otherwise, unset the instance value, since the term does not exist.
|
| 909 |
else {
|
| 910 |
unset($items[$id][$delta]);
|
| 911 |
}
|
| 912 |
}
|
| 913 |
}
|
| 914 |
}
|
| 915 |
}
|
| 916 |
|
| 917 |
/**
|
| 918 |
* Act on field_attach_pre_load.
|
| 919 |
*
|
| 920 |
* This hook allows modules to load data before the Field Storage API,
|
| 921 |
* optionally preventing the field storage module from doing so.
|
| 922 |
*
|
| 923 |
* This lets 3rd party modules override, mirror, shard, or otherwise store a
|
| 924 |
* subset of fields in a different way than the current storage engine.
|
| 925 |
* Possible use cases include : per-bundle storage, per-combo-field storage...
|
| 926 |
*
|
| 927 |
* @param $obj_type
|
| 928 |
* The type of objects for which to load fields; e.g. 'node' or 'user'.
|
| 929 |
* @param $objects
|
| 930 |
* An array of objects for which to load fields, keyed by object id.
|
| 931 |
* @param $age
|
| 932 |
* FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
|
| 933 |
* FIELD_LOAD_REVISION to load the version indicated by each object.
|
| 934 |
* @param $skip_fields
|
| 935 |
* An array keyed by field ids whose data has already been loaded and
|
| 936 |
* therefore should not be loaded again. The values associated to these keys
|
| 937 |
* are not specified.
|
| 938 |
* @return
|
| 939 |
* - Loaded field values are added to $objects. Fields with no values should be
|
| 940 |
* set as an empty array.
|
| 941 |
* - Loaded field ids are set as keys in $skip_fields.
|
| 942 |
*/
|
| 943 |
function hook_field_attach_pre_load($obj_type, $objects, $age, &$skip_fields) {
|
| 944 |
}
|
| 945 |
|
| 946 |
/**
|
| 947 |
* Act on field_attach_load.
|
| 948 |
*
|
| 949 |
* This hook is invoked after the field module has performed the operation.
|
| 950 |
*
|
| 951 |
* Unlike other field_attach hooks, this hook accounts for 'multiple loads'.
|
| 952 |
* Instead of the usual $object parameter, it accepts an array of objects,
|
| 953 |
* indexed by object id. For performance reasons, information for all available
|
| 954 |
* objects should be loaded in a single query where possible.
|
| 955 |
*
|
| 956 |
* Note that $objects might not be full-fledged 'objects'. When invoked through
|
| 957 |
* field_attach_query(), each object only includes properties that the Field
|
| 958 |
* API knows about: bundle, id, revision id, and field values (no node title,
|
| 959 |
* user name...)
|
| 960 |
|
| 961 |
* The changes made to the objects' field values get cached by the field cache
|
| 962 |
* for subsequent loads.
|
| 963 |
*
|
| 964 |
* See field_attach_load() for details and arguments.
|
| 965 |
*/
|
| 966 |
function hook_field_attach_load($obj_type, $objects, $age) {
|
| 967 |
}
|
| 968 |
|
| 969 |
/**
|
| 970 |
* Act on field_attach_validate.
|
| 971 |
*
|
| 972 |
* This hook is invoked after the field module has performed the operation.
|
| 973 |
*
|
| 974 |
* See field_attach_validate() for details and arguments.
|
| 975 |
*/
|
| 976 |
function hook_field_attach_validate($obj_type, $object, &$errors) {
|
| 977 |
}
|
| 978 |
|
| 979 |
/**
|
| 980 |
* Act on field_attach_submit.
|
| 981 |
*
|
| 982 |
* This hook is invoked after the field module has performed the operation.
|
| 983 |
*
|
| 984 |
* See field_attach_submit() for details and arguments.
|
| 985 |
*/
|
| 986 |
function hook_field_attach_submit($obj_type, $object, $form, &$form_state) {
|
| 987 |
}
|
| 988 |
|
| 989 |
/**
|
| 990 |
* Act on field_attach_presave.
|
| 991 |
*
|
| 992 |
* This hook is invoked after the field module has performed the operation.
|
| 993 |
*
|
| 994 |
* See field_attach_presave() for details and arguments.
|
| 995 |
*/
|
| 996 |
function hook_field_attach_presave($obj_type, $object) {
|
| 997 |
}
|
| 998 |
|
| 999 |
/**
|
| 1000 |
* Act on field_attach_preprocess.
|
| 1001 |
*
|
| 1002 |
* This hook is invoked while preprocessing the field.tpl.php template file.
|
| 1003 |
*
|
| 1004 |
* @param $variables
|
| 1005 |
* The variables array is passed by reference and will be populated with field
|
| 1006 |
* values.
|
| 1007 |
* @param $context
|
| 1008 |
* An associative array containing:
|
| 1009 |
* - obj_type: The type of $object; e.g. 'node' or 'user'.
|
| 1010 |
* - object: The object with fields to render.
|
| 1011 |
* - element: The structured array containing the values ready for rendering.
|
| 1012 |
*/
|
| 1013 |
function hook_field_attach_preprocess_alter(&$variables, $context) {
|
| 1014 |
}
|
| 1015 |
|
| 1016 |
/**
|
| 1017 |
* Act on field_attach_insert.
|
| 1018 |
*
|
| 1019 |
* This hook allows modules to store data before the Field Storage
|
| 1020 |
* API, optionally preventing the field storage module from doing so.
|
| 1021 |
*
|
| 1022 |
* @param $obj_type
|
| 1023 |
* The type of $object; e.g. 'node' or 'user'.
|
| 1024 |
* @param $object
|
| 1025 |
* The object with fields to save.
|
| 1026 |
* @param $skip_fields
|
| 1027 |
* An array keyed by field ids whose data has already been written and
|
| 1028 |
* therefore should not be written again. The values associated to these keys
|
| 1029 |
* are not specified.
|
| 1030 |
* @return
|
| 1031 |
* Saved field ids are set set as keys in $skip_fields.
|
| 1032 |
*/
|
| 1033 |
function hook_field_attach_pre_insert($obj_type, $object, &$skip_fields) {
|
| 1034 |
}
|
| 1035 |
|
| 1036 |
/**
|
| 1037 |
* Act on field_attach_update.
|
| 1038 |
*
|
| 1039 |
* This hook allows modules to store data before the Field Storage
|
| 1040 |
* API, optionally preventing the field storage module from doing so.
|
| 1041 |
*
|
| 1042 |
* @param $obj_type
|
| 1043 |
* The type of $object; e.g. 'node' or 'user'.
|
| 1044 |
* @param $object
|
| 1045 |
* The object with fields to save.
|
| 1046 |
* @param $skip_fields
|
| 1047 |
* An array keyed by field ids whose data has already been written and
|
| 1048 |
* therefore should not be written again. The values associated to these keys
|
| 1049 |
* are not specified.
|
| 1050 |
* @return
|
| 1051 |
* Saved field ids are set set as keys in $skip_fields.
|
| 1052 |
*/
|
| 1053 |
function hook_field_attach_pre_update($obj_type, $object, &$skip_fields) {
|
| 1054 |
}
|
| 1055 |
|
| 1056 |
/**
|
| 1057 |
* Act on field_attach_pre_query.
|
| 1058 |
*
|
| 1059 |
* This hook should be implemented by modules that use
|
| 1060 |
* hook_field_attach_pre_load(), hook_field_attach_pre_insert() and
|
| 1061 |
* hook_field_attach_pre_update() to bypass the regular storage engine, to
|
| 1062 |
* handle field queries.
|
| 1063 |
*
|
| 1064 |
* @param $field_name
|
| 1065 |
* The name of the field to query.
|
| 1066 |
* @param $conditions
|
| 1067 |
* See field_attach_query().
|
| 1068 |
* A storage module that doesn't support querying a given column should raise
|
| 1069 |
* a FieldQueryException. Incompatibilities should be mentioned on the module
|
| 1070 |
* project page.
|
| 1071 |
* @param $options
|
| 1072 |
* See field_attach_query(). All option keys are guaranteed to be specified.
|
| 1073 |
* @param $skip_field
|
| 1074 |
* Boolean, always coming as FALSE.
|
| 1075 |
* @return
|
| 1076 |
* See field_attach_query().
|
| 1077 |
* The $skip_field parameter should be set to TRUE if the query has been
|
| 1078 |
* handled.
|
| 1079 |
*/
|
| 1080 |
function hook_field_attach_pre_query($field_name, $conditions, $options, &$skip_field) {
|
| 1081 |
}
|
| 1082 |
|
| 1083 |
/**
|
| 1084 |
* Act on field_attach_delete.
|
| 1085 |
*
|
| 1086 |
* This hook is invoked after the field module has performed the operation.
|
| 1087 |
*
|
| 1088 |
* See field_attach_delete() for details and arguments.
|
| 1089 |
*/
|
| 1090 |
function hook_field_attach_delete($obj_type, $object) {
|
| 1091 |
}
|
| 1092 |
|
| 1093 |
/**
|
| 1094 |
* Act on field_attach_delete_revision.
|
| 1095 |
*
|
| 1096 |
* This hook is invoked after the field module has performed the operation.
|
| 1097 |
*
|
| 1098 |
* See field_attach_delete_revision() for details and arguments.
|
| 1099 |
*/
|
| 1100 |
function hook_field_attach_delete_revision($obj_type, $object) {
|
| 1101 |
}
|
| 1102 |
|
| 1103 |
/**
|
| 1104 |
* Act on field_attach_view.
|
| 1105 |
*
|
| 1106 |
* This hook is invoked after the field module has performed the operation.
|
| 1107 |
*
|
| 1108 |
* @param &$output
|
| 1109 |
* The structured content array tree for all of $object's fields.
|
| 1110 |
* @param $context
|
| 1111 |
* An associative array containing:
|
| 1112 |
* - obj_type: The type of $object; e.g. 'node' or 'user'.
|
| 1113 |
* - object: The object with fields to render.
|
| 1114 |
* - build_mode: Build mode, e.g. 'full', 'teaser'...
|
| 1115 |
* - langcode: The language in which the field values will be displayed.
|
| 1116 |
*/
|
| 1117 |
function hook_field_attach_view_alter(&$output, $context) {
|
| 1118 |
}
|
| 1119 |
|
| 1120 |
/**
|
| 1121 |
* Act on field_attach_create_bundle.
|
| 1122 |
*
|
| 1123 |
* This hook is invoked after the field module has performed the operation.
|
| 1124 |
*
|
| 1125 |
* See field_attach_create_bundle() for details and arguments.
|
| 1126 |
*/
|
| 1127 |
function hook_field_attach_create_bundle($obj_type, $bundle) {
|
| 1128 |
}
|
| 1129 |
|
| 1130 |
/**
|
| 1131 |
* Act on field_attach_rename_bundle.
|
| 1132 |
*
|
| 1133 |
* This hook is invoked after the field module has performed the operation.
|
| 1134 |
*
|
| 1135 |
* See field_attach_rename_bundle() for details and arguments.
|
| 1136 |
*/
|
| 1137 |
function hook_field_attach_rename_bundle($obj_type, $bundle_old, $bundle_new) {
|
| 1138 |
}
|
| 1139 |
|
| 1140 |
/**
|
| 1141 |
* Act on field_attach_delete_bundle.
|
| 1142 |
*
|
| 1143 |
* This hook is invoked after the field module has performed the operation.
|
| 1144 |
*
|
| 1145 |
* @param $obj_type
|
| 1146 |
* The type of object; e.g. 'node' or 'user'.
|
| 1147 |
* @param $bundle
|
| 1148 |
* The bundle that was just deleted.
|
| 1149 |
* @param $instances
|
| 1150 |
* An array of all instances that existed for the bundle before it was
|
| 1151 |
* deleted.
|
| 1152 |
*/
|
| 1153 |
function hook_field_attach_delete_bundle($obj_type, $bundle, $instances) {
|
| 1154 |
}
|
| 1155 |
|
| 1156 |
/**
|
| 1157 |
* @} End of "ingroup field_attach"
|
| 1158 |
*/
|
| 1159 |
|
| 1160 |
/**********************************************************************
|
| 1161 |
* Field Storage API
|
| 1162 |
**********************************************************************/
|
| 1163 |
|
| 1164 |
/**
|
| 1165 |
* @ingroup field_storage
|
| 1166 |
* @{
|
| 1167 |
*/
|
| 1168 |
|
| 1169 |
/**
|
| 1170 |
* Expose Field API storage backends.
|
| 1171 |
*
|
| 1172 |
* @return
|
| 1173 |
* An array describing the storage backends implemented by the module.
|
| 1174 |
* The keys are storage backend names. To avoid name clashes, storage backend
|
| 1175 |
* names should be prefixed with the name of the module that exposes them.
|
| 1176 |
* The values are arrays describing the storage backend, with the following
|
| 1177 |
* key/value pairs:
|
| 1178 |
* - label: The human-readable name of the storage backend.
|
| 1179 |
* - description: A short description for the storage backend.
|
| 1180 |
* - settings: An array whose keys are the names of the settings available
|
| 1181 |
* for the storage backend, and whose values are the default values for
|
| 1182 |
* those settings.
|
| 1183 |
*/
|
| 1184 |
function hook_field_storage_info() {
|
| 1185 |
return array(
|
| 1186 |
'field_sql_storage' => array(
|
| 1187 |
'label' => t('Default SQL storage'),
|
| 1188 |
'description' => t('Stores fields in the local SQL database, using per-field tables.'),
|
| 1189 |
'settings' => array(),
|
| 1190 |
),
|
| 1191 |
);
|
| 1192 |
}
|
| 1193 |
|
| 1194 |
/**
|
| 1195 |
* Perform alterations on Field API storage types.
|
| 1196 |
*
|
| 1197 |
* @param $info
|
| 1198 |
* Array of informations on storage types exposed by
|
| 1199 |
* hook_field_field_storage_info() implementations.
|
| 1200 |
*/
|
| 1201 |
function hook_field_storage_info_alter(&$info) {
|
| 1202 |
// Add a setting to a storage type.
|
| 1203 |
$info['field_sql_storage']['settings'] += array(
|
| 1204 |
'mymodule_additional_setting' => 'default value',
|
| 1205 |
);
|
| 1206 |
}
|
| 1207 |
|
| 1208 |
/**
|
| 1209 |
* Reveal the internal details about the storage for a field.
|
| 1210 |
*
|
| 1211 |
* For example, an SQL storage module might return the Schema API structure for
|
| 1212 |
* the table. A key/value storage module might return the server name,
|
| 1213 |
* authentication credentials, and bin name.
|
| 1214 |
*
|
| 1215 |
* Field storage modules are not obligated to implement this hook. Modules
|
| 1216 |
* that rely on these details must only use them for read operations.
|
| 1217 |
*
|
| 1218 |
* @param $field
|
| 1219 |
* A field structure.
|
| 1220 |
* @param $instance
|
| 1221 |
* A field instance structure.
|
| 1222 |
* @return
|
| 1223 |
* An array of details.
|
| 1224 |
* - The first dimension is a store type (sql, solr, etc).
|
| 1225 |
* - The second dimension indicates the age of the values in the store
|
| 1226 |
* FIELD_LOAD_CURRENT or FIELD_LOAD_REVISION.
|
| 1227 |
* - Other dimensions are specific to the field storage module.
|
| 1228 |
*/
|
| 1229 |
function hook_field_storage_details($field, $instance) {
|
| 1230 |
}
|
| 1231 |
|
| 1232 |
/**
|
| 1233 |
* Perform alterations on Field API storage details.
|
| 1234 |
*
|
| 1235 |
* The storage details are appended to the field instance structure after this
|
| 1236 |
* hook is invoked. Read and alter the $details only.
|
| 1237 |
*
|
| 1238 |
* @param $details
|
| 1239 |
* An array of storage details for fields as exposed by
|
| 1240 |
* hook_field_storage_details() implementations.
|
| 1241 |
* @param $field
|
| 1242 |
* A field structure.
|
| 1243 |
* @param $instance
|
| 1244 |
* A field instance structure.
|
| 1245 |
*/
|
| 1246 |
function hook_field_storage_details_alter(&$details, $field, $instance) {
|
| 1247 |
}
|
| 1248 |
|
| 1249 |
/**
|
| 1250 |
* Load field data for a set of objects.
|
| 1251 |
*
|
| 1252 |
* @param $obj_type
|
| 1253 |
* The entity type of object, such as 'node' or 'user'.
|
| 1254 |
* @param $objects
|
| 1255 |
* The array of objects for which to load data, keyed by object id.
|
| 1256 |
* @param $age
|
| 1257 |
* FIELD_LOAD_CURRENT to load the most recent revision for all
|
| 1258 |
* fields, or FIELD_LOAD_REVISION to load the version indicated by
|
| 1259 |
* each object.
|
| 1260 |
* @param $fields
|
| 1261 |
* An array listing the fields to be loaded. The keys of the array are field
|
| 1262 |
* ids, the values of the array are the object ids (or revision ids,
|
| 1263 |
* depending on the $age parameter) to be loaded for each field.
|
| 1264 |
* @return
|
| 1265 |
* Loaded field values are added to $objects. Fields with no values should be
|
| 1266 |
* set as an empty array.
|
| 1267 |
*/
|
| 1268 |
function hook_field_storage_load($obj_type, $objects, $age, $fields) {
|
| 1269 |
}
|
| 1270 |
|
| 1271 |
/**
|
| 1272 |
* Write field data for an object.
|
| 1273 |
*
|
| 1274 |
* @param $obj_type
|
| 1275 |
* The entity type of object, such as 'node' or 'user'.
|
| 1276 |
* @param $object
|
| 1277 |
* The object on which to operate.
|
| 1278 |
* @param $op
|
| 1279 |
* FIELD_STORAGE_UPDATE when updating an existing object,
|
| 1280 |
* FIELD_STORAGE_INSERT when inserting a new object.
|
| 1281 |
* @param $fields
|
| 1282 |
* An array listing the fields to be written. The keys and values of the
|
| 1283 |
* array are field ids.
|
| 1284 |
*/
|
| 1285 |
function hook_field_storage_write($obj_type, $object, $op, $fields) {
|
| 1286 |
}
|
| 1287 |
|
| 1288 |
/**
|
| 1289 |
* Delete all field data for an object.
|
| 1290 |
*
|
| 1291 |
* @param $obj_type
|
| 1292 |
* The entity type of object, such as 'node' or 'user'.
|
| 1293 |
* @param $object
|
| 1294 |
* The object on which to operate.
|
| 1295 |
* @param $fields
|
| 1296 |
* An array listing the fields to delete. The keys and values of the
|
| 1297 |
* array are field ids.
|
| 1298 |
*/
|
| 1299 |
function hook_field_storage_delete($obj_type, $object, $fields) {
|
| 1300 |
}
|
| 1301 |
|
| 1302 |
/**
|
| 1303 |
* Delete a single revision of field data for an object.
|
| 1304 |
*
|
| 1305 |
* Deleting the current (most recently written) revision is not
|
| 1306 |
* allowed as has undefined results.
|
| 1307 |
*
|
| 1308 |
* @param $obj_type
|
| 1309 |
* The entity type of object, such as 'node' or 'user'.
|
| 1310 |
* @param $object
|
| 1311 |
* The object on which to operate. The revision to delete is
|
| 1312 |
* indicated by the object's revision id property, as identified by
|
| 1313 |
* hook_fieldable_info() for $obj_type.
|
| 1314 |
* @param $fields
|
| 1315 |
* An array listing the fields to delete. The keys and values of the
|
| 1316 |
* array are field ids.
|
| 1317 |
*/
|
| 1318 |
function hook_field_storage_delete_revision($obj_type, $object, $fields) {
|
| 1319 |
}
|
| 1320 |
|
| 1321 |
/**
|
| 1322 |
* Handle a field query.
|
| 1323 |
*
|
| 1324 |
* @param $field_name
|
| 1325 |
* The name of the field to query.
|
| 1326 |
* @param $conditions
|
| 1327 |
* See field_attach_query().
|
| 1328 |
* A storage module that doesn't support querying a given column should raise
|
| 1329 |
* a FieldQueryException. Incompatibilities should be mentioned on the module
|
| 1330 |
* project page.
|
| 1331 |
* @param $options
|
| 1332 |
* See field_attach_query(). All option keys are guaranteed to be specified.
|
| 1333 |
* @return
|
| 1334 |
* See field_attach_query().
|
| 1335 |
*/
|
| 1336 |
function hook_field_storage_query($field_name, $conditions, $options) {
|
| 1337 |
}
|
| 1338 |
|
| 1339 |
/**
|
| 1340 |
* Act on creation of a new field.
|
| 1341 |
*
|
| 1342 |
* @param $field
|
| 1343 |
* The field structure being created.
|
| 1344 |
*/
|
| 1345 |
function hook_field_storage_create_field($field) {
|
| 1346 |
}
|
| 1347 |
|
| 1348 |
/**
|
| 1349 |
* Act on deletion of a field.
|
| 1350 |
*
|
| 1351 |
* @param $field
|
| 1352 |
* The field being deleted.
|
| 1353 |
*/
|
| 1354 |
function hook_field_storage_delete_field($field) {
|
| 1355 |
}
|
| 1356 |
|
| 1357 |
/**
|
| 1358 |
* Act on deletion of a field instance.
|
| 1359 |
*
|
| 1360 |
* @param $instance
|
| 1361 |
* The instance being deleted.
|
| 1362 |
*/
|
| 1363 |
function hook_field_storage_delete_instance($instance) {
|
| 1364 |
}
|
| 1365 |
|
| 1366 |
/**
|
| 1367 |
* @} End of "ingroup field_storage"
|
| 1368 |
*/
|
| 1369 |
|
| 1370 |
/**********************************************************************
|
| 1371 |
* Field CRUD API
|
| 1372 |
**********************************************************************/
|
| 1373 |
|
| 1374 |
/**
|
| 1375 |
* @ingroup field_crud
|
| 1376 |
* @{
|
| 1377 |
*/
|
| 1378 |
|
| 1379 |
/**
|
| 1380 |
* Act on a field being created.
|
| 1381 |
*
|
| 1382 |
* This hook is invoked after the field is created and so it cannot modify the
|
| 1383 |
* field itself.
|
| 1384 |
*
|
| 1385 |
* TODO: Not implemented.
|
| 1386 |
*
|
| 1387 |
* @param $field
|
| 1388 |
* The field just created.
|
| 1389 |
*/
|
| 1390 |
function hook_field_create_field($field) {
|
| 1391 |
}
|
| 1392 |
|
| 1393 |
/**
|
| 1394 |
* Act on a field instance being created.
|
| 1395 |
*
|
| 1396 |
* This hook is invoked after the instance record is saved and so it cannot
|
| 1397 |
* modify the instance itself.
|
| 1398 |
*
|
| 1399 |
* @param $instance
|
| 1400 |
* The instance just created.
|
| 1401 |
*/
|
| 1402 |
function hook_field_create_instance($instance) {
|
| 1403 |
}
|
| 1404 |
|
| 1405 |
/**
|
| 1406 |
* Forbid a field update from occurring.
|
| 1407 |
*
|
| 1408 |
* Any module may forbid any update for any reason. For example, the
|
| 1409 |
* field's storage module might forbid an update if it would change
|
| 1410 |
* the storage schema while data for the field exists. A field type
|
| 1411 |
* module might forbid an update if it would change existing data's
|
| 1412 |
* semantics, or if there are external dependencies on field settings
|
| 1413 |
* that cannot be updated.
|
| 1414 |
*
|
| 1415 |
* @param $field
|
| 1416 |
* The field as it will be post-update.
|
| 1417 |
* @param $prior_field
|
| 1418 |
* The field as it is pre-update.
|
| 1419 |
* @param $has_data
|
| 1420 |
* Whether any data already exists for this field.
|
| 1421 |
* @return
|
| 1422 |
* Throws a FieldUpdateForbiddenException to prevent the update from occuring.
|
| 1423 |
*/
|
| 1424 |
function hook_field_update_field_forbid($field, $prior_field, $has_data) {
|
| 1425 |
// A 'list' field stores integer keys mapped to display values. If
|
| 1426 |
// the new field will have fewer values, and any data exists for the
|
| 1427 |
// abandonded keys, the field will have no way to display them. So,
|
| 1428 |
// forbid such an update.
|
| 1429 |
if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) {
|
| 1430 |
// Identify the keys that will be lost.
|
| 1431 |
$lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values']));
|
| 1432 |
// If any data exist for those keys, forbid the update.
|
| 1433 |
$count = field_attach_query($prior_field['id'], array('value', $lost_keys, 'IN'), 1);
|
| 1434 |
if ($count > 0) {
|
| 1435 |
throw new FieldUpdateForbiddenException("Cannot update a list field not to include keys with existing data");
|
| 1436 |
}
|
| 1437 |
}
|
| 1438 |
}
|
| 1439 |
|
| 1440 |
/**
|
| 1441 |
* Act on a field being updated.
|
| 1442 |
*
|
| 1443 |
* This hook is invoked just after field is updated.
|
| 1444 |
*
|
| 1445 |
* @param $field
|
| 1446 |
* The field as it is post-update.
|
| 1447 |
* @param $prior_field
|
| 1448 |
* The field as it was pre-update.
|
| 1449 |
* @param $has_data
|
| 1450 |
* Whether any data already exists for this field.
|
| 1451 |
*/
|
| 1452 |
function hook_field_update_field($field, $prior_field, $has_data) {
|
| 1453 |
}
|
| 1454 |
|
| 1455 |
/**
|
| 1456 |
* Act on a field being deleted.
|
| 1457 |
*
|
| 1458 |
* This hook is invoked just after field is deleted.
|
| 1459 |
*
|
| 1460 |
* @param $field
|
| 1461 |
* The field just deleted.
|
| 1462 |
*/
|
| 1463 |
function hook_field_delete_field($field) {
|
| 1464 |
}
|
| 1465 |
|
| 1466 |
/**
|
| 1467 |
* Act on a field instance being updated.
|
| 1468 |
*
|
| 1469 |
* This hook is invoked after the instance record is saved and so it cannot
|
| 1470 |
* modify the instance itself.
|
| 1471 |
*
|
| 1472 |
* TODO: Not implemented.
|
| 1473 |
*
|
| 1474 |
* @param $instance
|
| 1475 |
* The instance just updated.
|
| 1476 |
*/
|
| 1477 |
function hook_field_update_instance($instance) {
|
| 1478 |
}
|
| 1479 |
|
| 1480 |
/**
|
| 1481 |
* Act on a field instance being deleted.
|
| 1482 |
*
|
| 1483 |
* This hook is invoked just after the instance is deleted.
|
| 1484 |
*
|
| 1485 |
* @param $instance
|
| 1486 |
* The instance just deleted.
|
| 1487 |
*/
|
| 1488 |
function hook_field_delete_instance($instance) {
|
| 1489 |
}
|
| 1490 |
|
| 1491 |
/**
|
| 1492 |
* Act on field records being read from the database.
|
| 1493 |
*
|
| 1494 |
* @param $field
|
| 1495 |
* The field record just read from the database.
|
| 1496 |
*/
|
| 1497 |
function hook_field_read_field($field) {
|
| 1498 |
}
|
| 1499 |
|
| 1500 |
/**
|
| 1501 |
* Act on a field record being read from the database.
|
| 1502 |
*
|
| 1503 |
* @param $instance
|
| 1504 |
* The instance record just read from the database.
|
| 1505 |
*/
|
| 1506 |
function hook_field_read_instance($instance) {
|
| 1507 |
}
|
| 1508 |
|
| 1509 |
/**
|
| 1510 |
* @} End of "ingroup field_crud"
|
| 1511 |
*/
|
| 1512 |
|
| 1513 |
/**********************************************************************
|
| 1514 |
* TODO: I'm not sure where these belong yet.
|
| 1515 |
**********************************************************************/
|
| 1516 |
|
| 1517 |
/**
|
| 1518 |
* TODO
|
| 1519 |
*
|
| 1520 |
* Note : Right now this belongs to the "Fieldable Type API".
|
| 1521 |
* Whether 'build modes' is actually a 'fields' concept is to be debated
|
| 1522 |
* in a separate overhaul patch for core.
|
| 1523 |
*/
|
| 1524 |
function hook_field_build_modes($obj_type) {
|
| 1525 |
}
|
| 1526 |
|
| 1527 |
/**
|
| 1528 |
* Determine whether the user has access to a given field.
|
| 1529 |
*
|
| 1530 |
* @param $op
|
| 1531 |
* The operation to be performed. Possible values:
|
| 1532 |
* - "edit"
|
| 1533 |
* - "view"
|
| 1534 |
* @param $field
|
| 1535 |
* The field on which the operation is to be performed.
|
| 1536 |
* @param $obj_type
|
| 1537 |
* The type of $object; e.g. 'node' or 'user'.
|
| 1538 |
* @param $object
|
| 1539 |
* (optional) The object for the operation.
|
| 1540 |
* @param $account
|
| 1541 |
* (optional) The account to check, if not given use currently logged in user.
|
| 1542 |
* @return
|
| 1543 |
* TRUE if the operation is allowed;
|
| 1544 |
* FALSE if the operation is denied.
|
| 1545 |
*/
|
| 1546 |
function hook_field_access($op, $field, $obj_type, $object, $account) {
|
| 1547 |
}
|