2009-02-05 03:42:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2012-03-09 16:23:34 +00:00
|
|
|
* @addtogroup hooks
|
2009-02-05 03:42:58 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-12-17 21:54:13 +00:00
|
|
|
use Drupal\Component\Utility\NestedArray;
|
2012-05-07 02:56:49 +00:00
|
|
|
use Drupal\field\FieldUpdateForbiddenException;
|
|
|
|
|
2009-08-19 13:31:14 +00:00
|
|
|
/**
|
2010-05-23 19:10:23 +00:00
|
|
|
* Exposes "pseudo-field" components on fieldable entities.
|
2009-08-19 13:31:14 +00:00
|
|
|
*
|
2010-05-23 19:10:23 +00:00
|
|
|
* Field UI's "Manage fields" and "Manage display" pages let users re-order
|
2013-01-23 17:29:29 +00:00
|
|
|
* fields, but also non-field components. For nodes, these include the title
|
|
|
|
* and other elements exposed by modules through hook_form() or hook_form_alter().
|
2009-08-19 13:31:14 +00:00
|
|
|
*
|
2010-05-23 19:10:23 +00:00
|
|
|
* Fieldable entities or modules that want to have their components supported
|
|
|
|
* should expose them using this hook. The user-defined settings (weight,
|
2012-09-27 15:44:17 +00:00
|
|
|
* visible) are automatically applied on rendered forms and displayed entities
|
|
|
|
* in a #pre_render callback added by field_attach_form() and
|
2010-05-23 19:10:23 +00:00
|
|
|
* field_attach_view().
|
|
|
|
*
|
|
|
|
* @see _field_extra_fields_pre_render()
|
|
|
|
* @see hook_field_extra_fields_alter()
|
2009-08-19 13:31:14 +00:00
|
|
|
*
|
2010-04-06 16:49:12 +00:00
|
|
|
* @return
|
2012-09-27 15:44:17 +00:00
|
|
|
* A nested array of 'pseudo-field' components. Each list is nested within the
|
|
|
|
* following keys: entity type, bundle name, context (either 'form' or
|
2010-05-23 19:10:23 +00:00
|
|
|
* 'display'). The keys are the name of the elements as appearing in the
|
|
|
|
* renderable array (either the entity form or the displayed entity). The
|
|
|
|
* value is an associative array:
|
2009-08-19 13:31:14 +00:00
|
|
|
* - label: The human readable name of the component.
|
|
|
|
* - description: A short description of the component contents.
|
|
|
|
* - weight: The default weight of the element.
|
2012-09-13 09:15:38 +00:00
|
|
|
* - visible: The default visibility of the element. Only for 'display'
|
|
|
|
* context.
|
2009-08-19 13:31:14 +00:00
|
|
|
*/
|
2010-01-02 15:00:34 +00:00
|
|
|
function hook_field_extra_fields() {
|
2013-01-23 17:29:29 +00:00
|
|
|
$extra = array();
|
|
|
|
$module_language_enabled = module_exists('language');
|
|
|
|
$description = t('Node module element');
|
|
|
|
|
|
|
|
foreach (node_type_get_types() as $bundle) {
|
|
|
|
if ($bundle->has_title) {
|
|
|
|
$extra['node'][$bundle->type]['form']['title'] = array(
|
|
|
|
'label' => $bundle->title_label,
|
|
|
|
'description' => $description,
|
|
|
|
'weight' => -5,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add also the 'language' select if Language module is enabled and the
|
|
|
|
// bundle has multilingual support.
|
|
|
|
// Visibility of the ordering of the language selector is the same as on the
|
|
|
|
// node/add form.
|
|
|
|
if ($module_language_enabled) {
|
|
|
|
$configuration = language_get_default_configuration('node', $bundle->type);
|
|
|
|
if ($configuration['language_show']) {
|
|
|
|
$extra['node'][$bundle->type]['form']['language'] = array(
|
|
|
|
'label' => t('Language'),
|
|
|
|
'description' => $description,
|
|
|
|
'weight' => 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$extra['node'][$bundle->type]['display']['language'] = array(
|
|
|
|
'label' => t('Language'),
|
|
|
|
'description' => $description,
|
|
|
|
'weight' => 0,
|
|
|
|
'visible' => FALSE,
|
|
|
|
);
|
|
|
|
}
|
2010-04-06 16:49:12 +00:00
|
|
|
|
2009-08-19 13:31:14 +00:00
|
|
|
return $extra;
|
|
|
|
}
|
|
|
|
|
2010-03-04 18:28:29 +00:00
|
|
|
/**
|
|
|
|
* Alter "pseudo-field" components on fieldable entities.
|
|
|
|
*
|
|
|
|
* @param $info
|
|
|
|
* The associative array of 'pseudo-field' components.
|
|
|
|
*
|
|
|
|
* @see hook_field_extra_fields()
|
|
|
|
*/
|
|
|
|
function hook_field_extra_fields_alter(&$info) {
|
2010-08-03 01:54:24 +00:00
|
|
|
// Force node title to always be at the top of the list by default.
|
2010-04-06 16:49:12 +00:00
|
|
|
foreach (node_type_get_types() as $bundle) {
|
2011-11-09 04:14:10 +00:00
|
|
|
if (isset($info['node'][$bundle->type]['form']['title'])) {
|
|
|
|
$info['node'][$bundle->type]['form']['title']['weight'] = -20;
|
2010-04-06 16:49:12 +00:00
|
|
|
}
|
2010-03-04 18:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
|
|
|
* @defgroup field_types Field Types API
|
|
|
|
* @{
|
2012-09-27 15:44:17 +00:00
|
|
|
* Defines field, widget, display formatter, and storage types.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2012-03-09 16:23:34 +00:00
|
|
|
* In the Field API, each field has a type, which determines what kind of data
|
|
|
|
* (integer, string, date, etc.) the field can hold, which settings it provides,
|
|
|
|
* and so on. The data type(s) accepted by a field are defined in
|
|
|
|
* hook_field_schema(); other basic properties of a field are defined in
|
|
|
|
* hook_field_info(). The other hooks below are called by the Field Attach API
|
|
|
|
* to perform field-type-specific actions.
|
2009-08-01 06:03:48 +00:00
|
|
|
*
|
|
|
|
* The Field Types API also defines two kinds of pluggable handlers: widgets
|
2012-03-09 16:23:34 +00:00
|
|
|
* and formatters. @link field_widget Widgets @endlink specify how the field
|
|
|
|
* appears in edit forms, while @link field_formatter formatters @endlink
|
|
|
|
* specify how the field appears in displayed entities.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* A third kind of pluggable handler, storage backends, is defined by the
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* @link field_storage Field Storage API @endlink.
|
2011-11-30 02:42:42 +00:00
|
|
|
*
|
2011-12-22 09:32:53 +00:00
|
|
|
* See @link field Field API @endlink for information about the other parts of
|
|
|
|
* the Field API.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define Field API field types.
|
|
|
|
*
|
|
|
|
* @return
|
2009-08-01 06:03:48 +00:00
|
|
|
* An array whose keys are field type names and whose values are arrays
|
|
|
|
* describing the field type, with the following key/value pairs:
|
|
|
|
* - label: The human-readable name of the field type.
|
|
|
|
* - description: A short description for the field type.
|
|
|
|
* - settings: An array whose keys are the names of the settings available
|
|
|
|
* for the field type, and whose values are the default values for those
|
|
|
|
* settings.
|
|
|
|
* - instance_settings: An array whose keys are the names of the settings
|
|
|
|
* available for instances of the field type, and whose values are the
|
2010-05-03 07:37:51 +00:00
|
|
|
* default values for those settings. Instance-level settings can have
|
|
|
|
* different values on each field instance, and thus allow greater
|
|
|
|
* flexibility than field-level settings. It is recommended to put settings
|
|
|
|
* at the instance level whenever possible. Notable exceptions: settings
|
|
|
|
* acting on the schema definition, or settings that Views needs to use
|
|
|
|
* across field instances (for example, the list of allowed values).
|
2009-08-01 06:03:48 +00:00
|
|
|
* - default_widget: The machine name of the default widget to be used by
|
2012-09-27 15:44:17 +00:00
|
|
|
* instances of this field type, when no widget is specified in the instance
|
|
|
|
* definition. This widget must be available whenever the field type is
|
|
|
|
* available (i.e. provided by the field type module, or by a module the
|
|
|
|
* field type module depends on).
|
2009-08-01 06:03:48 +00:00
|
|
|
* - default_formatter: The machine name of the default formatter to be used
|
|
|
|
* by instances of this field type, when no formatter is specified in the
|
|
|
|
* instance definition. This formatter must be available whenever the field
|
|
|
|
* type is available (i.e. provided by the field type module, or by a module
|
|
|
|
* the field type module depends on).
|
2010-05-18 18:30:49 +00:00
|
|
|
* - no_ui: (optional) A boolean specifying that users should not be allowed
|
|
|
|
* to create fields and instances of this field type through the UI. Such
|
|
|
|
* fields can only be created programmatically with field_create_field()
|
|
|
|
* and field_create_instance(). Defaults to FALSE.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_info_alter()
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
function hook_field_info() {
|
|
|
|
return array(
|
|
|
|
'text' => array(
|
|
|
|
'label' => t('Text'),
|
|
|
|
'description' => t('This field stores varchar text in the database.'),
|
|
|
|
'settings' => array('max_length' => 255),
|
|
|
|
'instance_settings' => array('text_processing' => 0),
|
|
|
|
'default_widget' => 'text_textfield',
|
|
|
|
'default_formatter' => 'text_default',
|
|
|
|
),
|
2009-08-01 06:03:48 +00:00
|
|
|
'text_long' => array(
|
|
|
|
'label' => t('Long text'),
|
2009-02-05 03:42:58 +00:00
|
|
|
'description' => t('This field stores long text in the database.'),
|
2009-08-01 06:03:48 +00:00
|
|
|
'settings' => array('max_length' => ''),
|
2009-02-05 03:42:58 +00:00
|
|
|
'instance_settings' => array('text_processing' => 0),
|
|
|
|
'default_widget' => 'text_textarea',
|
|
|
|
'default_formatter' => 'text_default',
|
|
|
|
),
|
2009-08-01 06:03:48 +00:00
|
|
|
'text_with_summary' => array(
|
|
|
|
'label' => t('Long text and summary'),
|
|
|
|
'description' => t('This field stores long text in the database along with optional summary text.'),
|
|
|
|
'settings' => array('max_length' => ''),
|
|
|
|
'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
|
|
|
|
'default_widget' => 'text_textarea_with_summary',
|
|
|
|
'default_formatter' => 'text_summary_or_trimmed',
|
|
|
|
),
|
2009-02-05 03:42:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-06-30 03:12:03 +00:00
|
|
|
/**
|
|
|
|
* Perform alterations on Field API field types.
|
|
|
|
*
|
|
|
|
* @param $info
|
2010-05-03 07:37:51 +00:00
|
|
|
* Array of information on field types exposed by hook_field_info()
|
2009-06-30 03:12:03 +00:00
|
|
|
* implementations.
|
|
|
|
*/
|
|
|
|
function hook_field_info_alter(&$info) {
|
|
|
|
// Add a setting to all field types.
|
|
|
|
foreach ($info as $field_type => $field_type_info) {
|
2009-07-30 19:35:47 +00:00
|
|
|
$info[$field_type]['settings'] += array(
|
|
|
|
'mymodule_additional_setting' => 'default value',
|
|
|
|
);
|
2009-06-30 03:12:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change the default widget for fields of type 'foo'.
|
|
|
|
if (isset($info['foo'])) {
|
|
|
|
$info['foo']['default widget'] = 'mymodule_widget';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
|
|
|
* Define the Field API schema for a field structure.
|
|
|
|
*
|
2010-09-04 15:40:52 +00:00
|
|
|
* This hook MUST be defined in .install for it to be detected during
|
|
|
|
* installation and upgrade.
|
|
|
|
*
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* A field structure.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2009-02-05 03:42:58 +00:00
|
|
|
* @return
|
2009-05-20 09:48:47 +00:00
|
|
|
* An associative array with the following keys:
|
2010-05-03 07:37:51 +00:00
|
|
|
* - columns: An array of Schema API column specifications, keyed by column
|
|
|
|
* name. This specifies what comprises a value for a given field. For
|
|
|
|
* example, a value for a number field is simply 'value', while a value for
|
|
|
|
* a formatted text field is the combination of 'value' and 'format'. It is
|
|
|
|
* recommended to avoid having the column definitions depend on field
|
|
|
|
* settings when possible. No assumptions should be made on how storage
|
|
|
|
* engines internally use the original column name to structure their
|
|
|
|
* storage.
|
2012-09-27 15:44:17 +00:00
|
|
|
* - indexes: (optional) An array of Schema API index definitions. Only
|
2010-11-07 22:06:16 +00:00
|
|
|
* columns that appear in the 'columns' array are allowed. Those indexes
|
|
|
|
* will be used as default indexes. Callers of field_create_field() can
|
2012-09-27 15:44:17 +00:00
|
|
|
* specify additional indexes or, at their own risk, modify the default
|
2010-11-07 22:06:16 +00:00
|
|
|
* indexes specified by the field-type module. Some storage engines might
|
|
|
|
* not support indexes.
|
2012-09-27 15:44:17 +00:00
|
|
|
* - foreign keys: (optional) An array of Schema API foreign key definitions.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2009-05-20 09:48:47 +00:00
|
|
|
function hook_field_schema($field) {
|
|
|
|
if ($field['type'] == 'text_long') {
|
2009-02-05 03:42:58 +00:00
|
|
|
$columns = array(
|
|
|
|
'value' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'size' => 'big',
|
|
|
|
'not null' => FALSE,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$columns = array(
|
|
|
|
'value' => array(
|
|
|
|
'type' => 'varchar',
|
|
|
|
'length' => $field['settings']['max_length'],
|
|
|
|
'not null' => FALSE,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$columns += array(
|
|
|
|
'format' => array(
|
2011-05-31 04:07:26 +00:00
|
|
|
'type' => 'varchar',
|
|
|
|
'length' => 255,
|
2009-02-05 03:42:58 +00:00
|
|
|
'not null' => FALSE,
|
|
|
|
),
|
|
|
|
);
|
2009-05-20 09:48:47 +00:00
|
|
|
return array(
|
|
|
|
'columns' => $columns,
|
|
|
|
'indexes' => array(
|
|
|
|
'format' => array('format'),
|
|
|
|
),
|
2010-11-07 22:06:16 +00:00
|
|
|
'foreign keys' => array(
|
|
|
|
'format' => array(
|
|
|
|
'table' => 'filter_format',
|
|
|
|
'columns' => array('format' => 'format'),
|
|
|
|
),
|
|
|
|
),
|
2009-05-20 09:48:47 +00:00
|
|
|
);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Define custom load behavior for this module's field types.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-12 05:38:10 +00:00
|
|
|
* Unlike most other field hooks, this hook operates on multiple entities. The
|
2010-05-03 07:37:51 +00:00
|
|
|
* $entities, $instances and $items parameters are arrays keyed by entity ID.
|
2010-02-12 05:38:10 +00:00
|
|
|
* For performance reasons, information for all available entity should be
|
2009-05-17 00:32:29 +00:00
|
|
|
* loaded in a single query where possible.
|
|
|
|
*
|
2009-10-16 03:21:23 +00:00
|
|
|
* Note that the changes made to the field values get cached by the field cache
|
|
|
|
* for subsequent loads. You should never use this hook to load fieldable
|
|
|
|
* entities, since this is likely to cause infinite recursions when
|
|
|
|
* hook_field_load() is run on those as well. Use
|
|
|
|
* hook_field_formatter_prepare_view() instead.
|
2009-05-17 00:32:29 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* Make changes or additions to field values by altering the $items parameter by
|
|
|
|
* reference. There is no return value.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity.
|
|
|
|
* @param $entities
|
2010-05-03 07:37:51 +00:00
|
|
|
* Array of entities being loaded, keyed by entity ID.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
2009-05-17 00:32:29 +00:00
|
|
|
* @param $instances
|
2010-02-12 05:38:10 +00:00
|
|
|
* Array of instance structures for $field for each entity, keyed by entity
|
2010-05-03 07:37:51 +00:00
|
|
|
* ID.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2010-05-03 07:37:51 +00:00
|
|
|
* The language code associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-05-03 07:37:51 +00:00
|
|
|
* Array of field values already loaded for the entities, keyed by entity ID.
|
|
|
|
* Store your changes in this parameter (passed by reference).
|
2009-05-17 00:32:29 +00:00
|
|
|
* @param $age
|
|
|
|
* FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
|
2010-02-12 05:38:10 +00:00
|
|
|
* FIELD_LOAD_REVISION to load the version indicated by each entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
|
2009-12-13 12:42:28 +00:00
|
|
|
// Sample code from text.module: precompute sanitized strings so they are
|
|
|
|
// stored in the field cache.
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach ($entities as $id => $entity) {
|
2009-08-01 06:03:48 +00:00
|
|
|
foreach ($items[$id] as $delta => $item) {
|
2009-12-13 12:42:28 +00:00
|
|
|
// Only process items with a cacheable format, the rest will be handled
|
|
|
|
// by formatters if needed.
|
|
|
|
if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) {
|
|
|
|
$items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : '';
|
2009-08-01 06:03:48 +00:00
|
|
|
if ($field['type'] == 'text_with_summary') {
|
2009-12-13 12:42:28 +00:00
|
|
|
$items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : '';
|
2009-08-01 06:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Prepare field values prior to display.
|
2009-08-01 06:03:48 +00:00
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* This hook is invoked before the field values are handed to formatters for
|
|
|
|
* display, and runs before the formatters' own
|
2009-12-13 12:42:28 +00:00
|
|
|
* hook_field_formatter_prepare_view().
|
|
|
|
*
|
2010-02-12 05:38:10 +00:00
|
|
|
* Unlike most other field hooks, this hook operates on multiple entities. The
|
2010-05-03 07:37:51 +00:00
|
|
|
* $entities, $instances and $items parameters are arrays keyed by entity ID.
|
2010-02-12 05:38:10 +00:00
|
|
|
* For performance reasons, information for all available entities should be
|
2009-12-13 12:42:28 +00:00
|
|
|
* loaded in a single query where possible.
|
2009-08-01 06:03:48 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* Make changes or additions to field values by altering the $items parameter by
|
|
|
|
* reference. There is no return value.
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The type of $entity.
|
|
|
|
* @param $entities
|
2010-05-03 07:37:51 +00:00
|
|
|
* Array of entities being displayed, keyed by entity ID.
|
2009-08-01 06:03:48 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
2009-12-13 12:42:28 +00:00
|
|
|
* @param $instances
|
2010-02-12 05:38:10 +00:00
|
|
|
* Array of instance structures for $field for each entity, keyed by entity
|
2010-05-03 07:37:51 +00:00
|
|
|
* ID.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2012-09-27 15:44:17 +00:00
|
|
|
* The language associated with $items.
|
2009-08-01 06:03:48 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}, or an empty array if unset.
|
2009-08-01 06:03:48 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
|
2009-12-13 12:42:28 +00:00
|
|
|
// Sample code from image.module: if there are no images specified at all,
|
2010-05-03 07:37:51 +00:00
|
|
|
// use the default image.
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach ($entities as $id => $entity) {
|
2009-12-13 12:42:28 +00:00
|
|
|
if (empty($items[$id]) && $field['settings']['default_image']) {
|
|
|
|
if ($file = file_load($field['settings']['default_image'])) {
|
|
|
|
$items[$id][0] = (array) $file + array(
|
|
|
|
'is_default' => TRUE,
|
|
|
|
'alt' => '',
|
|
|
|
'title' => '',
|
|
|
|
);
|
2009-08-01 06:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Validate this module's field data.
|
|
|
|
*
|
|
|
|
* If there are validation problems, add to the $errors array (passed by
|
|
|
|
* reference). There is no return value.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
|
|
|
* @param $instance
|
2010-02-11 17:44:47 +00:00
|
|
|
* The instance structure for $field on $entity's bundle.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2010-05-03 07:37:51 +00:00
|
|
|
* The language associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
2009-03-26 13:31:28 +00:00
|
|
|
* @param $errors
|
2010-12-02 00:13:47 +00:00
|
|
|
* The array of errors (keyed by field name, language code, and delta) that
|
|
|
|
* have already been reported for the entity. The function should add its
|
|
|
|
* errors to this array. Each error is an associative array with the following
|
2009-03-26 13:31:28 +00:00
|
|
|
* keys and values:
|
2010-12-02 00:13:47 +00:00
|
|
|
* - error: An error code (should be a string prefixed with the module name).
|
2012-09-27 15:44:17 +00:00
|
|
|
* - message: The human-readable message to be displayed.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_validate(\Drupal\Core\Entity\EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) {
|
2009-03-26 13:31:28 +00:00
|
|
|
foreach ($items as $delta => $item) {
|
|
|
|
if (!empty($item['value'])) {
|
|
|
|
if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) {
|
2010-12-02 00:13:47 +00:00
|
|
|
$errors[$field['field_name']][$langcode][$delta][] = array(
|
2009-03-26 13:31:28 +00:00
|
|
|
'error' => 'text_max_length',
|
|
|
|
'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define custom presave behavior for this module's field types.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* Make changes or additions to field values by altering the $items parameter by
|
|
|
|
* reference. There is no return value.
|
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
|
|
|
* @param $instance
|
2010-02-11 17:44:47 +00:00
|
|
|
* The instance structure for $field on $entity's bundle.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2010-05-03 07:37:51 +00:00
|
|
|
* The language associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_presave(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) {
|
2009-11-12 21:03:36 +00:00
|
|
|
if ($field['type'] == 'number_decimal') {
|
|
|
|
// Let PHP round the value to ensure consistent behavior across storage
|
|
|
|
// backends.
|
|
|
|
foreach ($items as $delta => $item) {
|
|
|
|
if (isset($item['value'])) {
|
|
|
|
$items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-30 21:45:46 +00:00
|
|
|
* Define custom insert behavior for this module's field data.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2012-03-30 21:45:46 +00:00
|
|
|
* This hook is invoked from field_attach_insert() on the module that defines a
|
|
|
|
* field, during the process of inserting an entity object (node, taxonomy term,
|
|
|
|
* etc.). It is invoked just before the data for this field on the particular
|
|
|
|
* entity object is inserted into field storage. Only field modules that are
|
|
|
|
* storing or tracking information outside the standard field storage mechanism
|
|
|
|
* need to implement this hook.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
|
|
|
* @param $instance
|
2010-02-11 17:44:47 +00:00
|
|
|
* The instance structure for $field on $entity's bundle.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2010-05-03 07:37:51 +00:00
|
|
|
* The language associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
2012-03-30 21:45:46 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_update()
|
|
|
|
* @see hook_field_delete()
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_insert(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) {
|
|
|
|
if (config('taxonomy.settings')->get('maintain_index_table') && $field['storage']['type'] == 'field_sql_storage' && $entity->entityType() == 'node' && $entity->status) {
|
2010-07-25 02:45:27 +00:00
|
|
|
$query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', ));
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$query->values(array(
|
|
|
|
'nid' => $entity->nid,
|
|
|
|
'tid' => $item['tid'],
|
|
|
|
'sticky' => $entity->sticky,
|
|
|
|
'created' => $entity->created,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$query->execute();
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-30 21:45:46 +00:00
|
|
|
* Define custom update behavior for this module's field data.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2012-03-30 21:45:46 +00:00
|
|
|
* This hook is invoked from field_attach_update() on the module that defines a
|
|
|
|
* field, during the process of updating an entity object (node, taxonomy term,
|
|
|
|
* etc.). It is invoked just before the data for this field on the particular
|
|
|
|
* entity object is updated into field storage. Only field modules that are
|
|
|
|
* storing or tracking information outside the standard field storage mechanism
|
|
|
|
* need to implement this hook.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
|
|
|
* @param $instance
|
2010-02-11 17:44:47 +00:00
|
|
|
* The instance structure for $field on $entity's bundle.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2010-05-03 07:37:51 +00:00
|
|
|
* The language associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
2012-03-30 21:45:46 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_insert()
|
|
|
|
* @see hook_field_delete()
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_update(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) {
|
|
|
|
if (config('taxonomy.settings')->get('maintain_index_table') && $field['storage']['type'] == 'field_sql_storage' && $entity->entityType() == 'node') {
|
2010-07-25 02:45:27 +00:00
|
|
|
$first_call = &drupal_static(__FUNCTION__, array());
|
|
|
|
|
|
|
|
// We don't maintain data for old revisions, so clear all previous values
|
|
|
|
// from the table. Since this hook runs once per field, per object, make
|
|
|
|
// sure we only wipe values once.
|
|
|
|
if (!isset($first_call[$entity->nid])) {
|
|
|
|
$first_call[$entity->nid] = FALSE;
|
|
|
|
db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
|
|
|
|
}
|
|
|
|
// Only save data to the table if the node is published.
|
|
|
|
if ($entity->status) {
|
|
|
|
$query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$query->values(array(
|
|
|
|
'nid' => $entity->nid,
|
|
|
|
'tid' => $item['tid'],
|
|
|
|
'sticky' => $entity->sticky,
|
|
|
|
'created' => $entity->created,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 00:06:44 +00:00
|
|
|
/**
|
|
|
|
* Update the storage information for a field.
|
|
|
|
*
|
|
|
|
* This is invoked on the field's storage module from field_update_field(),
|
|
|
|
* before the new field information is saved to the database. The field storage
|
|
|
|
* module should update its storage tables to agree with the new field
|
|
|
|
* information. If there is a problem, the field storage module should throw an
|
|
|
|
* exception.
|
|
|
|
*
|
|
|
|
* @param $field
|
|
|
|
* The updated field structure to be saved.
|
|
|
|
* @param $prior_field
|
|
|
|
* The previously-saved field structure.
|
|
|
|
* @param $has_data
|
|
|
|
* TRUE if the field has data in storage currently.
|
|
|
|
*/
|
|
|
|
function hook_field_storage_update_field($field, $prior_field, $has_data) {
|
|
|
|
if (!$has_data) {
|
|
|
|
// There is no data. Re-create the tables completely.
|
|
|
|
$prior_schema = _field_sql_storage_schema($prior_field);
|
|
|
|
foreach ($prior_schema as $name => $table) {
|
|
|
|
db_drop_table($name, $table);
|
|
|
|
}
|
|
|
|
$schema = _field_sql_storage_schema($field);
|
|
|
|
foreach ($schema as $name => $table) {
|
|
|
|
db_create_table($name, $table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// There is data. See field_sql_storage_field_storage_update_field() for
|
|
|
|
// an example of what to do to modify the schema in place, preserving the
|
|
|
|
// old data as much as possible.
|
|
|
|
}
|
|
|
|
drupal_get_schema(NULL, TRUE);
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2012-03-30 21:45:46 +00:00
|
|
|
* Define custom delete behavior for this module's field data.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2012-03-30 21:45:46 +00:00
|
|
|
* This hook is invoked from field_attach_delete() on the module that defines a
|
|
|
|
* field, during the process of deleting an entity object (node, taxonomy term,
|
|
|
|
* etc.). It is invoked just before the data for this field on the particular
|
|
|
|
* entity object is deleted from field storage. Only field modules that are
|
|
|
|
* storing or tracking information outside the standard field storage mechanism
|
|
|
|
* need to implement this hook.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
|
|
|
* @param $instance
|
2010-02-11 17:44:47 +00:00
|
|
|
* The instance structure for $field on $entity's bundle.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2010-05-03 07:37:51 +00:00
|
|
|
* The language associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
2012-03-30 21:45:46 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_insert()
|
|
|
|
* @see hook_field_update()
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_delete(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) {
|
|
|
|
// Delete all file usages within this entity.
|
2010-07-25 02:45:27 +00:00
|
|
|
foreach ($items as $delta => $item) {
|
2013-01-07 11:22:28 +00:00
|
|
|
file_usage()->delete(file_load($item['fid']), 'file', $entity->entityType(), $entity->id(), 0);
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Define custom revision delete behavior for this module's field types.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* This hook is invoked just before the data is deleted from field storage in
|
|
|
|
* field_attach_delete_revision(), and will only be called for fieldable types
|
|
|
|
* that are versioned.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
|
|
|
* @param $instance
|
2010-02-11 17:44:47 +00:00
|
|
|
* The instance structure for $field on $entity's bundle.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2010-05-03 07:37:51 +00:00
|
|
|
* The language associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_delete_revision(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items) {
|
2010-07-25 02:45:27 +00:00
|
|
|
foreach ($items as $delta => $item) {
|
2013-01-07 11:22:28 +00:00
|
|
|
// Decrement the file usage count by 1.
|
|
|
|
file_usage()->delete(file_load($item['fid']), 'file', $entity->entityType(), $entity->id());
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-01 06:03:48 +00:00
|
|
|
* Define custom prepare_translation behavior for this module's field types.
|
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure for the operation.
|
|
|
|
* @param $instance
|
2010-02-11 17:44:47 +00:00
|
|
|
* The instance structure for $field on $entity's bundle.
|
2009-08-22 00:58:55 +00:00
|
|
|
* @param $langcode
|
2012-09-27 15:44:17 +00:00
|
|
|
* The language associated with $items.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $items
|
2010-02-11 17:44:47 +00:00
|
|
|
* $entity->{$field['field_name']}[$langcode], or an empty array if unset.
|
2010-06-06 00:24:16 +00:00
|
|
|
* @param $source_entity
|
|
|
|
* The source entity from which field values are being copied.
|
|
|
|
* @param $source_langcode
|
|
|
|
* The source language from which field values are being copied.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_prepare_translation(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
|
2010-06-06 00:24:16 +00:00
|
|
|
// If the translating user is not permitted to use the assigned text format,
|
|
|
|
// we must not expose the source values.
|
|
|
|
$field_name = $field['field_name'];
|
|
|
|
$formats = filter_formats();
|
|
|
|
$format_id = $source_entity->{$field_name}[$source_langcode][0]['format'];
|
|
|
|
if (!filter_access($formats[$format_id])) {
|
|
|
|
$items = array();
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-08-14 05:16:26 +00:00
|
|
|
/**
|
|
|
|
* Define what constitutes an empty item for a field type.
|
|
|
|
*
|
|
|
|
* @param $item
|
|
|
|
* An item that may or may not be empty.
|
|
|
|
* @param $field
|
|
|
|
* The field to which $item belongs.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2009-08-14 05:16:26 +00:00
|
|
|
* @return
|
2012-09-27 15:44:17 +00:00
|
|
|
* TRUE if $field's type considers $item not to contain any data; FALSE
|
|
|
|
* otherwise.
|
2009-08-14 05:16:26 +00:00
|
|
|
*/
|
|
|
|
function hook_field_is_empty($item, $field) {
|
2010-05-06 05:59:31 +00:00
|
|
|
if (empty($item['value']) && (string) $item['value'] !== '0') {
|
2009-08-14 05:16:26 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "defgroup field_types".
|
2012-03-09 16:23:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @defgroup field_widget Field Widget API
|
|
|
|
* @{
|
|
|
|
* Define Field API widget types.
|
|
|
|
*
|
|
|
|
* Field API widgets specify how fields are displayed in edit forms. Fields of a
|
|
|
|
* given @link field_types field type @endlink may be edited using more than one
|
|
|
|
* widget. In this case, the Field UI module allows the site builder to choose
|
2012-09-26 19:39:39 +00:00
|
|
|
* which widget to use.
|
|
|
|
*
|
|
|
|
* Widgets are Plugins managed by the
|
|
|
|
* Drupal\field\Plugin\Type\Widget\WidgetPluginManager class. A widget is
|
|
|
|
* implemented by providing a class that implements
|
|
|
|
* Drupal\field\Plugin\Type\Widget\WidgetInterface (in most cases, by
|
|
|
|
* subclassing Drupal\field\Plugin\Type\Widget\WidgetBase), and provides the
|
|
|
|
* proper annotation block.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2012-05-04 20:07:43 +00:00
|
|
|
* Widgets are @link forms_api_reference.html Form API @endlink
|
2012-09-26 19:39:39 +00:00
|
|
|
* elements with additional processing capabilities. The methods of the
|
|
|
|
* WidgetInterface object are typically called by the Field Attach API during
|
|
|
|
* the creation of the field form structure with field_attach_form().
|
2012-03-09 16:23:34 +00:00
|
|
|
*
|
|
|
|
* @see field
|
|
|
|
* @see field_types
|
|
|
|
* @see field_formatter
|
|
|
|
*/
|
|
|
|
|
2009-08-01 06:03:48 +00:00
|
|
|
/**
|
|
|
|
* Perform alterations on Field API widget types.
|
|
|
|
*
|
2012-09-26 19:39:39 +00:00
|
|
|
* @param array $info
|
|
|
|
* An array of informations on existing widget types, as collected by the
|
|
|
|
* annotation discovery mechanism.
|
2009-08-01 06:03:48 +00:00
|
|
|
*/
|
2012-09-26 19:39:39 +00:00
|
|
|
function hook_field_widget_info_alter(array &$info) {
|
2009-08-01 06:03:48 +00:00
|
|
|
// Add a setting to a widget type.
|
|
|
|
$info['text_textfield']['settings'] += array(
|
|
|
|
'mymodule_additional_setting' => 'default value',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Let a new field type re-use an existing widget.
|
2012-09-26 19:39:39 +00:00
|
|
|
$info['options_select']['field_types'][] = 'my_field_type';
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2011-08-29 18:00:33 +00:00
|
|
|
/**
|
|
|
|
* Alter forms for field widgets provided by other modules.
|
|
|
|
*
|
|
|
|
* @param $element
|
|
|
|
* The field widget form element as constructed by hook_field_widget_form().
|
|
|
|
* @param $form_state
|
|
|
|
* An associative array containing the current state of the form.
|
|
|
|
* @param $context
|
|
|
|
* An associative array containing the following key-value pairs, matching the
|
|
|
|
* arguments received by hook_field_widget_form():
|
2012-04-17 22:13:57 +00:00
|
|
|
* - form: The form structure to which widgets are being attached. This may be
|
|
|
|
* a full form structure, or a sub-element of a larger form.
|
|
|
|
* - field: The field structure.
|
|
|
|
* - instance: The field instance structure.
|
|
|
|
* - langcode: The language associated with $items.
|
|
|
|
* - items: Array of default values for this field.
|
|
|
|
* - delta: The order of this item in the array of subelements (0, 1, 2, etc).
|
2012-08-15 14:20:02 +00:00
|
|
|
* - default: A boolean indicating whether the form is being shown as a dummy
|
|
|
|
* form to set default values.
|
2011-08-29 18:00:33 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_widget_form()
|
2012-04-17 22:13:57 +00:00
|
|
|
* @see hook_field_widget_WIDGET_TYPE_form_alter()
|
2011-08-29 18:00:33 +00:00
|
|
|
*/
|
|
|
|
function hook_field_widget_form_alter(&$element, &$form_state, $context) {
|
|
|
|
// Add a css class to widget form elements for all fields of type mytype.
|
|
|
|
if ($context['field']['type'] == 'mytype') {
|
|
|
|
// Be sure not to overwrite existing attributes.
|
|
|
|
$element['#attributes']['class'][] = 'myclass';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alter widget forms for a specific widget provided by another module.
|
|
|
|
*
|
|
|
|
* Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
|
|
|
|
* specific widget form, rather than using hook_field_widget_form_alter() and
|
|
|
|
* checking the widget type.
|
|
|
|
*
|
|
|
|
* @param $element
|
|
|
|
* The field widget form element as constructed by hook_field_widget_form().
|
|
|
|
* @param $form_state
|
|
|
|
* An associative array containing the current state of the form.
|
|
|
|
* @param $context
|
|
|
|
* An associative array containing the following key-value pairs, matching the
|
|
|
|
* arguments received by hook_field_widget_form():
|
|
|
|
* - "form": The form structure where widgets are being attached to. This
|
|
|
|
* might be a full form structure, or a sub-element of a larger form.
|
|
|
|
* - "field": The field structure.
|
|
|
|
* - "instance": The field instance structure.
|
|
|
|
* - "langcode": The language associated with $items.
|
|
|
|
* - "items": Array of default values for this field.
|
|
|
|
* - "delta": The order of this item in the array of subelements (0, 1, 2,
|
|
|
|
* etc).
|
2012-08-15 14:20:02 +00:00
|
|
|
* - default: A boolean indicating whether the form is being shown as a dummy
|
|
|
|
* form to set default values.
|
2011-08-29 18:00:33 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_widget_form()
|
|
|
|
* @see hook_field_widget_form_alter()
|
|
|
|
*/
|
|
|
|
function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) {
|
|
|
|
// Code here will only act on widgets of type WIDGET_TYPE. For example,
|
|
|
|
// hook_field_widget_mymodule_autocomplete_form_alter() will only act on
|
|
|
|
// widgets of type 'mymodule_autocomplete'.
|
|
|
|
$element['#autocomplete_path'] = 'mymodule/autocomplete_path';
|
|
|
|
}
|
|
|
|
|
2012-07-17 23:25:48 +00:00
|
|
|
/**
|
|
|
|
* Alters the widget properties of a field instance before it gets displayed.
|
|
|
|
*
|
|
|
|
* Note that instead of hook_field_widget_properties_alter(), which is called
|
|
|
|
* for all fields on all entity types,
|
|
|
|
* hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget
|
|
|
|
* properties for fields on a specific entity type only.
|
|
|
|
*
|
|
|
|
* This hook is called once per field per added or edit entity. If the result
|
|
|
|
* of the hook involves reading from the database, it is highly recommended to
|
|
|
|
* statically cache the information.
|
|
|
|
*
|
2012-09-26 19:39:39 +00:00
|
|
|
* @param array $widget_properties
|
2012-07-17 23:25:48 +00:00
|
|
|
* The instance's widget properties.
|
2012-09-26 19:39:39 +00:00
|
|
|
* @param array $context
|
2012-07-17 23:25:48 +00:00
|
|
|
* An associative array containing:
|
2012-10-14 06:04:15 +00:00
|
|
|
* - entity_type: The entity type, e.g., 'node' or 'user'.
|
|
|
|
* - bundle: The bundle, e.g., 'page' or 'article'.
|
2012-07-17 23:25:48 +00:00
|
|
|
* - field: The field that the widget belongs to.
|
|
|
|
* - instance: The instance of the field.
|
|
|
|
*
|
|
|
|
* @see hook_field_widget_properties_ENTITY_TYPE_alter()
|
|
|
|
*/
|
2012-09-26 19:39:39 +00:00
|
|
|
function hook_field_widget_properties_alter(array &$widget_properties, array $context) {
|
2012-07-17 23:25:48 +00:00
|
|
|
// Change a widget's type according to the time of day.
|
|
|
|
$field = $context['field'];
|
|
|
|
if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') {
|
|
|
|
$time = date('H');
|
2012-09-26 19:39:39 +00:00
|
|
|
$widget_properties['type'] = $time < 12 ? 'widget_am' : 'widget_pm';
|
2012-07-17 23:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 16:23:34 +00:00
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "defgroup field_widget".
|
2012-03-09 16:23:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @defgroup field_formatter Field Formatter API
|
|
|
|
* @{
|
|
|
|
* Define Field API formatter types.
|
|
|
|
*
|
|
|
|
* Field API formatters specify how fields are displayed when the entity to
|
|
|
|
* which the field is attached is displayed. Fields of a given
|
|
|
|
* @link field_types field type @endlink may be displayed using more than one
|
|
|
|
* formatter. In this case, the Field UI module allows the site builder to
|
2012-10-14 06:04:15 +00:00
|
|
|
* choose which formatter to use.
|
|
|
|
*
|
|
|
|
* Formatters are Plugins managed by the
|
|
|
|
* Drupal\field\Plugin\Type\Formatter\FormatterPluginManager class. A formatter
|
|
|
|
* is implemented by providing a class that implements
|
|
|
|
* Drupal\field\Plugin\Type\Formatter\FormatterInterface (in most cases, by
|
|
|
|
* subclassing Drupal\field\Plugin\Type\Formatter\FormatterBase), and provides
|
|
|
|
* the proper annotation block.
|
2012-03-09 16:23:34 +00:00
|
|
|
*
|
|
|
|
* @see field
|
|
|
|
* @see field_types
|
|
|
|
* @see field_widget
|
|
|
|
*/
|
|
|
|
|
2009-08-01 06:03:48 +00:00
|
|
|
/**
|
|
|
|
* Perform alterations on Field API formatter types.
|
|
|
|
*
|
2012-10-14 06:04:15 +00:00
|
|
|
* @param array $info
|
|
|
|
* An array of informations on existing formatter types, as collected by the
|
|
|
|
* annotation discovery mechanism.
|
2009-08-01 06:03:48 +00:00
|
|
|
*/
|
2012-10-14 06:04:15 +00:00
|
|
|
function hook_field_formatter_info_alter(array &$info) {
|
2009-08-01 06:03:48 +00:00
|
|
|
// Add a setting to a formatter type.
|
|
|
|
$info['text_default']['settings'] += array(
|
|
|
|
'mymodule_additional_setting' => 'default value',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Let a new field type re-use an existing formatter.
|
|
|
|
$info['text_default']['field types'][] = 'my_field_type';
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "defgroup field_formatter".
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @addtogroup field_attach
|
2009-02-05 03:42:58 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-11-20 19:57:01 +00:00
|
|
|
* Act on field_attach_form().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2010-05-03 07:37:51 +00:00
|
|
|
* Implementing modules should alter the $form or $form_state parameters.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-11-20 19:57:01 +00:00
|
|
|
* The entity for which an edit form is being built.
|
2010-05-03 07:37:51 +00:00
|
|
|
* @param $form
|
2012-09-27 15:44:17 +00:00
|
|
|
* The form structure field elements are attached to. This might be a full
|
|
|
|
* form structure, or a sub-element of a larger form. The $form['#parents']
|
|
|
|
* property can be used to identify the corresponding part of
|
|
|
|
* $form_state['values']. Hook implementations that need to act on the
|
2010-11-20 19:57:01 +00:00
|
|
|
* top-level properties of the global form (like #submit, #validate...) can
|
|
|
|
* add a #process callback to the array received in the $form parameter, and
|
|
|
|
* act on the $complete_form parameter in the process callback.
|
2010-05-03 07:37:51 +00:00
|
|
|
* @param $form_state
|
|
|
|
* An associative array containing the current state of the form.
|
|
|
|
* @param $langcode
|
2012-09-27 15:44:17 +00:00
|
|
|
* The language the field values are going to be entered in. If no language is
|
|
|
|
* provided the default site language will be used.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_form(\Drupal\Core\Entity\EntityInterface $entity, &$form, &$form_state, $langcode) {
|
2010-11-20 19:57:01 +00:00
|
|
|
// Add a checkbox allowing a given field to be emptied.
|
|
|
|
// See hook_field_attach_submit() for the corresponding processing code.
|
|
|
|
$form['empty_field_foo'] = array(
|
|
|
|
'#type' => 'checkbox',
|
|
|
|
'#title' => t("Empty the 'field_foo' field"),
|
|
|
|
);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_load().
|
2009-03-30 03:44:55 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2009-05-17 00:32:29 +00:00
|
|
|
* Unlike other field_attach hooks, this hook accounts for 'multiple loads'.
|
2010-02-12 05:38:10 +00:00
|
|
|
* Instead of the usual $entity parameter, it accepts an array of entities,
|
2010-05-03 07:37:51 +00:00
|
|
|
* indexed by entity ID. For performance reasons, information for all available
|
2010-02-12 05:38:10 +00:00
|
|
|
* entities should be loaded in a single query where possible.
|
2009-05-17 00:32:29 +00:00
|
|
|
*
|
2010-02-12 05:38:10 +00:00
|
|
|
* The changes made to the entities' field values get cached by the field cache
|
2009-06-06 16:17:30 +00:00
|
|
|
* for subsequent loads.
|
2009-05-17 00:32:29 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* See field_attach_load() for details and arguments.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2011-07-04 17:09:41 +00:00
|
|
|
function hook_field_attach_load($entity_type, $entities, $age, $options) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_validate().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2012-09-27 15:44:17 +00:00
|
|
|
* The entity with fields to validate.
|
|
|
|
* @param $errors
|
|
|
|
* The array of errors (keyed by field name, language code, and delta) that
|
|
|
|
* have already been reported for the entity. The function should add its
|
|
|
|
* errors to this array. Each error is an associative array with the following
|
|
|
|
* keys and values:
|
|
|
|
* - error: An error code (should be a string prefixed with the module name).
|
|
|
|
* - message: The human-readable message to be displayed.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_validate(\Drupal\Core\Entity\EntityInterface $entity, &$errors) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_submit().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-11-20 19:57:01 +00:00
|
|
|
* The entity for which an edit form is being submitted. The incoming form
|
|
|
|
* values have been extracted as field values of the $entity object.
|
|
|
|
* @param $form
|
2012-09-27 15:44:17 +00:00
|
|
|
* The form structure field elements are attached to. This might be a full
|
|
|
|
* form structure, or a sub-part of a larger form. The $form['#parents']
|
2010-11-20 19:57:01 +00:00
|
|
|
* property can be used to identify the corresponding part of
|
|
|
|
* $form_state['values'].
|
|
|
|
* @param $form_state
|
|
|
|
* An associative array containing the current state of the form.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_submit(\Drupal\Core\Entity\EntityInterface $entity, $form, &$form_state) {
|
2010-11-20 19:57:01 +00:00
|
|
|
// Sample case of an 'Empty the field' checkbox added on the form, allowing
|
|
|
|
// a given field to be emptied.
|
2012-12-17 21:54:13 +00:00
|
|
|
$values = NestedArray::getValue($form_state['values'], $form['#parents']);
|
2010-11-20 19:57:01 +00:00
|
|
|
if (!empty($values['empty_field_foo'])) {
|
|
|
|
unset($entity->field_foo);
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_presave().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2012-09-27 15:44:17 +00:00
|
|
|
* the entity with fields to process.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_presave(\Drupal\Core\Entity\EntityInterface $entity) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_insert().
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2009-11-08 19:11:56 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-03-30 03:44:55 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2012-09-27 15:44:17 +00:00
|
|
|
* the entity with fields to process.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_insert(\Drupal\Core\Entity\EntityInterface $entity) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_update().
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2009-11-08 19:11:56 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-03-30 03:44:55 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2012-09-27 15:44:17 +00:00
|
|
|
* the entity with fields to process.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_update(\Drupal\Core\Entity\EntityInterface $entity) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 16:17:30 +00:00
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Alter field_attach_preprocess() variables.
|
2009-06-06 16:17:30 +00:00
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* This hook is invoked while preprocessing the field.tpl.php template file in
|
|
|
|
* field_attach_preprocess().
|
2009-06-06 16:17:30 +00:00
|
|
|
*
|
2009-11-08 19:11:56 +00:00
|
|
|
* @param $variables
|
|
|
|
* The variables array is passed by reference and will be populated with field
|
|
|
|
* values.
|
|
|
|
* @param $context
|
|
|
|
* An associative array containing:
|
2010-05-03 07:37:51 +00:00
|
|
|
* - entity: The entity with fields to render.
|
2009-11-08 19:11:56 +00:00
|
|
|
* - element: The structured array containing the values ready for rendering.
|
2009-06-06 16:17:30 +00:00
|
|
|
*/
|
2009-11-08 19:11:56 +00:00
|
|
|
function hook_field_attach_preprocess_alter(&$variables, $context) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-06-06 16:17:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_delete().
|
2009-03-30 03:44:55 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2012-09-27 15:44:17 +00:00
|
|
|
* the entity with fields to process.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_delete(\Drupal\Core\Entity\EntityInterface $entity) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_delete_revision().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2012-09-27 15:44:17 +00:00
|
|
|
* the entity with fields to process.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_delete_revision(\Drupal\Core\Entity\EntityInterface $entity) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-24 07:19:09 +00:00
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_purge_data().
|
2010-04-24 07:19:09 +00:00
|
|
|
*
|
|
|
|
* This hook is invoked in field_purge_data() and allows modules to act on
|
2010-05-03 07:37:51 +00:00
|
|
|
* purging data from a single field pseudo-entity. For example, if a module
|
2012-09-27 15:44:17 +00:00
|
|
|
* relates data in the field with its own data, it may purge its own data during
|
|
|
|
* this process as well.
|
2010-04-24 07:19:09 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-04-24 07:19:09 +00:00
|
|
|
* The pseudo-entity whose field data is being purged.
|
|
|
|
* @param $field
|
|
|
|
* The (possibly deleted) field whose data is being purged.
|
|
|
|
* @param $instance
|
|
|
|
* The deleted field instance whose data is being purged.
|
|
|
|
*
|
|
|
|
* @see @link field_purge Field API bulk data deletion @endlink
|
|
|
|
* @see field_purge_data()
|
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_purge(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance) {
|
2010-04-24 07:19:09 +00:00
|
|
|
// find the corresponding data in mymodule and purge it
|
2013-01-07 11:22:28 +00:00
|
|
|
if ($entity->entityType() == 'node' && $field->field_name == 'my_field_name') {
|
2010-04-24 07:19:09 +00:00
|
|
|
mymodule_remove_mydata($entity->nid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2010-12-07 05:09:58 +00:00
|
|
|
* Perform alterations on field_attach_view() or field_view_field().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* @param $output
|
|
|
|
* The structured content array tree for all of the entity's fields.
|
2009-10-13 16:38:43 +00:00
|
|
|
* @param $context
|
|
|
|
* An associative array containing:
|
2010-05-03 07:37:51 +00:00
|
|
|
* - entity: The entity with fields to render.
|
2010-12-07 05:09:58 +00:00
|
|
|
* - view_mode: View mode; for example, 'full' or 'teaser'.
|
2012-12-28 23:03:17 +00:00
|
|
|
* - display_options: Either a view mode string or an array of display
|
|
|
|
* options. If this hook is being invoked from field_attach_view(), the
|
|
|
|
* 'display_options' element is set to the view mode string. If this hook
|
|
|
|
* is being invoked from field_view_field(), this element is set to the
|
|
|
|
* $display_options argument and the view_mode element is set to '_custom'.
|
|
|
|
* See field_view_field() for more information on what its $display_options
|
|
|
|
* argument contains.
|
2010-10-03 01:15:34 +00:00
|
|
|
* - language: The language code used for rendering.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2009-10-13 16:38:43 +00:00
|
|
|
function hook_field_attach_view_alter(&$output, $context) {
|
2010-07-25 02:45:27 +00:00
|
|
|
// Append RDF term mappings on displayed taxonomy links.
|
|
|
|
foreach (element_children($output) as $field_name) {
|
|
|
|
$element = &$output[$field_name];
|
2013-02-01 16:08:58 +00:00
|
|
|
if ($element['#field_type'] == 'entity_reference' && $element['#formatter'] == 'entity_reference_label') {
|
2010-07-25 02:45:27 +00:00
|
|
|
foreach ($element['#items'] as $delta => $item) {
|
|
|
|
$term = $item['taxonomy_term'];
|
|
|
|
if (!empty($term->rdf_mapping['rdftype'])) {
|
|
|
|
$element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
|
|
|
|
}
|
|
|
|
if (!empty($term->rdf_mapping['name']['predicates'])) {
|
|
|
|
$element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-25 11:46:21 +00:00
|
|
|
}
|
|
|
|
|
2010-06-06 00:24:16 +00:00
|
|
|
/**
|
|
|
|
* Perform alterations on field_attach_prepare_translation().
|
|
|
|
*
|
|
|
|
* This hook is invoked after the field module has performed the operation.
|
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-06-06 00:24:16 +00:00
|
|
|
* The entity being prepared for translation.
|
|
|
|
* @param $context
|
|
|
|
* An associative array containing:
|
2012-09-27 15:44:17 +00:00
|
|
|
* - langcode: The language the entity will be translated to.
|
2010-06-06 00:24:16 +00:00
|
|
|
* - source_entity: The entity holding the field values to be translated.
|
2012-09-27 15:44:17 +00:00
|
|
|
* - source_langcode: The source language from which to translate.
|
2010-06-06 00:24:16 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_attach_prepare_translation_alter(\Drupal\Core\Entity\EntityInterface $entity, $context) {
|
|
|
|
if ($entity->entityType() == 'custom_entity_type') {
|
2010-06-06 00:24:16 +00:00
|
|
|
$entity->custom_field = $context['source_entity']->custom_field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 11:46:21 +00:00
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Perform alterations on field_language() values.
|
2010-03-25 11:46:21 +00:00
|
|
|
*
|
2012-04-09 18:51:01 +00:00
|
|
|
* This hook is invoked to alter the array of display language codes for the
|
|
|
|
* given entity.
|
2010-03-25 11:46:21 +00:00
|
|
|
*
|
2012-04-09 18:51:01 +00:00
|
|
|
* @param $display_langcode
|
2010-03-25 11:46:21 +00:00
|
|
|
* A reference to an array of language codes keyed by field name.
|
|
|
|
* @param $context
|
|
|
|
* An associative array containing:
|
|
|
|
* - entity: The entity with fields to render.
|
|
|
|
* - langcode: The language code $entity has to be displayed in.
|
|
|
|
*/
|
2012-04-09 18:51:01 +00:00
|
|
|
function hook_field_language_alter(&$display_langcode, $context) {
|
2010-07-25 02:45:27 +00:00
|
|
|
// Do not apply core language fallback rules if they are disabled or if Locale
|
|
|
|
// is not registered as a translation handler.
|
2013-01-07 11:22:28 +00:00
|
|
|
if (variable_get('field_language_fallback', TRUE) && field_has_translation_handler($context['entity']->entityType())) {
|
2012-06-15 17:14:22 +00:00
|
|
|
field_language_fallback($display_langcode, $context['entity'], $context['langcode']);
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
2010-03-25 11:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Alter field_available_languages() values.
|
2010-03-25 11:46:21 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_available_languages() to allow modules to
|
2012-04-09 18:51:01 +00:00
|
|
|
* alter the array of available language codes for the given field.
|
2010-03-25 11:46:21 +00:00
|
|
|
*
|
2012-04-09 18:51:01 +00:00
|
|
|
* @param $langcodes
|
2010-03-25 11:46:21 +00:00
|
|
|
* A reference to an array of language codes to be made available.
|
|
|
|
* @param $context
|
|
|
|
* An associative array containing:
|
|
|
|
* - entity_type: The type of the entity the field is attached to.
|
|
|
|
* - field: A field data structure.
|
|
|
|
*/
|
2012-04-09 18:51:01 +00:00
|
|
|
function hook_field_available_languages_alter(&$langcodes, $context) {
|
|
|
|
// Add an unavailable language code.
|
|
|
|
$langcodes[] = 'xx';
|
2010-07-25 02:45:27 +00:00
|
|
|
|
2012-04-09 18:51:01 +00:00
|
|
|
// Remove an available language code.
|
|
|
|
$index = array_search('yy', $langcodes);
|
|
|
|
unset($langcodes[$index]);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_create_bundle().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2013-01-09 00:19:14 +00:00
|
|
|
* @param string $entity_type
|
2012-09-27 15:44:17 +00:00
|
|
|
* The type of $entity; e.g. 'node' or 'user'.
|
2013-01-09 00:19:14 +00:00
|
|
|
* @param string $bundle
|
|
|
|
* The name of the bundle.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function hook_field_attach_create_bundle($entity_type, $bundle) {
|
2010-07-25 02:45:27 +00:00
|
|
|
// When a new bundle is created, the menu needs to be rebuilt to add the
|
|
|
|
// Field UI menu item tabs.
|
2012-09-20 09:20:07 +00:00
|
|
|
state()->set('menu_rebuild_needed', TRUE);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-03 07:37:51 +00:00
|
|
|
* Act on field_attach_rename_bundle().
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* @param $entity_type
|
|
|
|
* The entity type to which the bundle is bound.
|
|
|
|
* @param $bundle_old
|
|
|
|
* The previous name of the bundle.
|
|
|
|
* @param $bundle_new
|
|
|
|
* The new name of the bundle.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function hook_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
|
2010-07-25 02:45:27 +00:00
|
|
|
// Update the extra weights variable with new information.
|
|
|
|
if ($bundle_old !== $bundle_new) {
|
|
|
|
$extra_weights = variable_get('field_extra_weights', array());
|
|
|
|
if (isset($info[$entity_type][$bundle_old])) {
|
|
|
|
$extra_weights[$entity_type][$bundle_new] = $extra_weights[$entity_type][$bundle_old];
|
|
|
|
unset($extra_weights[$entity_type][$bundle_old]);
|
|
|
|
variable_set('field_extra_weights', $extra_weights);
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-17 03:46:51 +00:00
|
|
|
* Act on field_attach_delete_bundle.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2009-03-17 03:46:51 +00:00
|
|
|
* This hook is invoked after the field module has performed the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-05-03 07:37:51 +00:00
|
|
|
* The type of entity; for example, 'node' or 'user'.
|
2009-04-26 09:18:20 +00:00
|
|
|
* @param $bundle
|
|
|
|
* The bundle that was just deleted.
|
|
|
|
* @param $instances
|
2009-10-15 12:44:36 +00:00
|
|
|
* An array of all instances that existed for the bundle before it was
|
2009-04-26 09:18:20 +00:00
|
|
|
* deleted.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function hook_field_attach_delete_bundle($entity_type, $bundle, $instances) {
|
2010-07-25 02:45:27 +00:00
|
|
|
// Remove the extra weights variable information for this bundle.
|
|
|
|
$extra_weights = variable_get('field_extra_weights', array());
|
|
|
|
if (isset($extra_weights[$entity_type][$bundle])) {
|
|
|
|
unset($extra_weights[$entity_type][$bundle]);
|
|
|
|
variable_set('field_extra_weights', $extra_weights);
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "addtogroup field_attach".
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @addtogroup field_storage
|
2009-02-05 03:42:58 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
/**
|
|
|
|
* Expose Field API storage backends.
|
|
|
|
*
|
|
|
|
* @return
|
2012-09-27 15:44:17 +00:00
|
|
|
* An array describing the storage backends implemented by the module. The
|
|
|
|
* keys are storage backend names. To avoid name clashes, storage backend
|
|
|
|
* names should be prefixed with the name of the module that exposes them. The
|
|
|
|
* values are arrays describing the storage backend, with the following
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* key/value pairs:
|
|
|
|
* - label: The human-readable name of the storage backend.
|
|
|
|
* - description: A short description for the storage backend.
|
2012-09-27 15:44:17 +00:00
|
|
|
* - settings: An array whose keys are the names of the settings available to
|
|
|
|
* the storage backend, and whose values are the default values of those
|
|
|
|
* settings.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
*/
|
|
|
|
function hook_field_storage_info() {
|
|
|
|
return array(
|
|
|
|
'field_sql_storage' => array(
|
|
|
|
'label' => t('Default SQL storage'),
|
|
|
|
'description' => t('Stores fields in the local SQL database, using per-field tables.'),
|
|
|
|
'settings' => array(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform alterations on Field API storage types.
|
|
|
|
*
|
|
|
|
* @param $info
|
|
|
|
* Array of informations on storage types exposed by
|
|
|
|
* hook_field_field_storage_info() implementations.
|
|
|
|
*/
|
|
|
|
function hook_field_storage_info_alter(&$info) {
|
|
|
|
// Add a setting to a storage type.
|
|
|
|
$info['field_sql_storage']['settings'] += array(
|
|
|
|
'mymodule_additional_setting' => 'default value',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-10-14 14:55:12 +00:00
|
|
|
/**
|
|
|
|
* Reveal the internal details about the storage for a field.
|
|
|
|
*
|
|
|
|
* For example, an SQL storage module might return the Schema API structure for
|
|
|
|
* the table. A key/value storage module might return the server name,
|
|
|
|
* authentication credentials, and bin name.
|
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* Field storage modules are not obligated to implement this hook. Modules that
|
|
|
|
* rely on these details must only use them for read operations.
|
2009-10-14 14:55:12 +00:00
|
|
|
*
|
|
|
|
* @param $field
|
|
|
|
* A field structure.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2009-10-14 14:55:12 +00:00
|
|
|
* @return
|
|
|
|
* An array of details.
|
|
|
|
* - The first dimension is a store type (sql, solr, etc).
|
|
|
|
* - The second dimension indicates the age of the values in the store
|
|
|
|
* FIELD_LOAD_CURRENT or FIELD_LOAD_REVISION.
|
|
|
|
* - Other dimensions are specific to the field storage module.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_storage_details_alter()
|
2009-10-14 14:55:12 +00:00
|
|
|
*/
|
2010-01-13 04:37:03 +00:00
|
|
|
function hook_field_storage_details($field) {
|
2010-07-25 02:45:27 +00:00
|
|
|
$details = array();
|
|
|
|
|
|
|
|
// Add field columns.
|
|
|
|
foreach ((array) $field['columns'] as $column_name => $attributes) {
|
|
|
|
$real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
|
|
|
|
$columns[$column_name] = $real_name;
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'sql' => array(
|
|
|
|
FIELD_LOAD_CURRENT => array(
|
|
|
|
_field_sql_storage_tablename($field) => $columns,
|
|
|
|
),
|
|
|
|
FIELD_LOAD_REVISION => array(
|
|
|
|
_field_sql_storage_revision_tablename($field) => $columns,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2009-10-14 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform alterations on Field API storage details.
|
|
|
|
*
|
|
|
|
* @param $details
|
|
|
|
* An array of storage details for fields as exposed by
|
|
|
|
* hook_field_storage_details() implementations.
|
|
|
|
* @param $field
|
|
|
|
* A field structure.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
|
|
|
* @see hook_field_storage_details()
|
2009-10-14 14:55:12 +00:00
|
|
|
*/
|
2010-01-13 04:37:03 +00:00
|
|
|
function hook_field_storage_details_alter(&$details, $field) {
|
2010-07-25 02:45:27 +00:00
|
|
|
if ($field['field_name'] == 'field_of_interest') {
|
|
|
|
$columns = array();
|
|
|
|
foreach ((array) $field['columns'] as $column_name => $attributes) {
|
|
|
|
$columns[$column_name] = $column_name;
|
|
|
|
}
|
|
|
|
$details['drupal_variables'] = array(
|
|
|
|
FIELD_LOAD_CURRENT => array(
|
|
|
|
'moon' => $columns,
|
|
|
|
),
|
|
|
|
FIELD_LOAD_REVISION => array(
|
|
|
|
'mars' => $columns,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2009-10-14 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Load field data for a set of entities.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* This hook is invoked from field_attach_load() to ask the field storage module
|
|
|
|
* to load field data.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2010-04-04 07:33:15 +00:00
|
|
|
* Modules implementing this hook should load field values and add them to
|
2012-09-27 15:44:17 +00:00
|
|
|
* objects in $entities. Fields with no values should be added as empty arrays.
|
2010-04-04 07:33:15 +00:00
|
|
|
*
|
2012-09-28 22:11:59 +00:00
|
|
|
* By the time this hook runs, the relevant field definitions have been
|
|
|
|
* populated and cached in FieldInfo, so calling field_info_field_by_id() on
|
|
|
|
* each field individually is more efficient than loading all fields in memory
|
|
|
|
* upfront with field_info_field_by_ids() (which is uncached).
|
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-04-04 07:33:15 +00:00
|
|
|
* The type of entity, such as 'node' or 'user'.
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entities
|
2010-04-04 07:33:15 +00:00
|
|
|
* The array of entity objects to add fields to, keyed by entity ID.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $age
|
2010-04-04 07:33:15 +00:00
|
|
|
* FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
|
|
|
|
* FIELD_LOAD_REVISION to load the version indicated by each entity.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* @param $fields
|
|
|
|
* An array listing the fields to be loaded. The keys of the array are field
|
2010-04-04 07:33:15 +00:00
|
|
|
* IDs, and the values of the array are the entity IDs (or revision IDs,
|
|
|
|
* depending on the $age parameter) to add each field to.
|
|
|
|
* @param $options
|
|
|
|
* An associative array of additional options, with the following keys:
|
2012-09-27 15:44:17 +00:00
|
|
|
* - deleted: If TRUE, deleted fields should be loaded as well as non-deleted
|
|
|
|
* fields. If unset or FALSE, only non-deleted fields should be loaded.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2011-07-04 17:09:41 +00:00
|
|
|
function hook_field_storage_load($entity_type, $entities, $age, $fields, $options) {
|
2010-07-25 02:45:27 +00:00
|
|
|
$load_current = $age == FIELD_LOAD_CURRENT;
|
|
|
|
|
|
|
|
foreach ($fields as $field_id => $ids) {
|
2012-09-28 22:11:59 +00:00
|
|
|
$field = field_info_field_by_id($field_id);
|
2010-07-25 02:45:27 +00:00
|
|
|
$field_name = $field['field_name'];
|
|
|
|
$table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
|
|
|
|
|
|
|
|
$query = db_select($table, 't')
|
|
|
|
->fields('t')
|
2010-12-14 19:50:05 +00:00
|
|
|
->condition('entity_type', $entity_type)
|
2010-07-25 02:45:27 +00:00
|
|
|
->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
|
2012-04-09 18:51:01 +00:00
|
|
|
->condition('langcode', field_available_languages($entity_type, $field), 'IN')
|
2010-07-25 02:45:27 +00:00
|
|
|
->orderBy('delta');
|
|
|
|
|
|
|
|
if (empty($options['deleted'])) {
|
|
|
|
$query->condition('deleted', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = $query->execute();
|
|
|
|
|
|
|
|
$delta_count = array();
|
|
|
|
foreach ($results as $row) {
|
2012-04-09 18:51:01 +00:00
|
|
|
if (!isset($delta_count[$row->entity_id][$row->langcode])) {
|
|
|
|
$delta_count[$row->entity_id][$row->langcode] = 0;
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
|
2012-04-09 18:51:01 +00:00
|
|
|
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->langcode] < $field['cardinality']) {
|
2010-07-25 02:45:27 +00:00
|
|
|
$item = array();
|
|
|
|
// For each column declared by the field, populate the item
|
|
|
|
// from the prefixed database column.
|
|
|
|
foreach ($field['columns'] as $column => $attributes) {
|
|
|
|
$column_name = _field_sql_storage_columnname($field_name, $column);
|
|
|
|
$item[$column] = $row->$column_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the item to the field values for the entity.
|
2012-04-09 18:51:01 +00:00
|
|
|
$entities[$row->entity_id]->{$field_name}[$row->langcode][] = $item;
|
|
|
|
$delta_count[$row->entity_id][$row->langcode]++;
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Write field data for an entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* This hook is invoked from field_attach_insert() and field_attach_update(), to
|
|
|
|
* ask the field storage module to save field data.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity on which to operate.
|
2009-03-13 21:25:40 +00:00
|
|
|
* @param $op
|
2010-02-12 05:38:10 +00:00
|
|
|
* FIELD_STORAGE_UPDATE when updating an existing entity,
|
|
|
|
* FIELD_STORAGE_INSERT when inserting a new entity.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* @param $fields
|
|
|
|
* An array listing the fields to be written. The keys and values of the
|
2010-05-03 07:37:51 +00:00
|
|
|
* array are field IDs.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_storage_write(\Drupal\Core\Entity\EntityInterface $entity, $op, $fields) {
|
2012-08-11 16:19:01 +00:00
|
|
|
$id = $entity->id();
|
|
|
|
$vid = $entity->getRevisionId();
|
|
|
|
$bundle = $entity->bundle();
|
2010-12-14 19:50:05 +00:00
|
|
|
if (!isset($vid)) {
|
|
|
|
$vid = $id;
|
|
|
|
}
|
2010-07-25 02:45:27 +00:00
|
|
|
|
|
|
|
foreach ($fields as $field_id) {
|
|
|
|
$field = field_info_field_by_id($field_id);
|
|
|
|
$field_name = $field['field_name'];
|
|
|
|
$table_name = _field_sql_storage_tablename($field);
|
|
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
|
|
|
|
2013-01-07 11:22:28 +00:00
|
|
|
$all_langcodes = field_available_languages($entity->entityType(), $field);
|
2012-04-09 18:51:01 +00:00
|
|
|
$field_langcodes = array_intersect($all_langcodes, array_keys((array) $entity->$field_name));
|
2010-07-25 02:45:27 +00:00
|
|
|
|
|
|
|
// Delete and insert, rather than update, in case a value was added.
|
|
|
|
if ($op == FIELD_STORAGE_UPDATE) {
|
2012-04-09 18:51:01 +00:00
|
|
|
// Delete language codes present in the incoming $entity->$field_name.
|
|
|
|
// Delete all language codes if $entity->$field_name is empty.
|
|
|
|
$langcodes = !empty($entity->$field_name) ? $field_langcodes : $all_langcodes;
|
|
|
|
if ($langcodes) {
|
2010-07-25 02:45:27 +00:00
|
|
|
db_delete($table_name)
|
2013-01-07 11:22:28 +00:00
|
|
|
->condition('entity_type', $entity->entityType())
|
2010-07-25 02:45:27 +00:00
|
|
|
->condition('entity_id', $id)
|
2012-04-09 18:51:01 +00:00
|
|
|
->condition('langcode', $langcodes, 'IN')
|
2010-07-25 02:45:27 +00:00
|
|
|
->execute();
|
2010-12-14 19:50:05 +00:00
|
|
|
db_delete($revision_name)
|
2013-01-07 11:22:28 +00:00
|
|
|
->condition('entity_type', $entity->entityType())
|
2010-12-14 19:50:05 +00:00
|
|
|
->condition('entity_id', $id)
|
|
|
|
->condition('revision_id', $vid)
|
2012-04-09 18:51:01 +00:00
|
|
|
->condition('langcode', $langcodes, 'IN')
|
2010-12-14 19:50:05 +00:00
|
|
|
->execute();
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the multi-insert query.
|
|
|
|
$do_insert = FALSE;
|
2012-04-09 18:51:01 +00:00
|
|
|
$columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'langcode');
|
2010-07-25 02:45:27 +00:00
|
|
|
foreach ($field['columns'] as $column => $attributes) {
|
|
|
|
$columns[] = _field_sql_storage_columnname($field_name, $column);
|
|
|
|
}
|
|
|
|
$query = db_insert($table_name)->fields($columns);
|
2010-12-14 19:50:05 +00:00
|
|
|
$revision_query = db_insert($revision_name)->fields($columns);
|
2010-07-25 02:45:27 +00:00
|
|
|
|
2012-04-09 18:51:01 +00:00
|
|
|
foreach ($field_langcodes as $langcode) {
|
2010-07-25 02:45:27 +00:00
|
|
|
$items = (array) $entity->{$field_name}[$langcode];
|
|
|
|
$delta_count = 0;
|
|
|
|
foreach ($items as $delta => $item) {
|
|
|
|
// We now know we have someting to insert.
|
|
|
|
$do_insert = TRUE;
|
|
|
|
$record = array(
|
2013-01-07 11:22:28 +00:00
|
|
|
'entity_type' => $entity->entityType(),
|
2010-07-25 02:45:27 +00:00
|
|
|
'entity_id' => $id,
|
|
|
|
'revision_id' => $vid,
|
|
|
|
'bundle' => $bundle,
|
|
|
|
'delta' => $delta,
|
2012-04-09 18:51:01 +00:00
|
|
|
'langcode' => $langcode,
|
2010-07-25 02:45:27 +00:00
|
|
|
);
|
|
|
|
foreach ($field['columns'] as $column => $attributes) {
|
|
|
|
$record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
|
|
|
|
}
|
|
|
|
$query->values($record);
|
|
|
|
if (isset($vid)) {
|
|
|
|
$revision_query->values($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the query if we have values to insert.
|
|
|
|
if ($do_insert) {
|
|
|
|
$query->execute();
|
2010-12-14 19:50:05 +00:00
|
|
|
$revision_query->execute();
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Delete all field data for an entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_attach_delete() to ask the field storage
|
|
|
|
* module to delete field data.
|
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity on which to operate.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* @param $fields
|
|
|
|
* An array listing the fields to delete. The keys and values of the
|
2010-05-03 07:37:51 +00:00
|
|
|
* array are field IDs.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_storage_delete(\Drupal\Core\Entity\EntityInterface $entity, $fields) {
|
|
|
|
foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) {
|
2010-07-25 02:45:27 +00:00
|
|
|
if (isset($fields[$instance['field_id']])) {
|
|
|
|
$field = field_info_field_by_id($instance['field_id']);
|
2013-01-07 11:22:28 +00:00
|
|
|
field_sql_storage_field_storage_purge($entity, $field, $instance);
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-12 05:38:10 +00:00
|
|
|
* Delete a single revision of field data for an entity.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_attach_delete_revision() to ask the field
|
|
|
|
* storage module to delete field revision data.
|
|
|
|
*
|
2009-03-30 03:44:55 +00:00
|
|
|
* Deleting the current (most recently written) revision is not
|
|
|
|
* allowed as has undefined results.
|
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
|
|
|
* The entity on which to operate.
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* @param $fields
|
|
|
|
* An array listing the fields to delete. The keys and values of the
|
2010-05-03 07:37:51 +00:00
|
|
|
* array are field IDs.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_storage_delete_revision(\Drupal\Core\Entity\EntityInterface $entity, $fields) {
|
2012-08-11 16:19:01 +00:00
|
|
|
$vid = $entity->getRevisionId();
|
2010-07-25 02:45:27 +00:00
|
|
|
if (isset($vid)) {
|
|
|
|
foreach ($fields as $field_id) {
|
|
|
|
$field = field_info_field_by_id($field_id);
|
|
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
|
|
|
db_delete($revision_name)
|
2010-12-14 19:50:05 +00:00
|
|
|
->condition('entity_type', $entity_type)
|
2012-08-11 16:19:01 +00:00
|
|
|
->condition('entity_id', $entity->id())
|
2010-07-25 02:45:27 +00:00
|
|
|
->condition('revision_id', $vid)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
}
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 16:17:30 +00:00
|
|
|
/**
|
2012-09-12 09:18:04 +00:00
|
|
|
* Execute a Drupal\Core\Entity\EntityFieldQuery.
|
2009-06-06 16:17:30 +00:00
|
|
|
*
|
2010-06-14 15:41:03 +00:00
|
|
|
* This hook is called to find the entities having certain entity and field
|
|
|
|
* conditions and sort them in the given field order. If the field storage
|
|
|
|
* engine also handles property sorts and orders, it should unset those
|
|
|
|
* properties in the called object to signal that those have been handled.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2012-09-12 09:18:04 +00:00
|
|
|
* @param Drupal\Core\Entity\EntityFieldQuery $query
|
2010-06-14 15:41:03 +00:00
|
|
|
* An EntityFieldQuery.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2009-06-06 16:17:30 +00:00
|
|
|
* @return
|
2012-09-12 09:18:04 +00:00
|
|
|
* See Drupal\Core\Entity\EntityFieldQuery::execute() for the return values.
|
2009-06-06 16:17:30 +00:00
|
|
|
*/
|
2010-06-14 15:41:03 +00:00
|
|
|
function hook_field_storage_query($query) {
|
2010-12-14 19:50:05 +00:00
|
|
|
$groups = array();
|
|
|
|
if ($query->age == FIELD_LOAD_CURRENT) {
|
|
|
|
$tablename_function = '_field_sql_storage_tablename';
|
|
|
|
$id_key = 'entity_id';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$tablename_function = '_field_sql_storage_revision_tablename';
|
|
|
|
$id_key = 'revision_id';
|
|
|
|
}
|
|
|
|
$table_aliases = array();
|
|
|
|
// Add tables for the fields used.
|
|
|
|
foreach ($query->fields as $key => $field) {
|
|
|
|
$tablename = $tablename_function($field);
|
|
|
|
// Every field needs a new table.
|
|
|
|
$table_alias = $tablename . $key;
|
|
|
|
$table_aliases[$key] = $table_alias;
|
|
|
|
if ($key) {
|
|
|
|
$select_query->join($tablename, $table_alias, "$table_alias.entity_type = $field_base_table.entity_type AND $table_alias.$id_key = $field_base_table.$id_key");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$select_query = db_select($tablename, $table_alias);
|
|
|
|
$select_query->addTag('entity_field_access');
|
|
|
|
$select_query->addMetaData('base_table', $tablename);
|
|
|
|
$select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
|
|
|
|
$field_base_table = $table_alias;
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
2010-12-14 19:50:05 +00:00
|
|
|
if ($field['cardinality'] != 1) {
|
|
|
|
$select_query->distinct();
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
2010-12-14 19:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add field conditions.
|
|
|
|
foreach ($query->fieldConditions as $key => $condition) {
|
|
|
|
$table_alias = $table_aliases[$key];
|
|
|
|
$field = $condition['field'];
|
|
|
|
// Add the specified condition.
|
|
|
|
$sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']);
|
|
|
|
$query->addCondition($select_query, $sql_field, $condition);
|
|
|
|
// Add delta / language group conditions.
|
2012-04-09 18:51:01 +00:00
|
|
|
foreach (array('delta', 'langcode') as $column) {
|
2010-12-14 19:50:05 +00:00
|
|
|
if (isset($condition[$column . '_group'])) {
|
|
|
|
$group_name = $condition[$column . '_group'];
|
|
|
|
if (!isset($groups[$column][$group_name])) {
|
|
|
|
$groups[$column][$group_name] = $table_alias;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-14 19:50:05 +00:00
|
|
|
if (isset($query->deleted)) {
|
|
|
|
$select_query->condition("$field_base_table.deleted", (int) $query->deleted);
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 19:50:05 +00:00
|
|
|
// Is there a need to sort the query by property?
|
|
|
|
$has_property_order = FALSE;
|
|
|
|
foreach ($query->order as $order) {
|
|
|
|
if ($order['type'] == 'property') {
|
|
|
|
$has_property_order = TRUE;
|
|
|
|
}
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 19:50:05 +00:00
|
|
|
if ($query->propertyConditions || $has_property_order) {
|
|
|
|
if (empty($query->entityConditions['entity_type']['value'])) {
|
|
|
|
throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
2010-12-14 19:50:05 +00:00
|
|
|
$entity_type = $query->entityConditions['entity_type']['value'];
|
|
|
|
$entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
|
|
|
|
$query->entityConditions['entity_type']['operator'] = '=';
|
|
|
|
foreach ($query->propertyConditions as $property_condition) {
|
|
|
|
$query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
2010-12-14 19:50:05 +00:00
|
|
|
}
|
|
|
|
foreach ($query->entityConditions as $key => $condition) {
|
|
|
|
$query->addCondition($select_query, "$field_base_table.$key", $condition);
|
|
|
|
}
|
2010-07-25 02:45:27 +00:00
|
|
|
|
2010-12-14 19:50:05 +00:00
|
|
|
// Order the query.
|
|
|
|
foreach ($query->order as $order) {
|
|
|
|
if ($order['type'] == 'entity') {
|
|
|
|
$key = $order['specifier'];
|
|
|
|
$select_query->orderBy("$field_base_table.$key", $order['direction']);
|
|
|
|
}
|
|
|
|
elseif ($order['type'] == 'field') {
|
|
|
|
$specifier = $order['specifier'];
|
|
|
|
$field = $specifier['field'];
|
|
|
|
$table_alias = $table_aliases[$specifier['index']];
|
|
|
|
$sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
|
|
|
|
$select_query->orderBy($sql_field, $order['direction']);
|
|
|
|
}
|
|
|
|
elseif ($order['type'] == 'property') {
|
|
|
|
$select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
|
|
|
|
}
|
2010-07-25 02:45:27 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 19:50:05 +00:00
|
|
|
return $query->finishQuery($select_query, $id_key);
|
2009-06-06 16:17:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
|
|
|
* Act on creation of a new field.
|
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_create_field() to ask the field storage
|
2012-09-27 15:44:17 +00:00
|
|
|
* module to save field information and prepare for storing field instances. If
|
|
|
|
* there is a problem, the field storage module should throw an exception.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field structure being created.
|
|
|
|
*/
|
|
|
|
function hook_field_storage_create_field($field) {
|
2010-07-25 02:45:27 +00:00
|
|
|
$schema = _field_sql_storage_schema($field);
|
|
|
|
foreach ($schema as $name => $table) {
|
|
|
|
db_create_table($name, $table);
|
|
|
|
}
|
|
|
|
drupal_get_schema(NULL, TRUE);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act on deletion of a field.
|
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_delete_field() to ask the field storage
|
|
|
|
* module to mark all information stored in the field for deletion.
|
|
|
|
*
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* @param $field
|
|
|
|
* The field being deleted.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
function hook_field_storage_delete_field($field) {
|
2010-07-25 02:45:27 +00:00
|
|
|
// Mark all data associated with the field for deletion.
|
|
|
|
$field['deleted'] = 0;
|
|
|
|
$table = _field_sql_storage_tablename($field);
|
|
|
|
$revision_table = _field_sql_storage_revision_tablename($field);
|
|
|
|
db_update($table)
|
|
|
|
->fields(array('deleted' => 1))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
// Move the table to a unique name while the table contents are being deleted.
|
|
|
|
$field['deleted'] = 1;
|
|
|
|
$new_table = _field_sql_storage_tablename($field);
|
|
|
|
$revision_new_table = _field_sql_storage_revision_tablename($field);
|
|
|
|
db_rename_table($table, $new_table);
|
|
|
|
db_rename_table($revision_table, $revision_new_table);
|
|
|
|
drupal_get_schema(NULL, TRUE);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act on deletion of a field instance.
|
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_delete_instance() to ask the field storage
|
|
|
|
* module to mark all information stored for the field instance for deletion.
|
|
|
|
*
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
* @param $instance
|
|
|
|
* The instance being deleted.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
- Patch #443422 by yched, bjaspan | chx, merlinofchaos, Scott Reynolds, plach, profix898, mattyoung: added support for pluggable 'per field' storage engine. Comes with documentation and tests.
The Field Attach API uses the Field Storage API to perform all "database access". Each Field Storage API hook function defines a primitive database operation such as read, write, or delete. The default field storage module, field_sql_storage.module, uses the local SQL database to implement these operations, but alternative field storage backends can choose to represent the data in SQL differently or use a completely different storage mechanism such as a cloud-based database.
2009-09-27 12:52:55 +00:00
|
|
|
function hook_field_storage_delete_instance($instance) {
|
2010-07-25 02:45:27 +00:00
|
|
|
$field = field_info_field($instance['field_name']);
|
|
|
|
$table_name = _field_sql_storage_tablename($field);
|
|
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
|
|
|
db_update($table_name)
|
|
|
|
->fields(array('deleted' => 1))
|
2010-12-14 19:50:05 +00:00
|
|
|
->condition('entity_type', $instance['entity_type'])
|
2010-07-25 02:45:27 +00:00
|
|
|
->condition('bundle', $instance['bundle'])
|
|
|
|
->execute();
|
|
|
|
db_update($revision_name)
|
|
|
|
->fields(array('deleted' => 1))
|
2010-12-14 19:50:05 +00:00
|
|
|
->condition('entity_type', $instance['entity_type'])
|
2010-07-25 02:45:27 +00:00
|
|
|
->condition('bundle', $instance['bundle'])
|
|
|
|
->execute();
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-11-08 19:11:56 +00:00
|
|
|
/**
|
|
|
|
* Act before the storage backends load field data.
|
|
|
|
*
|
|
|
|
* This hook allows modules to load data before the Field Storage API,
|
|
|
|
* optionally preventing the field storage module from doing so.
|
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* This lets 3rd party modules override, mirror, share, or otherwise store a
|
|
|
|
* subset of fields in a different way than the current storage engine. Possible
|
|
|
|
* use cases include per-bundle storage, per-combo-field storage, etc.
|
2010-04-04 07:33:15 +00:00
|
|
|
*
|
|
|
|
* Modules implementing this hook should load field values and add them to
|
2012-09-27 15:44:17 +00:00
|
|
|
* objects in $entities. Fields with no values should be added as empty arrays.
|
|
|
|
* In addition, fields loaded should be added as keys to $skip_fields.
|
2009-11-08 19:11:56 +00:00
|
|
|
*
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-04-04 07:33:15 +00:00
|
|
|
* The type of entity, such as 'node' or 'user'.
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entities
|
2010-04-04 07:33:15 +00:00
|
|
|
* The array of entity objects to add fields to, keyed by entity ID.
|
2009-11-08 19:11:56 +00:00
|
|
|
* @param $age
|
|
|
|
* FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
|
2010-02-12 05:38:10 +00:00
|
|
|
* FIELD_LOAD_REVISION to load the version indicated by each entity.
|
2009-11-08 19:11:56 +00:00
|
|
|
* @param $skip_fields
|
2010-04-04 07:33:15 +00:00
|
|
|
* An array keyed by field IDs whose data has already been loaded and
|
|
|
|
* therefore should not be loaded again. Add a key to this array to indicate
|
|
|
|
* that your module has already loaded a field.
|
|
|
|
* @param $options
|
|
|
|
* An associative array of additional options, with the following keys:
|
2012-09-27 15:44:17 +00:00
|
|
|
* - field_id: The field ID that should be loaded. If unset, all fields should
|
|
|
|
* be loaded.
|
|
|
|
* - deleted: If TRUE, deleted fields should be loaded as well as non-deleted
|
|
|
|
* fields. If unset or FALSE, only non-deleted fields should be loaded.
|
2009-11-08 19:11:56 +00:00
|
|
|
*/
|
2010-04-04 07:33:15 +00:00
|
|
|
function hook_field_storage_pre_load($entity_type, $entities, $age, &$skip_fields, $options) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-11-08 19:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act before the storage backends insert field data.
|
|
|
|
*
|
|
|
|
* This hook allows modules to store data before the Field Storage API,
|
|
|
|
* optionally preventing the field storage module from doing so.
|
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to save.
|
2009-11-08 19:11:56 +00:00
|
|
|
* @param $skip_fields
|
2010-05-03 07:37:51 +00:00
|
|
|
* An array keyed by field IDs whose data has already been written and
|
|
|
|
* therefore should not be written again. The values associated with these
|
|
|
|
* keys are not specified.
|
2009-11-08 19:11:56 +00:00
|
|
|
* @return
|
2010-05-03 07:37:51 +00:00
|
|
|
* Saved field IDs are set set as keys in $skip_fields.
|
2009-11-08 19:11:56 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_storage_pre_insert(\Drupal\Core\Entity\EntityInterface $entity, &$skip_fields) {
|
|
|
|
if ($entity->entityType() == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
|
2009-11-08 19:11:56 +00:00
|
|
|
$query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach ($entity->taxonomy_forums as $language) {
|
2009-11-08 19:11:56 +00:00
|
|
|
foreach ($language as $delta) {
|
|
|
|
$query->values(array(
|
2010-02-11 17:44:47 +00:00
|
|
|
'nid' => $entity->nid,
|
|
|
|
'title' => $entity->title,
|
2009-11-08 19:11:56 +00:00
|
|
|
'tid' => $delta['value'],
|
2010-02-11 17:44:47 +00:00
|
|
|
'sticky' => $entity->sticky,
|
|
|
|
'created' => $entity->created,
|
2009-11-08 19:11:56 +00:00
|
|
|
'comment_count' => 0,
|
2010-02-11 17:44:47 +00:00
|
|
|
'last_comment_timestamp' => $entity->created,
|
2009-11-08 19:11:56 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act before the storage backends update field data.
|
|
|
|
*
|
|
|
|
* This hook allows modules to store data before the Field Storage API,
|
|
|
|
* optionally preventing the field storage module from doing so.
|
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* The entity with fields to save.
|
2009-11-08 19:11:56 +00:00
|
|
|
* @param $skip_fields
|
2010-05-03 07:37:51 +00:00
|
|
|
* An array keyed by field IDs whose data has already been written and
|
|
|
|
* therefore should not be written again. The values associated with these
|
|
|
|
* keys are not specified.
|
2009-11-08 19:11:56 +00:00
|
|
|
* @return
|
2010-05-03 07:37:51 +00:00
|
|
|
* Saved field IDs are set set as keys in $skip_fields.
|
2009-11-08 19:11:56 +00:00
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_storage_pre_update(\Drupal\Core\Entity\EntityInterface $entity, &$skip_fields) {
|
2009-11-08 19:11:56 +00:00
|
|
|
$first_call = &drupal_static(__FUNCTION__, array());
|
|
|
|
|
2013-01-07 11:22:28 +00:00
|
|
|
if ($entity->entityType() == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
|
2009-11-08 19:11:56 +00:00
|
|
|
// We don't maintain data for old revisions, so clear all previous values
|
2010-02-12 05:38:10 +00:00
|
|
|
// from the table. Since this hook runs once per field, per entity, make
|
2009-11-08 19:11:56 +00:00
|
|
|
// sure we only wipe values once.
|
2010-02-11 17:44:47 +00:00
|
|
|
if (!isset($first_call[$entity->nid])) {
|
|
|
|
$first_call[$entity->nid] = FALSE;
|
|
|
|
db_delete('forum_index')->condition('nid', $entity->nid)->execute();
|
2009-11-08 19:11:56 +00:00
|
|
|
}
|
|
|
|
// Only save data to the table if the node is published.
|
2010-02-11 17:44:47 +00:00
|
|
|
if ($entity->status) {
|
2009-11-08 19:11:56 +00:00
|
|
|
$query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
|
2010-02-11 17:44:47 +00:00
|
|
|
foreach ($entity->taxonomy_forums as $language) {
|
2009-11-08 19:11:56 +00:00
|
|
|
foreach ($language as $delta) {
|
|
|
|
$query->values(array(
|
2010-02-11 17:44:47 +00:00
|
|
|
'nid' => $entity->nid,
|
|
|
|
'title' => $entity->title,
|
2009-11-08 19:11:56 +00:00
|
|
|
'tid' => $delta['value'],
|
2010-02-11 17:44:47 +00:00
|
|
|
'sticky' => $entity->sticky,
|
|
|
|
'created' => $entity->created,
|
2009-11-08 19:11:56 +00:00
|
|
|
'comment_count' => 0,
|
2010-02-11 17:44:47 +00:00
|
|
|
'last_comment_timestamp' => $entity->created,
|
2009-11-08 19:11:56 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$query->execute();
|
|
|
|
// The logic for determining last_comment_count is fairly complex, so
|
|
|
|
// call _forum_update_forum_index() too.
|
2010-02-11 17:44:47 +00:00
|
|
|
_forum_update_forum_index($entity->nid);
|
2009-11-08 19:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-11 00:03:42 +00:00
|
|
|
/**
|
|
|
|
* Returns the maximum weight for the entity components handled by the module.
|
|
|
|
*
|
|
|
|
* Field API takes care of fields and 'extra_fields'. This hook is intended for
|
|
|
|
* third-party modules adding other entity components (e.g. field_group).
|
|
|
|
*
|
|
|
|
* @param $entity_type
|
|
|
|
* The type of entity; e.g. 'node' or 'user'.
|
|
|
|
* @param $bundle
|
|
|
|
* The bundle name.
|
|
|
|
* @param $context
|
|
|
|
* The context for which the maximum weight is requested. Either 'form', or
|
|
|
|
* the name of a view mode.
|
|
|
|
* @return
|
|
|
|
* The maximum weight of the entity's components, or NULL if no components
|
|
|
|
* were found.
|
|
|
|
*/
|
|
|
|
function hook_field_info_max_weight($entity_type, $bundle, $context) {
|
|
|
|
$weights = array();
|
|
|
|
|
|
|
|
foreach (my_module_entity_additions($entity_type, $bundle, $context) as $addition) {
|
|
|
|
$weights[] = $addition['weight'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $weights ? max($weights) : NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-23 18:23:24 +00:00
|
|
|
/**
|
|
|
|
* Alters the widget properties of a field instance on a given entity type
|
|
|
|
* before it gets displayed.
|
|
|
|
*
|
|
|
|
* Modules can implement hook_field_widget_properties_ENTITY_TYPE_alter() to
|
|
|
|
* alter the widget properties for fields on a specific entity type, rather than
|
|
|
|
* implementing hook_field_widget_properties_alter().
|
|
|
|
*
|
|
|
|
* This hook is called once per field per displayed widget entity. If the result
|
|
|
|
* of the hook involves reading from the database, it is highly recommended to
|
|
|
|
* statically cache the information.
|
|
|
|
*
|
2012-09-26 19:39:39 +00:00
|
|
|
* @param array $widget_properties
|
2010-10-23 18:23:24 +00:00
|
|
|
* The instance's widget properties.
|
2012-09-26 19:39:39 +00:00
|
|
|
* @param array $context
|
2010-10-23 18:23:24 +00:00
|
|
|
* An associative array containing:
|
2012-10-14 06:04:15 +00:00
|
|
|
* - entity_type: The entity type, e.g., 'node' or 'user'.
|
|
|
|
* - bundle: The bundle, e.g., 'page' or 'article'.
|
2010-11-18 05:37:47 +00:00
|
|
|
* - field: The field that the widget belongs to.
|
2010-10-23 18:23:24 +00:00
|
|
|
* - instance: The instance of the field.
|
|
|
|
*
|
|
|
|
* @see hook_field_widget_properties_alter()
|
|
|
|
*/
|
2012-09-26 19:39:39 +00:00
|
|
|
function hook_field_widget_properties_ENTITY_TYPE_alter(array &$widget_properties, array $context) {
|
2010-10-23 18:23:24 +00:00
|
|
|
// Change a widget's type according to the time of day.
|
|
|
|
$field = $context['field'];
|
|
|
|
if ($field['field_name'] == 'field_foo') {
|
|
|
|
$time = date('H');
|
2012-09-26 19:39:39 +00:00
|
|
|
$widget_properties['type'] = $time < 12 ? 'widget_am' : 'widget_pm';
|
2010-10-23 18:23:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "addtogroup field_storage".
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @addtogroup field_crud
|
2009-02-05 03:42:58 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-03-17 03:46:51 +00:00
|
|
|
* Act on a field being created.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_create_field() after the field is created, to
|
|
|
|
* allow modules to act on field creation.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
|
|
|
* @param $field
|
|
|
|
* The field just created.
|
|
|
|
*/
|
|
|
|
function hook_field_create_field($field) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-17 03:46:51 +00:00
|
|
|
* Act on a field instance being created.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_create_instance() after the instance record
|
|
|
|
* is saved, so it cannot be used to modify the instance itself.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
|
|
|
* @param $instance
|
|
|
|
* The instance just created.
|
|
|
|
*/
|
|
|
|
function hook_field_create_instance($instance) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-09-26 15:57:39 +00:00
|
|
|
/**
|
|
|
|
* Forbid a field update from occurring.
|
|
|
|
*
|
|
|
|
* Any module may forbid any update for any reason. For example, the
|
|
|
|
* field's storage module might forbid an update if it would change
|
|
|
|
* the storage schema while data for the field exists. A field type
|
|
|
|
* module might forbid an update if it would change existing data's
|
|
|
|
* semantics, or if there are external dependencies on field settings
|
|
|
|
* that cannot be updated.
|
|
|
|
*
|
2012-05-07 02:56:49 +00:00
|
|
|
* To forbid the update from occurring, throw a
|
|
|
|
* Drupal\field\FieldUpdateForbiddenException.
|
2010-05-03 07:37:51 +00:00
|
|
|
*
|
2009-09-26 15:57:39 +00:00
|
|
|
* @param $field
|
|
|
|
* The field as it will be post-update.
|
|
|
|
* @param $prior_field
|
|
|
|
* The field as it is pre-update.
|
|
|
|
* @param $has_data
|
|
|
|
* Whether any data already exists for this field.
|
|
|
|
*/
|
2010-05-03 07:37:51 +00:00
|
|
|
function hook_field_update_forbid($field, $prior_field, $has_data) {
|
2009-09-26 15:57:39 +00:00
|
|
|
// A 'list' field stores integer keys mapped to display values. If
|
|
|
|
// the new field will have fewer values, and any data exists for the
|
2010-01-25 10:38:35 +00:00
|
|
|
// abandoned keys, the field will have no way to display them. So,
|
2009-09-26 15:57:39 +00:00
|
|
|
// forbid such an update.
|
|
|
|
if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) {
|
|
|
|
// Identify the keys that will be lost.
|
|
|
|
$lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values']));
|
|
|
|
// If any data exist for those keys, forbid the update.
|
2010-06-17 13:16:57 +00:00
|
|
|
$query = new EntityFieldQuery();
|
2010-06-14 15:41:03 +00:00
|
|
|
$found = $query
|
|
|
|
->fieldCondition($prior_field['field_name'], 'value', $lost_keys)
|
|
|
|
->range(0, 1)
|
|
|
|
->execute();
|
|
|
|
if ($found) {
|
2009-09-30 12:26:36 +00:00
|
|
|
throw new FieldUpdateForbiddenException("Cannot update a list field not to include keys with existing data");
|
2009-09-26 15:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act on a field being updated.
|
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked just after field is updated in field_update_field().
|
2009-09-26 15:57:39 +00:00
|
|
|
*
|
|
|
|
* @param $field
|
|
|
|
* The field as it is post-update.
|
|
|
|
* @param $prior_field
|
|
|
|
* The field as it was pre-update.
|
|
|
|
* @param $has_data
|
|
|
|
* Whether any data already exists for this field.
|
|
|
|
*/
|
|
|
|
function hook_field_update_field($field, $prior_field, $has_data) {
|
2010-07-25 02:45:27 +00:00
|
|
|
// Reset the static value that keeps track of allowed values for list fields.
|
|
|
|
drupal_static_reset('list_allowed_values');
|
2009-09-26 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2009-03-17 03:46:51 +00:00
|
|
|
* Act on a field being deleted.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked just after a field is deleted by field_delete_field().
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
|
|
|
* @param $field
|
2009-09-09 11:37:34 +00:00
|
|
|
* The field just deleted.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
function hook_field_delete_field($field) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-17 03:46:51 +00:00
|
|
|
* Act on a field instance being updated.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_update_instance() after the instance record
|
|
|
|
* is saved, so it cannot be used by a module to modify the instance itself.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
|
|
|
* @param $instance
|
2010-04-22 19:06:34 +00:00
|
|
|
* The instance as it is post-update.
|
|
|
|
* @param $prior_$instance
|
|
|
|
* The instance as it was pre-update.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-04-22 19:06:34 +00:00
|
|
|
function hook_field_update_instance($instance, $prior_instance) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-17 03:46:51 +00:00
|
|
|
* Act on a field instance being deleted.
|
2009-04-13 05:18:18 +00:00
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_delete_instance() after the instance is
|
|
|
|
* deleted.
|
2009-02-05 03:42:58 +00:00
|
|
|
*
|
|
|
|
* @param $instance
|
2009-09-09 11:37:34 +00:00
|
|
|
* The instance just deleted.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
function hook_field_delete_instance($instance) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act on field records being read from the database.
|
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_read_fields() on each field being read.
|
|
|
|
*
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field record just read from the database.
|
|
|
|
*/
|
2011-01-18 18:11:30 +00:00
|
|
|
function hook_field_read_field($field) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Act on a field record being read from the database.
|
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_read_instances() on each instance being read.
|
|
|
|
*
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $instance
|
|
|
|
* The instance record just read from the database.
|
|
|
|
*/
|
|
|
|
function hook_field_read_instance($instance) {
|
2010-05-03 07:37:51 +00:00
|
|
|
// @todo Needs function body.
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-22 22:51:54 +00:00
|
|
|
/**
|
|
|
|
* Acts when a field record is being purged.
|
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* In field_purge_field(), after the field configuration has been removed from
|
|
|
|
* the database, the field storage module has had a chance to run its
|
|
|
|
* hook_field_storage_purge_field(), and the field info cache has been cleared,
|
|
|
|
* this hook is invoked on all modules to allow them to respond to the field
|
|
|
|
* being purged.
|
2010-04-22 22:51:54 +00:00
|
|
|
*
|
|
|
|
* @param $field
|
|
|
|
* The field being purged.
|
|
|
|
*/
|
|
|
|
function hook_field_purge_field($field) {
|
|
|
|
db_delete('my_module_field_info')
|
|
|
|
->condition('id', $field['id'])
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Acts when a field instance is being purged.
|
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* In field_purge_instance(), after the field instance has been removed from the
|
|
|
|
* database, the field storage module has had a chance to run its
|
|
|
|
* hook_field_storage_purge_instance(), and the field info cache has been
|
|
|
|
* cleared, this hook is invoked on all modules to allow them to respond to the
|
|
|
|
* field instance being purged.
|
2010-04-22 22:51:54 +00:00
|
|
|
*
|
|
|
|
* @param $instance
|
|
|
|
* The instance being purged.
|
|
|
|
*/
|
2011-10-25 08:11:30 +00:00
|
|
|
function hook_field_purge_instance($instance) {
|
2010-04-22 22:51:54 +00:00
|
|
|
db_delete('my_module_field_instance_info')
|
|
|
|
->condition('id', $instance['id'])
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
2010-04-22 23:38:17 +00:00
|
|
|
/**
|
|
|
|
* Remove field storage information when a field record is purged.
|
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* Called from field_purge_field() to allow the field storage module to remove
|
|
|
|
* field information when a field is being purged.
|
2010-04-22 23:38:17 +00:00
|
|
|
*
|
|
|
|
* @param $field
|
|
|
|
* The field being purged.
|
|
|
|
*/
|
|
|
|
function hook_field_storage_purge_field($field) {
|
|
|
|
$table_name = _field_sql_storage_tablename($field);
|
|
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
|
|
|
db_drop_table($table_name);
|
|
|
|
db_drop_table($revision_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove field storage information when a field instance is purged.
|
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* Called from field_purge_instance() to allow the field storage module to
|
|
|
|
* remove field instance information when a field instance is being purged.
|
2010-04-22 23:38:17 +00:00
|
|
|
*
|
|
|
|
* @param $instance
|
|
|
|
* The instance being purged.
|
|
|
|
*/
|
|
|
|
function hook_field_storage_purge_field_instance($instance) {
|
|
|
|
db_delete('my_module_field_instance_info')
|
|
|
|
->condition('id', $instance['id'])
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove field storage information when field data is purged.
|
|
|
|
*
|
2012-09-27 15:44:17 +00:00
|
|
|
* Called from field_purge_data() to allow the field storage module to delete
|
|
|
|
* field data information.
|
2010-04-22 23:38:17 +00:00
|
|
|
*
|
2013-01-07 11:22:28 +00:00
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
2010-04-22 23:38:17 +00:00
|
|
|
* The pseudo-entity whose field data to delete.
|
|
|
|
* @param $field
|
|
|
|
* The (possibly deleted) field whose data is being purged.
|
|
|
|
* @param $instance
|
|
|
|
* The deleted field instance whose data is being purged.
|
|
|
|
*/
|
2013-01-07 11:22:28 +00:00
|
|
|
function hook_field_storage_purge(\Drupal\Core\Entity\EntityInterface $entity, $field, $instance) {
|
2010-04-22 23:38:17 +00:00
|
|
|
$table_name = _field_sql_storage_tablename($field);
|
|
|
|
$revision_name = _field_sql_storage_revision_tablename($field);
|
|
|
|
db_delete($table_name)
|
2013-01-07 11:22:28 +00:00
|
|
|
->condition('entity_type', $entity->entityType())
|
2012-08-11 16:19:01 +00:00
|
|
|
->condition('entity_id', $entity->id())
|
2010-04-22 23:38:17 +00:00
|
|
|
->execute();
|
|
|
|
db_delete($revision_name)
|
2013-01-07 11:22:28 +00:00
|
|
|
->condition('entity_type', $entity->entityType())
|
2012-08-11 16:19:01 +00:00
|
|
|
->condition('entity_id', $entity->id())
|
2010-04-22 23:38:17 +00:00
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
2009-02-05 03:42:58 +00:00
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "addtogroup field_crud".
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether the user has access to a given field.
|
|
|
|
*
|
2010-05-03 07:37:51 +00:00
|
|
|
* This hook is invoked from field_access() to let modules block access to
|
|
|
|
* operations on fields. If no module returns FALSE, the operation is allowed.
|
|
|
|
*
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $op
|
2010-05-03 07:37:51 +00:00
|
|
|
* The operation to be performed. Possible values: 'edit', 'view'.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $field
|
|
|
|
* The field on which the operation is to be performed.
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity_type
|
2010-05-03 07:37:51 +00:00
|
|
|
* The type of $entity; for example, 'node' or 'user'.
|
2010-02-11 17:44:47 +00:00
|
|
|
* @param $entity
|
2010-02-12 05:38:10 +00:00
|
|
|
* (optional) The entity for the operation.
|
2009-02-05 03:42:58 +00:00
|
|
|
* @param $account
|
2010-05-03 07:37:51 +00:00
|
|
|
* (optional) The account to check; if not given use currently logged in user.
|
|
|
|
*
|
2009-02-05 03:42:58 +00:00
|
|
|
* @return
|
2010-05-03 07:37:51 +00:00
|
|
|
* TRUE if the operation is allowed, and FALSE if the operation is denied.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2010-02-11 17:44:47 +00:00
|
|
|
function hook_field_access($op, $field, $entity_type, $entity, $account) {
|
2010-07-25 02:45:27 +00:00
|
|
|
if ($field['field_name'] == 'field_of_interest' && $op == 'edit') {
|
|
|
|
return user_access('edit field of interest', $account);
|
|
|
|
}
|
|
|
|
return TRUE;
|
2010-04-30 19:24:03 +00:00
|
|
|
}
|
2011-11-25 03:35:12 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "addtogroup hooks".
|
2011-11-25 03:35:12 +00:00
|
|
|
*/
|