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
|
2013-06-17 11:03:40 +00:00
|
|
|
* and other elements exposed by modules through 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 hook_field_extra_fields_alter()
|
2009-08-19 13:31:14 +00:00
|
|
|
*
|
2013-05-19 20:02:16 +00:00
|
|
|
* @return array
|
2013-04-04 22:43:37 +00:00
|
|
|
* A nested array of 'pseudo-field' elements. Each list is nested within the
|
2012-09-27 15:44:17 +00:00
|
|
|
* 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:
|
2013-04-04 22:43:37 +00:00
|
|
|
* - label: The human readable name of the element.
|
|
|
|
* - description: A short description of the element contents.
|
2009-08-19 13:31:14 +00:00
|
|
|
* - weight: The default weight of the element.
|
2013-06-27 21:24:39 +00:00
|
|
|
* - visible: (optional) The default visibility of the element. Defaults to
|
|
|
|
* TRUE.
|
2013-04-20 03:47:11 +00:00
|
|
|
* - edit: (optional) String containing markup (normally a link) used as the
|
|
|
|
* element's 'edit' operation in the administration interface. Only for
|
2013-04-04 22:43:37 +00:00
|
|
|
* 'form' context.
|
2013-04-20 03:47:11 +00:00
|
|
|
* - delete: (optional) String containing markup (normally a link) used as the
|
|
|
|
* element's 'delete' operation in the administration interface. Only for
|
2013-04-04 22:43:37 +00:00
|
|
|
* 'form' 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();
|
2013-10-16 12:17:55 +00:00
|
|
|
$module_language_enabled = \Drupal::moduleHandler()->moduleExists('language');
|
2013-01-23 17:29:29 +00:00
|
|
|
$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
|
2013-06-25 10:27:47 +00:00
|
|
|
* hook_field_schema().
|
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
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-06-30 03:12:03 +00:00
|
|
|
/**
|
|
|
|
* Perform alterations on Field API field types.
|
|
|
|
*
|
|
|
|
* @param $info
|
2013-06-25 10:27:47 +00:00
|
|
|
* Array of information on field types as collected by the "field type" plugin
|
|
|
|
* manager.
|
2009-06-30 03:12:03 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
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
|
2013-10-18 11:57:37 +00:00
|
|
|
* Drupal\Core\Field\WidgetPluginManager class. A widget is
|
2012-09-26 19:39:39 +00:00
|
|
|
* implemented by providing a class that implements
|
2013-10-18 11:57:37 +00:00
|
|
|
* Drupal\Core\Field\WidgetInterface (in most cases, by
|
|
|
|
* subclassing Drupal\Core\Field\WidgetBase), and provides the
|
2012-09-26 19:39:39 +00:00
|
|
|
* 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
|
2013-06-16 08:27:11 +00:00
|
|
|
* An associative array containing the following key-value pairs:
|
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.
|
2013-06-16 08:27:11 +00:00
|
|
|
* - widget: The widget plugin instance.
|
2013-09-28 11:29:14 +00:00
|
|
|
* - items: The field values, as a
|
|
|
|
* \Drupal\Core\Entity\Field\FieldItemListInterface object.
|
2012-04-17 22:13:57 +00:00
|
|
|
* - 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
|
|
|
*
|
2013-10-18 11:57:37 +00:00
|
|
|
* @see \Drupal\Core\Field\WidgetBase::formSingleElement()
|
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.
|
2013-09-16 15:52:28 +00:00
|
|
|
$field_definition = $context['items']->getFieldDefinition();
|
|
|
|
if ($field_definition->getFieldType() == 'mytype') {
|
2011-08-29 18:00:33 +00:00
|
|
|
// 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
|
2013-06-16 08:27:11 +00:00
|
|
|
* An associative array. See hook_field_widget_form_alter() for the structure
|
|
|
|
* and content of the array.
|
2011-08-29 18:00:33 +00:00
|
|
|
*
|
2013-10-18 11:57:37 +00:00
|
|
|
* @see \Drupal\Core\Field\WidgetBase::formSingleElement()
|
2011-08-29 18:00:33 +00:00
|
|
|
* @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'.
|
2013-10-15 11:54:21 +00:00
|
|
|
$element['#autocomplete_route_name'] = 'mymodule.autocomplete_route';
|
2011-08-29 18:00:33 +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
|
2013-10-18 11:57:37 +00:00
|
|
|
* Drupal\Core\Field\FormatterPluginManager class. A formatter
|
2012-10-14 06:04:15 +00:00
|
|
|
* is implemented by providing a class that implements
|
2013-10-18 11:57:37 +00:00
|
|
|
* Drupal\Core\Field\FormatterInterface (in most cases, by
|
|
|
|
* subclassing Drupal\Core\Field\FormatterBase), and provides
|
2012-10-14 06:04:15 +00:00
|
|
|
* 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.
|
2013-06-27 02:19:04 +00:00
|
|
|
*
|
|
|
|
* @deprecated as of Drupal 8.0. Use the entity system instead.
|
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.
|
2013-02-04 14:30:22 +00:00
|
|
|
// See hook_field_attach_extract_form_values() for the corresponding
|
|
|
|
// processing code.
|
2010-11-20 19:57:01 +00:00
|
|
|
$form['empty_field_foo'] = array(
|
|
|
|
'#type' => 'checkbox',
|
|
|
|
'#title' => t("Empty the 'field_foo' field"),
|
|
|
|
);
|
2009-02-05 03:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-04 14:30:22 +00:00
|
|
|
* Act on field_attach_extract_form_values().
|
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.
|
2013-06-27 02:19:04 +00:00
|
|
|
*
|
|
|
|
* @deprecated as of Drupal 8.0. Use the entity system instead.
|
2009-02-05 03:42:58 +00:00
|
|
|
*/
|
2013-02-04 14:30:22 +00:00
|
|
|
function hook_field_attach_extract_form_values(\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
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2013-05-24 17:37:11 +00:00
|
|
|
* This hook is invoked while preprocessing field templates in
|
2012-09-27 15:44:17 +00:00
|
|
|
* 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-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.
|
2013-05-09 20:43:04 +00:00
|
|
|
* - langcode: The language code used for rendering.
|
2013-06-27 02:19:04 +00:00
|
|
|
*
|
|
|
|
* @deprecated as of Drupal 8.0. Use the entity system instead.
|
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) {
|
2013-03-10 19:05:24 +00:00
|
|
|
$term = $item['entity'];
|
2010-07-25 02:45:27 +00:00
|
|
|
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-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-04-22 21:57:38 +00:00
|
|
|
if (field_language_fallback_enabled() && 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-17 12:58:49 +00:00
|
|
|
* @} End of "addtogroup field_attach".
|
2009-02-05 03:42:58 +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).
|
|
|
|
*
|
2013-05-19 20:02:16 +00:00
|
|
|
* @param string $entity_type
|
2010-09-11 00:03:42 +00:00
|
|
|
* The type of entity; e.g. 'node' or 'user'.
|
2013-05-19 20:02:16 +00:00
|
|
|
* @param string $bundle
|
2010-09-11 00:03:42 +00:00
|
|
|
* The bundle name.
|
2013-05-19 20:02:16 +00:00
|
|
|
* @param string $context
|
|
|
|
* The context for which the maximum weight is requested. Either 'form' or
|
|
|
|
* 'display'.
|
|
|
|
* @param string $context_mode
|
|
|
|
* The view or form mode name.
|
|
|
|
*
|
|
|
|
* @return int
|
2010-09-11 00:03:42 +00:00
|
|
|
* The maximum weight of the entity's components, or NULL if no components
|
|
|
|
* were found.
|
|
|
|
*/
|
2013-05-19 20:02:16 +00:00
|
|
|
function hook_field_info_max_weight($entity_type, $bundle, $context, $context_mode) {
|
2010-09-11 00:03:42 +00:00
|
|
|
$weights = array();
|
|
|
|
|
2013-05-19 20:02:16 +00:00
|
|
|
foreach (my_module_entity_additions($entity_type, $bundle, $context, $context_mode) as $addition) {
|
2010-09-11 00:03:42 +00:00
|
|
|
$weights[] = $addition['weight'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $weights ? max($weights) : NULL;
|
|
|
|
}
|
|
|
|
|
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-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.
|
|
|
|
*/
|
2013-06-17 00:22:30 +00:00
|
|
|
function hook_field_update_forbid($field, $prior_field) {
|
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.
|
2013-06-17 00:22:30 +00:00
|
|
|
if ($field->hasData() && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) {
|
2009-09-26 15:57:39 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 22:51:54 +00:00
|
|
|
/**
|
|
|
|
* Acts when a field record is being purged.
|
|
|
|
*
|
2013-09-02 20:26:08 +00:00
|
|
|
* In field_purge_field(), after the field definition has been removed from the
|
|
|
|
* the system, the entity storage has purged stored field data, 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.
|
|
|
|
*
|
2013-09-02 20:26:08 +00:00
|
|
|
* In field_purge_instance(), after the instance definition has been removed
|
|
|
|
* from the the system, the entity storage has purged stored field data, 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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
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
|
|
|
*/
|