danielebarchiesi@0: array( danielebarchiesi@0: 'choice_wrapper' => array( danielebarchiesi@0: 'label' => t('Poll choices'), danielebarchiesi@0: 'description' => t('Poll choices'), danielebarchiesi@0: 'weight' => -4, danielebarchiesi@0: ), danielebarchiesi@0: 'settings' => array( danielebarchiesi@0: 'label' => t('Poll settings'), danielebarchiesi@0: 'description' => t('Poll module settings'), danielebarchiesi@0: 'weight' => -3, danielebarchiesi@0: ), danielebarchiesi@0: ), danielebarchiesi@0: 'display' => array( danielebarchiesi@0: 'poll_view_voting' => array( danielebarchiesi@0: 'label' => t('Poll vote'), danielebarchiesi@0: 'description' => t('Poll vote'), danielebarchiesi@0: 'weight' => 0, danielebarchiesi@0: ), danielebarchiesi@0: 'poll_view_results' => array( danielebarchiesi@0: 'label' => t('Poll results'), danielebarchiesi@0: 'description' => t('Poll results'), danielebarchiesi@0: 'weight' => 0, danielebarchiesi@0: ), danielebarchiesi@0: ) danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: return $extra; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter "pseudo-field" components on fieldable entities. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * The associative array of 'pseudo-field' components. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_extra_fields() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_extra_fields_alter(&$info) { danielebarchiesi@0: // Force node title to always be at the top of the list by default. danielebarchiesi@0: foreach (node_type_get_types() as $bundle) { danielebarchiesi@0: if (isset($info['node'][$bundle->type]['form']['title'])) { danielebarchiesi@0: $info['node'][$bundle->type]['form']['title']['weight'] = -20; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @defgroup field_types Field Types API danielebarchiesi@0: * @{ danielebarchiesi@0: * Define field types. danielebarchiesi@0: * danielebarchiesi@0: * In the Field API, each field has a type, which determines what kind of data danielebarchiesi@0: * (integer, string, date, etc.) the field can hold, which settings it provides, danielebarchiesi@0: * and so on. The data type(s) accepted by a field are defined in danielebarchiesi@0: * hook_field_schema(); other basic properties of a field are defined in danielebarchiesi@0: * hook_field_info(). The other hooks below are called by the Field Attach API danielebarchiesi@0: * to perform field-type-specific actions. danielebarchiesi@0: * danielebarchiesi@0: * The Field Types API also defines two kinds of pluggable handlers: widgets danielebarchiesi@0: * and formatters. @link field_widget Widgets @endlink specify how the field danielebarchiesi@0: * appears in edit forms, while @link field_formatter formatters @endlink danielebarchiesi@0: * specify how the field appears in displayed entities. danielebarchiesi@0: * danielebarchiesi@0: * A third kind of pluggable handlers, storage backends, is defined by the danielebarchiesi@0: * @link field_storage Field Storage API @endlink. danielebarchiesi@0: * danielebarchiesi@0: * See @link field Field API @endlink for information about the other parts of danielebarchiesi@0: * the Field API. danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define Field API field types. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array whose keys are field type names and whose values are arrays danielebarchiesi@0: * describing the field type, with the following key/value pairs: danielebarchiesi@0: * - label: The human-readable name of the field type. danielebarchiesi@0: * - description: A short description for the field type. danielebarchiesi@0: * - settings: An array whose keys are the names of the settings available danielebarchiesi@0: * for the field type, and whose values are the default values for those danielebarchiesi@0: * settings. danielebarchiesi@0: * - instance_settings: An array whose keys are the names of the settings danielebarchiesi@0: * available for instances of the field type, and whose values are the danielebarchiesi@0: * default values for those settings. Instance-level settings can have danielebarchiesi@0: * different values on each field instance, and thus allow greater danielebarchiesi@0: * flexibility than field-level settings. It is recommended to put settings danielebarchiesi@0: * at the instance level whenever possible. Notable exceptions: settings danielebarchiesi@0: * acting on the schema definition, or settings that Views needs to use danielebarchiesi@0: * across field instances (for example, the list of allowed values). danielebarchiesi@0: * - default_widget: The machine name of the default widget to be used by danielebarchiesi@0: * instances of this field type, when no widget is specified in the danielebarchiesi@0: * instance definition. This widget must be available whenever the field danielebarchiesi@0: * type is available (i.e. provided by the field type module, or by a module danielebarchiesi@0: * the field type module depends on). danielebarchiesi@0: * - default_formatter: The machine name of the default formatter to be used danielebarchiesi@0: * by instances of this field type, when no formatter is specified in the danielebarchiesi@0: * instance definition. This formatter must be available whenever the field danielebarchiesi@0: * type is available (i.e. provided by the field type module, or by a module danielebarchiesi@0: * the field type module depends on). danielebarchiesi@0: * - no_ui: (optional) A boolean specifying that users should not be allowed danielebarchiesi@0: * to create fields and instances of this field type through the UI. Such danielebarchiesi@0: * fields can only be created programmatically with field_create_field() danielebarchiesi@0: * and field_create_instance(). Defaults to FALSE. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_info_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_info() { danielebarchiesi@0: return array( danielebarchiesi@0: 'text' => array( danielebarchiesi@0: 'label' => t('Text'), danielebarchiesi@0: 'description' => t('This field stores varchar text in the database.'), danielebarchiesi@0: 'settings' => array('max_length' => 255), danielebarchiesi@0: 'instance_settings' => array('text_processing' => 0), danielebarchiesi@0: 'default_widget' => 'text_textfield', danielebarchiesi@0: 'default_formatter' => 'text_default', danielebarchiesi@0: ), danielebarchiesi@0: 'text_long' => array( danielebarchiesi@0: 'label' => t('Long text'), danielebarchiesi@0: 'description' => t('This field stores long text in the database.'), danielebarchiesi@0: 'settings' => array('max_length' => ''), danielebarchiesi@0: 'instance_settings' => array('text_processing' => 0), danielebarchiesi@0: 'default_widget' => 'text_textarea', danielebarchiesi@0: 'default_formatter' => 'text_default', danielebarchiesi@0: ), danielebarchiesi@0: 'text_with_summary' => array( danielebarchiesi@0: 'label' => t('Long text and summary'), danielebarchiesi@0: 'description' => t('This field stores long text in the database along with optional summary text.'), danielebarchiesi@0: 'settings' => array('max_length' => ''), danielebarchiesi@0: 'instance_settings' => array('text_processing' => 1, 'display_summary' => 0), danielebarchiesi@0: 'default_widget' => 'text_textarea_with_summary', danielebarchiesi@0: 'default_formatter' => 'text_summary_or_trimmed', danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on Field API field types. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * Array of information on field types exposed by hook_field_info() danielebarchiesi@0: * implementations. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_info_alter(&$info) { danielebarchiesi@0: // Add a setting to all field types. danielebarchiesi@0: foreach ($info as $field_type => $field_type_info) { danielebarchiesi@0: $info[$field_type]['settings'] += array( danielebarchiesi@0: 'mymodule_additional_setting' => 'default value', danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Change the default widget for fields of type 'foo'. danielebarchiesi@0: if (isset($info['foo'])) { danielebarchiesi@0: $info['foo']['default widget'] = 'mymodule_widget'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define the Field API schema for a field structure. danielebarchiesi@0: * danielebarchiesi@0: * This hook MUST be defined in .install for it to be detected during danielebarchiesi@0: * installation and upgrade. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * A field structure. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An associative array with the following keys: danielebarchiesi@0: * - columns: An array of Schema API column specifications, keyed by column danielebarchiesi@0: * name. This specifies what comprises a value for a given field. For danielebarchiesi@0: * example, a value for a number field is simply 'value', while a value for danielebarchiesi@0: * a formatted text field is the combination of 'value' and 'format'. It is danielebarchiesi@0: * recommended to avoid having the column definitions depend on field danielebarchiesi@0: * settings when possible. No assumptions should be made on how storage danielebarchiesi@0: * engines internally use the original column name to structure their danielebarchiesi@0: * storage. danielebarchiesi@0: * - indexes: (optional) An array of Schema API indexes definitions. Only danielebarchiesi@0: * columns that appear in the 'columns' array are allowed. Those indexes danielebarchiesi@0: * will be used as default indexes. Callers of field_create_field() can danielebarchiesi@0: * specify additional indexes, or, at their own risk, modify the default danielebarchiesi@0: * indexes specified by the field-type module. Some storage engines might danielebarchiesi@0: * not support indexes. danielebarchiesi@0: * - foreign keys: (optional) An array of Schema API foreign keys danielebarchiesi@0: * definitions. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_schema($field) { danielebarchiesi@0: if ($field['type'] == 'text_long') { danielebarchiesi@0: $columns = array( danielebarchiesi@0: 'value' => array( danielebarchiesi@0: 'type' => 'text', danielebarchiesi@0: 'size' => 'big', danielebarchiesi@0: 'not null' => FALSE, danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $columns = array( danielebarchiesi@0: 'value' => array( danielebarchiesi@0: 'type' => 'varchar', danielebarchiesi@0: 'length' => $field['settings']['max_length'], danielebarchiesi@0: 'not null' => FALSE, danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: $columns += array( danielebarchiesi@0: 'format' => array( danielebarchiesi@0: 'type' => 'varchar', danielebarchiesi@0: 'length' => 255, danielebarchiesi@0: 'not null' => FALSE, danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: return array( danielebarchiesi@0: 'columns' => $columns, danielebarchiesi@0: 'indexes' => array( danielebarchiesi@0: 'format' => array('format'), danielebarchiesi@0: ), danielebarchiesi@0: 'foreign keys' => array( danielebarchiesi@0: 'format' => array( danielebarchiesi@0: 'table' => 'filter_format', danielebarchiesi@0: 'columns' => array('format' => 'format'), danielebarchiesi@0: ), danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define custom load behavior for this module's field types. danielebarchiesi@0: * danielebarchiesi@0: * Unlike most other field hooks, this hook operates on multiple entities. The danielebarchiesi@0: * $entities, $instances and $items parameters are arrays keyed by entity ID. danielebarchiesi@0: * For performance reasons, information for all available entity should be danielebarchiesi@0: * loaded in a single query where possible. danielebarchiesi@0: * danielebarchiesi@0: * Note that the changes made to the field values get cached by the field cache danielebarchiesi@0: * for subsequent loads. You should never use this hook to load fieldable danielebarchiesi@0: * entities, since this is likely to cause infinite recursions when danielebarchiesi@0: * hook_field_load() is run on those as well. Use danielebarchiesi@0: * hook_field_formatter_prepare_view() instead. danielebarchiesi@0: * danielebarchiesi@0: * Make changes or additions to field values by altering the $items parameter by danielebarchiesi@0: * reference. There is no return value. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entities danielebarchiesi@0: * Array of entities being loaded, keyed by entity ID. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instances danielebarchiesi@0: * Array of instance structures for $field for each entity, keyed by entity danielebarchiesi@0: * ID. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language code associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * Array of field values already loaded for the entities, keyed by entity ID. danielebarchiesi@0: * Store your changes in this parameter (passed by reference). danielebarchiesi@0: * @param $age danielebarchiesi@0: * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or danielebarchiesi@0: * FIELD_LOAD_REVISION to load the version indicated by each entity. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) { danielebarchiesi@0: // Sample code from text.module: precompute sanitized strings so they are danielebarchiesi@0: // stored in the field cache. danielebarchiesi@0: foreach ($entities as $id => $entity) { danielebarchiesi@0: foreach ($items[$id] as $delta => $item) { danielebarchiesi@0: // Only process items with a cacheable format, the rest will be handled danielebarchiesi@0: // by formatters if needed. danielebarchiesi@0: if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) { danielebarchiesi@0: $items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : ''; danielebarchiesi@0: if ($field['type'] == 'text_with_summary') { danielebarchiesi@0: $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : ''; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Prepare field values prior to display. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked before the field values are handed to formatters danielebarchiesi@0: * for display, and runs before the formatters' own danielebarchiesi@0: * hook_field_formatter_prepare_view(). danielebarchiesi@0: * danielebarchiesi@0: * Unlike most other field hooks, this hook operates on multiple entities. The danielebarchiesi@0: * $entities, $instances and $items parameters are arrays keyed by entity ID. danielebarchiesi@0: * For performance reasons, information for all available entities should be danielebarchiesi@0: * loaded in a single query where possible. danielebarchiesi@0: * danielebarchiesi@0: * Make changes or additions to field values by altering the $items parameter by danielebarchiesi@0: * reference. There is no return value. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entities danielebarchiesi@0: * Array of entities being displayed, keyed by entity ID. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instances danielebarchiesi@0: * Array of instance structures for $field for each entity, keyed by entity danielebarchiesi@0: * ID. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated to $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}, or an empty array if unset. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) { danielebarchiesi@0: // Sample code from image.module: if there are no images specified at all, danielebarchiesi@0: // use the default image. danielebarchiesi@0: foreach ($entities as $id => $entity) { danielebarchiesi@0: if (empty($items[$id]) && $field['settings']['default_image']) { danielebarchiesi@0: if ($file = file_load($field['settings']['default_image'])) { danielebarchiesi@0: $items[$id][0] = (array) $file + array( danielebarchiesi@0: 'is_default' => TRUE, danielebarchiesi@0: 'alt' => '', danielebarchiesi@0: 'title' => '', danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Validate this module's field data. danielebarchiesi@0: * danielebarchiesi@0: * If there are validation problems, add to the $errors array (passed by danielebarchiesi@0: * reference). There is no return value. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for the operation. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance structure for $field on $entity's bundle. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}[$langcode], or an empty array if unset. danielebarchiesi@0: * @param $errors danielebarchiesi@0: * The array of errors (keyed by field name, language code, and delta) that danielebarchiesi@0: * have already been reported for the entity. The function should add its danielebarchiesi@0: * errors to this array. Each error is an associative array with the following danielebarchiesi@0: * keys and values: danielebarchiesi@0: * - error: An error code (should be a string prefixed with the module name). danielebarchiesi@0: * - message: The human readable message to be displayed. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: if (!empty($item['value'])) { danielebarchiesi@0: if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) { danielebarchiesi@0: $errors[$field['field_name']][$langcode][$delta][] = array( danielebarchiesi@0: 'error' => 'text_max_length', danielebarchiesi@0: 'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define custom presave behavior for this module's field types. danielebarchiesi@0: * danielebarchiesi@0: * Make changes or additions to field values by altering the $items parameter by danielebarchiesi@0: * reference. There is no return value. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for the operation. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance structure for $field on $entity's bundle. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}[$langcode], or an empty array if unset. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { danielebarchiesi@0: if ($field['type'] == 'number_decimal') { danielebarchiesi@0: // Let PHP round the value to ensure consistent behavior across storage danielebarchiesi@0: // backends. danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: if (isset($item['value'])) { danielebarchiesi@0: $items[$delta]['value'] = round($item['value'], $field['settings']['scale']); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define custom insert behavior for this module's field data. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_attach_insert() on the module that defines a danielebarchiesi@0: * field, during the process of inserting an entity object (node, taxonomy term, danielebarchiesi@0: * etc.). It is invoked just before the data for this field on the particular danielebarchiesi@0: * entity object is inserted into field storage. Only field modules that are danielebarchiesi@0: * storing or tracking information outside the standard field storage mechanism danielebarchiesi@0: * need to implement this hook. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for the operation. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance structure for $field on $entity's bundle. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}[$langcode], or an empty array if unset. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_update() danielebarchiesi@0: * @see hook_field_delete() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) { danielebarchiesi@0: if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node' && $entity->status) { danielebarchiesi@0: $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', )); danielebarchiesi@0: foreach ($items as $item) { danielebarchiesi@0: $query->values(array( danielebarchiesi@0: 'nid' => $entity->nid, danielebarchiesi@0: 'tid' => $item['tid'], danielebarchiesi@0: 'sticky' => $entity->sticky, danielebarchiesi@0: 'created' => $entity->created, danielebarchiesi@0: )); danielebarchiesi@0: } danielebarchiesi@0: $query->execute(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define custom update behavior for this module's field data. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_attach_update() on the module that defines a danielebarchiesi@0: * field, during the process of updating an entity object (node, taxonomy term, danielebarchiesi@0: * etc.). It is invoked just before the data for this field on the particular danielebarchiesi@0: * entity object is updated into field storage. Only field modules that are danielebarchiesi@0: * storing or tracking information outside the standard field storage mechanism danielebarchiesi@0: * need to implement this hook. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for the operation. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance structure for $field on $entity's bundle. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}[$langcode], or an empty array if unset. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_insert() danielebarchiesi@0: * @see hook_field_delete() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) { danielebarchiesi@0: if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') { danielebarchiesi@0: $first_call = &drupal_static(__FUNCTION__, array()); danielebarchiesi@0: danielebarchiesi@0: // We don't maintain data for old revisions, so clear all previous values danielebarchiesi@0: // from the table. Since this hook runs once per field, per object, make danielebarchiesi@0: // sure we only wipe values once. danielebarchiesi@0: if (!isset($first_call[$entity->nid])) { danielebarchiesi@0: $first_call[$entity->nid] = FALSE; danielebarchiesi@0: db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute(); danielebarchiesi@0: } danielebarchiesi@0: // Only save data to the table if the node is published. danielebarchiesi@0: if ($entity->status) { danielebarchiesi@0: $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created')); danielebarchiesi@0: foreach ($items as $item) { danielebarchiesi@0: $query->values(array( danielebarchiesi@0: 'nid' => $entity->nid, danielebarchiesi@0: 'tid' => $item['tid'], danielebarchiesi@0: 'sticky' => $entity->sticky, danielebarchiesi@0: 'created' => $entity->created, danielebarchiesi@0: )); danielebarchiesi@0: } danielebarchiesi@0: $query->execute(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Update the storage information for a field. danielebarchiesi@0: * danielebarchiesi@0: * This is invoked on the field's storage module from field_update_field(), danielebarchiesi@0: * before the new field information is saved to the database. The field storage danielebarchiesi@0: * module should update its storage tables to agree with the new field danielebarchiesi@0: * information. If there is a problem, the field storage module should throw an danielebarchiesi@0: * exception. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The updated field structure to be saved. danielebarchiesi@0: * @param $prior_field danielebarchiesi@0: * The previously-saved field structure. danielebarchiesi@0: * @param $has_data danielebarchiesi@0: * TRUE if the field has data in storage currently. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_update_field($field, $prior_field, $has_data) { danielebarchiesi@0: if (!$has_data) { danielebarchiesi@0: // There is no data. Re-create the tables completely. danielebarchiesi@0: $prior_schema = _field_sql_storage_schema($prior_field); danielebarchiesi@0: foreach ($prior_schema as $name => $table) { danielebarchiesi@0: db_drop_table($name, $table); danielebarchiesi@0: } danielebarchiesi@0: $schema = _field_sql_storage_schema($field); danielebarchiesi@0: foreach ($schema as $name => $table) { danielebarchiesi@0: db_create_table($name, $table); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // There is data. See field_sql_storage_field_storage_update_field() for danielebarchiesi@0: // an example of what to do to modify the schema in place, preserving the danielebarchiesi@0: // old data as much as possible. danielebarchiesi@0: } danielebarchiesi@0: drupal_get_schema(NULL, TRUE); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define custom delete behavior for this module's field data. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_attach_delete() on the module that defines a danielebarchiesi@0: * field, during the process of deleting an entity object (node, taxonomy term, danielebarchiesi@0: * etc.). It is invoked just before the data for this field on the particular danielebarchiesi@0: * entity object is deleted from field storage. Only field modules that are danielebarchiesi@0: * storing or tracking information outside the standard field storage mechanism danielebarchiesi@0: * need to implement this hook. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for the operation. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance structure for $field on $entity's bundle. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}[$langcode], or an empty array if unset. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_insert() danielebarchiesi@0: * @see hook_field_update() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) { danielebarchiesi@0: list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: // For hook_file_references(), remember that this is being deleted. danielebarchiesi@0: $item['file_field_name'] = $field['field_name']; danielebarchiesi@0: // Pass in the ID of the object that is being removed so all references can danielebarchiesi@0: // be counted in hook_file_references(). danielebarchiesi@0: $item['file_field_type'] = $entity_type; danielebarchiesi@0: $item['file_field_id'] = $id; danielebarchiesi@0: file_field_delete_file($item, $field, $entity_type, $id); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define custom revision delete behavior for this module's field types. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked just before the data is deleted from field storage danielebarchiesi@0: * in field_attach_delete_revision(), and will only be called for fieldable danielebarchiesi@0: * types that are versioned. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for the operation. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance structure for $field on $entity's bundle. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}[$langcode], or an empty array if unset. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) { danielebarchiesi@0: list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: // For hook_file_references, remember that this file is being deleted. danielebarchiesi@0: $item['file_field_name'] = $field['field_name']; danielebarchiesi@0: if (file_field_delete_file($item, $field, $entity_type, $id)) { danielebarchiesi@0: $items[$delta] = NULL; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define custom prepare_translation behavior for this module's field types. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for the operation. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance structure for $field on $entity's bundle. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated to $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * $entity->{$field['field_name']}[$langcode], or an empty array if unset. danielebarchiesi@0: * @param $source_entity danielebarchiesi@0: * The source entity from which field values are being copied. danielebarchiesi@0: * @param $source_langcode danielebarchiesi@0: * The source language from which field values are being copied. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) { danielebarchiesi@0: // If the translating user is not permitted to use the assigned text format, danielebarchiesi@0: // we must not expose the source values. danielebarchiesi@0: $field_name = $field['field_name']; danielebarchiesi@0: $formats = filter_formats(); danielebarchiesi@0: $format_id = $source_entity->{$field_name}[$source_langcode][0]['format']; danielebarchiesi@0: if (!filter_access($formats[$format_id])) { danielebarchiesi@0: $items = array(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define what constitutes an empty item for a field type. danielebarchiesi@0: * danielebarchiesi@0: * @param $item danielebarchiesi@0: * An item that may or may not be empty. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field to which $item belongs. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if $field's type considers $item not to contain any data; danielebarchiesi@0: * FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_is_empty($item, $field) { danielebarchiesi@0: if (empty($item['value']) && (string) $item['value'] !== '0') { danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "defgroup field_types". danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @defgroup field_widget Field Widget API danielebarchiesi@0: * @{ danielebarchiesi@0: * Define Field API widget types. danielebarchiesi@0: * danielebarchiesi@0: * Field API widgets specify how fields are displayed in edit forms. Fields of a danielebarchiesi@0: * given @link field_types field type @endlink may be edited using more than one danielebarchiesi@0: * widget. In this case, the Field UI module allows the site builder to choose danielebarchiesi@0: * which widget to use. Widget types are defined by implementing danielebarchiesi@0: * hook_field_widget_info(). danielebarchiesi@0: * danielebarchiesi@0: * Widgets are @link forms_api_reference.html Form API @endlink elements with danielebarchiesi@0: * additional processing capabilities. Widget hooks are typically called by the danielebarchiesi@0: * Field Attach API during the creation of the field form structure with danielebarchiesi@0: * field_attach_form(). danielebarchiesi@0: * danielebarchiesi@0: * @see field danielebarchiesi@0: * @see field_types danielebarchiesi@0: * @see field_formatter danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Expose Field API widget types. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array describing the widget types implemented by the module. danielebarchiesi@0: * The keys are widget type names. To avoid name clashes, widget type danielebarchiesi@0: * names should be prefixed with the name of the module that exposes them. danielebarchiesi@0: * The values are arrays describing the widget type, with the following danielebarchiesi@0: * key/value pairs: danielebarchiesi@0: * - label: The human-readable name of the widget type. danielebarchiesi@0: * - description: A short description for the widget type. danielebarchiesi@0: * - field types: An array of field types the widget supports. danielebarchiesi@0: * - settings: An array whose keys are the names of the settings available danielebarchiesi@0: * for the widget type, and whose values are the default values for those danielebarchiesi@0: * settings. danielebarchiesi@0: * - behaviors: (optional) An array describing behaviors of the widget, with danielebarchiesi@0: * the following elements: danielebarchiesi@0: * - multiple values: One of the following constants: danielebarchiesi@0: * - FIELD_BEHAVIOR_DEFAULT: (default) If the widget allows the input of danielebarchiesi@0: * one single field value (most common case). The widget will be danielebarchiesi@0: * repeated for each value input. danielebarchiesi@0: * - FIELD_BEHAVIOR_CUSTOM: If one single copy of the widget can receive danielebarchiesi@0: * several field values. Examples: checkboxes, multiple select, danielebarchiesi@0: * comma-separated textfield. danielebarchiesi@0: * - default value: One of the following constants: danielebarchiesi@0: * - FIELD_BEHAVIOR_DEFAULT: (default) If the widget accepts default danielebarchiesi@0: * values. danielebarchiesi@0: * - FIELD_BEHAVIOR_NONE: if the widget does not support default values. danielebarchiesi@0: * - weight: (optional) An integer to determine the weight of this widget danielebarchiesi@0: * relative to other widgets in the Field UI when selecting a widget for a danielebarchiesi@0: * given field instance. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_widget_info_alter() danielebarchiesi@0: * @see hook_field_widget_form() danielebarchiesi@0: * @see hook_field_widget_form_alter() danielebarchiesi@0: * @see hook_field_widget_WIDGET_TYPE_form_alter() danielebarchiesi@0: * @see hook_field_widget_error() danielebarchiesi@0: * @see hook_field_widget_settings_form() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_info() { danielebarchiesi@0: return array( danielebarchiesi@0: 'text_textfield' => array( danielebarchiesi@0: 'label' => t('Text field'), danielebarchiesi@0: 'field types' => array('text'), danielebarchiesi@0: 'settings' => array('size' => 60), danielebarchiesi@0: 'behaviors' => array( danielebarchiesi@0: 'multiple values' => FIELD_BEHAVIOR_DEFAULT, danielebarchiesi@0: 'default value' => FIELD_BEHAVIOR_DEFAULT, danielebarchiesi@0: ), danielebarchiesi@0: ), danielebarchiesi@0: 'text_textarea' => array( danielebarchiesi@0: 'label' => t('Text area (multiple rows)'), danielebarchiesi@0: 'field types' => array('text_long'), danielebarchiesi@0: 'settings' => array('rows' => 5), danielebarchiesi@0: 'behaviors' => array( danielebarchiesi@0: 'multiple values' => FIELD_BEHAVIOR_DEFAULT, danielebarchiesi@0: 'default value' => FIELD_BEHAVIOR_DEFAULT, danielebarchiesi@0: ), danielebarchiesi@0: ), danielebarchiesi@0: 'text_textarea_with_summary' => array( danielebarchiesi@0: 'label' => t('Text area with a summary'), danielebarchiesi@0: 'field types' => array('text_with_summary'), danielebarchiesi@0: 'settings' => array('rows' => 20, 'summary_rows' => 5), danielebarchiesi@0: 'behaviors' => array( danielebarchiesi@0: 'multiple values' => FIELD_BEHAVIOR_DEFAULT, danielebarchiesi@0: 'default value' => FIELD_BEHAVIOR_DEFAULT, danielebarchiesi@0: ), danielebarchiesi@0: // As an advanced widget, force it to sink to the bottom of the choices. danielebarchiesi@0: 'weight' => 2, danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on Field API widget types. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * Array of informations on widget types exposed by hook_field_widget_info() danielebarchiesi@0: * implementations. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_info_alter(&$info) { danielebarchiesi@0: // Add a setting to a widget type. danielebarchiesi@0: $info['text_textfield']['settings'] += array( danielebarchiesi@0: 'mymodule_additional_setting' => 'default value', danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: // Let a new field type re-use an existing widget. danielebarchiesi@0: $info['options_select']['field types'][] = 'my_field_type'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Return the form for a single field widget. danielebarchiesi@0: * danielebarchiesi@0: * Field widget form elements should be based on the passed-in $element, which danielebarchiesi@0: * contains the base form element properties derived from the field danielebarchiesi@0: * configuration. danielebarchiesi@0: * danielebarchiesi@0: * Field API will set the weight, field name and delta values for each form danielebarchiesi@0: * element. If there are multiple values for this field, the Field API will danielebarchiesi@0: * invoke this hook as many times as needed. danielebarchiesi@0: * danielebarchiesi@0: * Note that, depending on the context in which the widget is being included danielebarchiesi@0: * (regular entity form, field configuration form, advanced search form...), danielebarchiesi@0: * the values for $field and $instance might be different from the "official" danielebarchiesi@0: * definitions returned by field_info_field() and field_info_instance(). danielebarchiesi@0: * Examples: mono-value widget even if the field is multi-valued, non-required danielebarchiesi@0: * widget even if the field is 'required'... danielebarchiesi@0: * danielebarchiesi@0: * Therefore, the FAPI element callbacks (such as #process, #element_validate, danielebarchiesi@0: * #value_callback...) used by the widget cannot use the field_info_field() danielebarchiesi@0: * or field_info_instance() functions to retrieve the $field or $instance danielebarchiesi@0: * definitions they should operate on. The field_widget_field() and danielebarchiesi@0: * field_widget_instance() functions should be used instead to fetch the danielebarchiesi@0: * current working definitions from $form_state, where Field API stores them. danielebarchiesi@0: * danielebarchiesi@0: * Alternatively, hook_field_widget_form() can extract the needed specific danielebarchiesi@0: * properties from $field and $instance and set them as ad-hoc danielebarchiesi@0: * $element['#custom'] properties, for later use by its element callbacks. danielebarchiesi@0: * danielebarchiesi@0: * Other modules may alter the form element provided by this function using danielebarchiesi@0: * hook_field_widget_form_alter(). danielebarchiesi@0: * danielebarchiesi@0: * @param $form danielebarchiesi@0: * The form structure where widgets are being attached to. This might be a danielebarchiesi@0: * full form structure, or a sub-element of a larger form. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * An associative array containing the current state of the form. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The field instance. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * Array of default values for this field. danielebarchiesi@0: * @param $delta danielebarchiesi@0: * The order of this item in the array of subelements (0, 1, 2, etc). danielebarchiesi@0: * @param $element danielebarchiesi@0: * A form element array containing basic properties for the widget: danielebarchiesi@0: * - #entity_type: The name of the entity the field is attached to. danielebarchiesi@0: * - #bundle: The name of the field bundle the field is contained in. danielebarchiesi@0: * - #field_name: The name of the field. danielebarchiesi@0: * - #language: The language the field is being edited in. danielebarchiesi@0: * - #field_parents: The 'parents' space for the field in the form. Most danielebarchiesi@0: * widgets can simply overlook this property. This identifies the danielebarchiesi@0: * location where the field values are placed within danielebarchiesi@0: * $form_state['values'], and is used to access processing information danielebarchiesi@0: * for the field through the field_form_get_state() and danielebarchiesi@0: * field_form_set_state() functions. danielebarchiesi@0: * - #columns: A list of field storage columns of the field. danielebarchiesi@0: * - #title: The sanitized element label for the field instance, ready for danielebarchiesi@0: * output. danielebarchiesi@0: * - #description: The sanitized element description for the field instance, danielebarchiesi@0: * ready for output. danielebarchiesi@0: * - #required: A Boolean indicating whether the element value is required; danielebarchiesi@0: * for required multiple value fields, only the first widget's values are danielebarchiesi@0: * required. danielebarchiesi@0: * - #delta: The order of this item in the array of subelements; see $delta danielebarchiesi@0: * above. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The form elements for a single widget for this field. danielebarchiesi@0: * danielebarchiesi@0: * @see field_widget_field() danielebarchiesi@0: * @see field_widget_instance() danielebarchiesi@0: * @see hook_field_widget_form_alter() danielebarchiesi@0: * @see hook_field_widget_WIDGET_TYPE_form_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { danielebarchiesi@0: $element += array( danielebarchiesi@0: '#type' => $instance['widget']['type'], danielebarchiesi@0: '#default_value' => isset($items[$delta]) ? $items[$delta] : '', danielebarchiesi@0: ); danielebarchiesi@0: return array('value' => $element); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter forms for field widgets provided by other modules. danielebarchiesi@0: * danielebarchiesi@0: * @param $element danielebarchiesi@0: * The field widget form element as constructed by hook_field_widget_form(). danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * An associative array containing the current state of the form. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing the following key-value pairs, matching the danielebarchiesi@0: * arguments received by hook_field_widget_form(): danielebarchiesi@0: * - form: The form structure to which widgets are being attached. This may be danielebarchiesi@0: * a full form structure, or a sub-element of a larger form. danielebarchiesi@0: * - field: The field structure. danielebarchiesi@0: * - instance: The field instance structure. danielebarchiesi@0: * - langcode: The language associated with $items. danielebarchiesi@0: * - items: Array of default values for this field. danielebarchiesi@0: * - delta: The order of this item in the array of subelements (0, 1, 2, etc). danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_widget_form() danielebarchiesi@0: * @see hook_field_widget_WIDGET_TYPE_form_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_form_alter(&$element, &$form_state, $context) { danielebarchiesi@0: // Add a css class to widget form elements for all fields of type mytype. danielebarchiesi@0: if ($context['field']['type'] == 'mytype') { danielebarchiesi@0: // Be sure not to overwrite existing attributes. danielebarchiesi@0: $element['#attributes']['class'][] = 'myclass'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter widget forms for a specific widget provided by another module. danielebarchiesi@0: * danielebarchiesi@0: * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a danielebarchiesi@0: * specific widget form, rather than using hook_field_widget_form_alter() and danielebarchiesi@0: * checking the widget type. danielebarchiesi@0: * danielebarchiesi@0: * @param $element danielebarchiesi@0: * The field widget form element as constructed by hook_field_widget_form(). danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * An associative array containing the current state of the form. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing the following key-value pairs, matching the danielebarchiesi@0: * arguments received by hook_field_widget_form(): danielebarchiesi@0: * - "form": The form structure where widgets are being attached to. This danielebarchiesi@0: * might be a full form structure, or a sub-element of a larger form. danielebarchiesi@0: * - "field": The field structure. danielebarchiesi@0: * - "instance": The field instance structure. danielebarchiesi@0: * - "langcode": The language associated with $items. danielebarchiesi@0: * - "items": Array of default values for this field. danielebarchiesi@0: * - "delta": The order of this item in the array of subelements (0, 1, 2, danielebarchiesi@0: * etc). danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_widget_form() danielebarchiesi@0: * @see hook_field_widget_form_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) { danielebarchiesi@0: // Code here will only act on widgets of type WIDGET_TYPE. For example, danielebarchiesi@0: // hook_field_widget_mymodule_autocomplete_form_alter() will only act on danielebarchiesi@0: // widgets of type 'mymodule_autocomplete'. danielebarchiesi@0: $element['#autocomplete_path'] = 'mymodule/autocomplete_path'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alters the widget properties of a field instance before it gets displayed. danielebarchiesi@0: * danielebarchiesi@0: * Note that instead of hook_field_widget_properties_alter(), which is called danielebarchiesi@0: * for all fields on all entity types, danielebarchiesi@0: * hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget danielebarchiesi@0: * properties for fields on a specific entity type only. danielebarchiesi@0: * danielebarchiesi@0: * This hook is called once per field per added or edit entity. If the result danielebarchiesi@0: * of the hook involves reading from the database, it is highly recommended to danielebarchiesi@0: * statically cache the information. danielebarchiesi@0: * danielebarchiesi@0: * @param $widget danielebarchiesi@0: * The instance's widget properties. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The entity type; e.g., 'node' or 'user'. danielebarchiesi@0: * - entity: The entity object. danielebarchiesi@0: * - field: The field that the widget belongs to. danielebarchiesi@0: * - instance: The instance of the field. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_widget_properties_ENTITY_TYPE_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_properties_alter(&$widget, $context) { danielebarchiesi@0: // Change a widget's type according to the time of day. danielebarchiesi@0: $field = $context['field']; danielebarchiesi@0: if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') { danielebarchiesi@0: $time = date('H'); danielebarchiesi@0: $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Flag a field-level validation error. danielebarchiesi@0: * danielebarchiesi@0: * @param $element danielebarchiesi@0: * An array containing the form element for the widget. The error needs to be danielebarchiesi@0: * flagged on the right sub-element, according to the widget's internal danielebarchiesi@0: * structure. danielebarchiesi@0: * @param $error danielebarchiesi@0: * An associative array with the following key-value pairs, as returned by danielebarchiesi@0: * hook_field_validate(): danielebarchiesi@0: * - error: the error code. Complex widgets might need to report different danielebarchiesi@0: * errors to different form elements inside the widget. danielebarchiesi@0: * - message: the human readable message to be displayed. danielebarchiesi@0: * @param $form danielebarchiesi@0: * The form structure where field elements are attached to. This might be a danielebarchiesi@0: * full form structure, or a sub-element of a larger form. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * An associative array containing the current state of the form. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_error($element, $error, $form, &$form_state) { danielebarchiesi@0: form_error($element, $error['message']); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "defgroup field_widget". danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @defgroup field_formatter Field Formatter API danielebarchiesi@0: * @{ danielebarchiesi@0: * Define Field API formatter types. danielebarchiesi@0: * danielebarchiesi@0: * Field API formatters specify how fields are displayed when the entity to danielebarchiesi@0: * which the field is attached is displayed. Fields of a given danielebarchiesi@0: * @link field_types field type @endlink may be displayed using more than one danielebarchiesi@0: * formatter. In this case, the Field UI module allows the site builder to danielebarchiesi@0: * choose which formatter to use. Field formatters are defined by implementing danielebarchiesi@0: * hook_field_formatter_info(). danielebarchiesi@0: * danielebarchiesi@0: * @see field danielebarchiesi@0: * @see field_types danielebarchiesi@0: * @see field_widget danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Expose Field API formatter types. danielebarchiesi@0: * danielebarchiesi@0: * Formatters handle the display of field values. Formatter hooks are typically danielebarchiesi@0: * called by the Field Attach API field_attach_prepare_view() and danielebarchiesi@0: * field_attach_view() functions. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array describing the formatter types implemented by the module. danielebarchiesi@0: * The keys are formatter type names. To avoid name clashes, formatter type danielebarchiesi@0: * names should be prefixed with the name of the module that exposes them. danielebarchiesi@0: * The values are arrays describing the formatter type, with the following danielebarchiesi@0: * key/value pairs: danielebarchiesi@0: * - label: The human-readable name of the formatter type. danielebarchiesi@0: * - description: A short description for the formatter type. danielebarchiesi@0: * - field types: An array of field types the formatter supports. danielebarchiesi@0: * - settings: An array whose keys are the names of the settings available danielebarchiesi@0: * for the formatter type, and whose values are the default values for danielebarchiesi@0: * those settings. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_formatter_info_alter() danielebarchiesi@0: * @see hook_field_formatter_view() danielebarchiesi@0: * @see hook_field_formatter_prepare_view() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_formatter_info() { danielebarchiesi@0: return array( danielebarchiesi@0: 'text_default' => array( danielebarchiesi@0: 'label' => t('Default'), danielebarchiesi@0: 'field types' => array('text', 'text_long', 'text_with_summary'), danielebarchiesi@0: ), danielebarchiesi@0: 'text_plain' => array( danielebarchiesi@0: 'label' => t('Plain text'), danielebarchiesi@0: 'field types' => array('text', 'text_long', 'text_with_summary'), danielebarchiesi@0: ), danielebarchiesi@0: danielebarchiesi@0: // The text_trimmed formatter displays the trimmed version of the danielebarchiesi@0: // full element of the field. It is intended to be used with text danielebarchiesi@0: // and text_long fields. It also works with text_with_summary danielebarchiesi@0: // fields though the text_summary_or_trimmed formatter makes more danielebarchiesi@0: // sense for that field type. danielebarchiesi@0: 'text_trimmed' => array( danielebarchiesi@0: 'label' => t('Trimmed'), danielebarchiesi@0: 'field types' => array('text', 'text_long', 'text_with_summary'), danielebarchiesi@0: ), danielebarchiesi@0: danielebarchiesi@0: // The 'summary or trimmed' field formatter for text_with_summary danielebarchiesi@0: // fields displays returns the summary element of the field or, if danielebarchiesi@0: // the summary is empty, the trimmed version of the full element danielebarchiesi@0: // of the field. danielebarchiesi@0: 'text_summary_or_trimmed' => array( danielebarchiesi@0: 'label' => t('Summary or trimmed'), danielebarchiesi@0: 'field types' => array('text_with_summary'), danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on Field API formatter types. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * An array of information on formatter types exposed by danielebarchiesi@0: * hook_field_formatter_info() implementations. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_formatter_info_alter(&$info) { danielebarchiesi@0: // Add a setting to a formatter type. danielebarchiesi@0: $info['text_default']['settings'] += array( danielebarchiesi@0: 'mymodule_additional_setting' => 'default value', danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: // Let a new field type re-use an existing formatter. danielebarchiesi@0: $info['text_default']['field types'][] = 'my_field_type'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Allow formatters to load information for field values being displayed. danielebarchiesi@0: * danielebarchiesi@0: * This should be used when a formatter needs to load additional information danielebarchiesi@0: * from the database in order to render a field, for example a reference field danielebarchiesi@0: * which displays properties of the referenced entities such as name or type. danielebarchiesi@0: * danielebarchiesi@0: * This hook is called after the field type's own hook_field_prepare_view(). danielebarchiesi@0: * danielebarchiesi@0: * Unlike most other field hooks, this hook operates on multiple entities. The danielebarchiesi@0: * $entities, $instances and $items parameters are arrays keyed by entity ID. danielebarchiesi@0: * For performance reasons, information for all available entities should be danielebarchiesi@0: * loaded in a single query where possible. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entities danielebarchiesi@0: * Array of entities being displayed, keyed by entity ID. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure for the operation. danielebarchiesi@0: * @param $instances danielebarchiesi@0: * Array of instance structures for $field for each entity, keyed by entity danielebarchiesi@0: * ID. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language the field values are to be shown in. If no language is danielebarchiesi@0: * provided the current language is used. danielebarchiesi@0: * @param $items danielebarchiesi@0: * Array of field values for the entities, keyed by entity ID. danielebarchiesi@0: * @param $displays danielebarchiesi@0: * Array of display settings to use for each entity, keyed by entity ID. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * Changes or additions to field values are done by altering the $items danielebarchiesi@0: * parameter by reference. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) { danielebarchiesi@0: $tids = array(); danielebarchiesi@0: danielebarchiesi@0: // Collect every possible term attached to any of the fieldable entities. danielebarchiesi@0: foreach ($entities as $id => $entity) { danielebarchiesi@0: foreach ($items[$id] as $delta => $item) { danielebarchiesi@0: // Force the array key to prevent duplicates. danielebarchiesi@0: $tids[$item['tid']] = $item['tid']; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if ($tids) { danielebarchiesi@0: $terms = taxonomy_term_load_multiple($tids); danielebarchiesi@0: danielebarchiesi@0: // Iterate through the fieldable entities again to attach the loaded term danielebarchiesi@0: // data. danielebarchiesi@0: foreach ($entities as $id => $entity) { danielebarchiesi@0: $rekey = FALSE; danielebarchiesi@0: danielebarchiesi@0: foreach ($items[$id] as $delta => $item) { danielebarchiesi@0: // Check whether the taxonomy term field instance value could be loaded. danielebarchiesi@0: if (isset($terms[$item['tid']])) { danielebarchiesi@0: // Replace the instance value with the term data. danielebarchiesi@0: $items[$id][$delta]['taxonomy_term'] = $terms[$item['tid']]; danielebarchiesi@0: } danielebarchiesi@0: // Otherwise, unset the instance value, since the term does not exist. danielebarchiesi@0: else { danielebarchiesi@0: unset($items[$id][$delta]); danielebarchiesi@0: $rekey = TRUE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if ($rekey) { danielebarchiesi@0: // Rekey the items array. danielebarchiesi@0: $items[$id] = array_values($items[$id]); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Build a renderable array for a field value. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity being displayed. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The field instance. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language associated with $items. danielebarchiesi@0: * @param $items danielebarchiesi@0: * Array of values for this field. danielebarchiesi@0: * @param $display danielebarchiesi@0: * The display settings to use, as found in the 'display' entry of instance danielebarchiesi@0: * definitions. The array notably contains the following keys and values; danielebarchiesi@0: * - type: The name of the formatter to use. danielebarchiesi@0: * - settings: The array of formatter settings. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * A renderable array for the $items, as an array of child elements keyed danielebarchiesi@0: * by numeric indexes starting from 0. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { danielebarchiesi@0: $element = array(); danielebarchiesi@0: $settings = $display['settings']; danielebarchiesi@0: danielebarchiesi@0: switch ($display['type']) { danielebarchiesi@0: case 'sample_field_formatter_simple': danielebarchiesi@0: // Common case: each value is displayed individually in a sub-element danielebarchiesi@0: // keyed by delta. The field.tpl.php template specifies the markup danielebarchiesi@0: // wrapping each value. danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: $element[$delta] = array('#markup' => $settings['some_setting'] . $item['value']); danielebarchiesi@0: } danielebarchiesi@0: break; danielebarchiesi@0: danielebarchiesi@0: case 'sample_field_formatter_themeable': danielebarchiesi@0: // More elaborate formatters can defer to a theme function for easier danielebarchiesi@0: // customization. danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: $element[$delta] = array( danielebarchiesi@0: '#theme' => 'mymodule_theme_sample_field_formatter_themeable', danielebarchiesi@0: '#data' => $item['value'], danielebarchiesi@0: '#some_setting' => $settings['some_setting'], danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: break; danielebarchiesi@0: danielebarchiesi@0: case 'sample_field_formatter_combined': danielebarchiesi@0: // Some formatters might need to display all values within a single piece danielebarchiesi@0: // of markup. danielebarchiesi@0: $rows = array(); danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: $rows[] = array($delta, $item['value']); danielebarchiesi@0: } danielebarchiesi@0: $element[0] = array( danielebarchiesi@0: '#theme' => 'table', danielebarchiesi@0: '#header' => array(t('Delta'), t('Value')), danielebarchiesi@0: '#rows' => $rows, danielebarchiesi@0: ); danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $element; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "defgroup field_formatter". danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @ingroup field_attach danielebarchiesi@0: * @{ danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_form(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * Implementing modules should alter the $form or $form_state parameters. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for which an edit form is being built. danielebarchiesi@0: * @param $form danielebarchiesi@0: * The form structure where field elements are attached to. This might be a danielebarchiesi@0: * full form structure, or a sub-element of a larger form. The danielebarchiesi@0: * $form['#parents'] property can be used to identify the corresponding part danielebarchiesi@0: * of $form_state['values']. Hook implementations that need to act on the danielebarchiesi@0: * top-level properties of the global form (like #submit, #validate...) can danielebarchiesi@0: * add a #process callback to the array received in the $form parameter, and danielebarchiesi@0: * act on the $complete_form parameter in the process callback. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * An associative array containing the current state of the form. danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language the field values are going to be entered in. If no language danielebarchiesi@0: * is provided the default site language will be used. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { danielebarchiesi@0: // Add a checkbox allowing a given field to be emptied. danielebarchiesi@0: // See hook_field_attach_submit() for the corresponding processing code. danielebarchiesi@0: $form['empty_field_foo'] = array( danielebarchiesi@0: '#type' => 'checkbox', danielebarchiesi@0: '#title' => t("Empty the 'field_foo' field"), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_load(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * Unlike other field_attach hooks, this hook accounts for 'multiple loads'. danielebarchiesi@0: * Instead of the usual $entity parameter, it accepts an array of entities, danielebarchiesi@0: * indexed by entity ID. For performance reasons, information for all available danielebarchiesi@0: * entities should be loaded in a single query where possible. danielebarchiesi@0: * danielebarchiesi@0: * The changes made to the entities' field values get cached by the field cache danielebarchiesi@0: * for subsequent loads. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_load() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_load($entity_type, $entities, $age, $options) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_validate(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_validate() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_validate($entity_type, $entity, &$errors) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_submit(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity for which an edit form is being submitted. The incoming form danielebarchiesi@0: * values have been extracted as field values of the $entity object. danielebarchiesi@0: * @param $form danielebarchiesi@0: * The form structure where field elements are attached to. This might be a danielebarchiesi@0: * full form structure, or a sub-part of a larger form. The $form['#parents'] danielebarchiesi@0: * property can be used to identify the corresponding part of danielebarchiesi@0: * $form_state['values']. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * An associative array containing the current state of the form. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_submit($entity_type, $entity, $form, &$form_state) { danielebarchiesi@0: // Sample case of an 'Empty the field' checkbox added on the form, allowing danielebarchiesi@0: // a given field to be emptied. danielebarchiesi@0: $values = drupal_array_get_nested_value($form_state['values'], $form['#parents']); danielebarchiesi@0: if (!empty($values['empty_field_foo'])) { danielebarchiesi@0: unset($entity->field_foo); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_presave(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_presave() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_presave($entity_type, $entity) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_insert(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_insert() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_insert($entity_type, $entity) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_update(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_update() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_update($entity_type, $entity) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter field_attach_preprocess() variables. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked while preprocessing the field.tpl.php template file danielebarchiesi@0: * in field_attach_preprocess(). danielebarchiesi@0: * danielebarchiesi@0: * @param $variables danielebarchiesi@0: * The variables array is passed by reference and will be populated with field danielebarchiesi@0: * values. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * - entity: The entity with fields to render. danielebarchiesi@0: * - element: The structured array containing the values ready for rendering. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_preprocess_alter(&$variables, $context) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_delete(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_delete() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_delete($entity_type, $entity) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_delete_revision(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_delete_revision() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_delete_revision($entity_type, $entity) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_purge_data(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked in field_purge_data() and allows modules to act on danielebarchiesi@0: * purging data from a single field pseudo-entity. For example, if a module danielebarchiesi@0: * relates data in the field with its own data, it may purge its own data danielebarchiesi@0: * during this process as well. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The pseudo-entity whose field data is being purged. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The (possibly deleted) field whose data is being purged. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The deleted field instance whose data is being purged. danielebarchiesi@0: * danielebarchiesi@0: * @see @link field_purge Field API bulk data deletion @endlink danielebarchiesi@0: * @see field_purge_data() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_purge($entity_type, $entity, $field, $instance) { danielebarchiesi@0: // find the corresponding data in mymodule and purge it danielebarchiesi@0: if ($entity_type == 'node' && $field->field_name == 'my_field_name') { danielebarchiesi@0: mymodule_remove_mydata($entity->nid); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on field_attach_view() or field_view_field(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * @param $output danielebarchiesi@0: * The structured content array tree for all of the entity's fields. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * - entity: The entity with fields to render. danielebarchiesi@0: * - view_mode: View mode; for example, 'full' or 'teaser'. danielebarchiesi@0: * - display: Either a view mode string or an array of display settings. If danielebarchiesi@0: * this hook is being invoked from field_attach_view(), the 'display' danielebarchiesi@0: * element is set to the view mode string. If this hook is being invoked danielebarchiesi@0: * from field_view_field(), this element is set to the $display argument danielebarchiesi@0: * and the view_mode element is set to '_custom'. See field_view_field() danielebarchiesi@0: * for more information on what its $display argument contains. danielebarchiesi@0: * - language: The language code used for rendering. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_view_alter(&$output, $context) { danielebarchiesi@0: // Append RDF term mappings on displayed taxonomy links. danielebarchiesi@0: foreach (element_children($output) as $field_name) { danielebarchiesi@0: $element = &$output[$field_name]; danielebarchiesi@0: if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') { danielebarchiesi@0: foreach ($element['#items'] as $delta => $item) { danielebarchiesi@0: $term = $item['taxonomy_term']; danielebarchiesi@0: if (!empty($term->rdf_mapping['rdftype'])) { danielebarchiesi@0: $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype']; danielebarchiesi@0: } danielebarchiesi@0: if (!empty($term->rdf_mapping['name']['predicates'])) { danielebarchiesi@0: $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates']; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on field_attach_prepare_translation(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity being prepared for translation. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The type of $entity; e.g. 'node' or 'user'. danielebarchiesi@0: * - langcode: The language the entity has to be translated in. danielebarchiesi@0: * - source_entity: The entity holding the field values to be translated. danielebarchiesi@0: * - source_langcode: The source language from which translate. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_prepare_translation_alter(&$entity, $context) { danielebarchiesi@0: if ($context['entity_type'] == 'custom_entity_type') { danielebarchiesi@0: $entity->custom_field = $context['source_entity']->custom_field; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on field_language() values. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked to alter the array of display languages for the given danielebarchiesi@0: * entity. danielebarchiesi@0: * danielebarchiesi@0: * @param $display_language danielebarchiesi@0: * A reference to an array of language codes keyed by field name. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The type of the entity to be displayed. danielebarchiesi@0: * - entity: The entity with fields to render. danielebarchiesi@0: * - langcode: The language code $entity has to be displayed in. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_language_alter(&$display_language, $context) { danielebarchiesi@0: // Do not apply core language fallback rules if they are disabled or if Locale danielebarchiesi@0: // is not registered as a translation handler. danielebarchiesi@0: if (variable_get('locale_field_language_fallback', TRUE) && field_has_translation_handler($context['entity_type'], 'locale')) { danielebarchiesi@0: locale_field_language_fallback($display_language, $context['entity'], $context['language']); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter field_available_languages() values. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_available_languages() to allow modules to danielebarchiesi@0: * alter the array of available languages for the given field. danielebarchiesi@0: * danielebarchiesi@0: * @param $languages danielebarchiesi@0: * A reference to an array of language codes to be made available. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The type of the entity the field is attached to. danielebarchiesi@0: * - field: A field data structure. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_available_languages_alter(&$languages, $context) { danielebarchiesi@0: // Add an unavailable language. danielebarchiesi@0: $languages[] = 'xx'; danielebarchiesi@0: danielebarchiesi@0: // Remove an available language. danielebarchiesi@0: $index = array_search('yy', $languages); danielebarchiesi@0: unset($languages[$index]); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_create_bundle(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_create_bundle() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_create_bundle($entity_type, $bundle) { danielebarchiesi@0: // When a new bundle is created, the menu needs to be rebuilt to add the danielebarchiesi@0: // Field UI menu item tabs. danielebarchiesi@0: variable_set('menu_rebuild_needed', TRUE); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_rename_bundle(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * See field_attach_rename_bundle() for details and arguments. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { danielebarchiesi@0: // Update the extra weights variable with new information. danielebarchiesi@0: if ($bundle_old !== $bundle_new) { danielebarchiesi@0: $extra_weights = variable_get('field_extra_weights', array()); danielebarchiesi@0: if (isset($info[$entity_type][$bundle_old])) { danielebarchiesi@0: $extra_weights[$entity_type][$bundle_new] = $extra_weights[$entity_type][$bundle_old]; danielebarchiesi@0: unset($extra_weights[$entity_type][$bundle_old]); danielebarchiesi@0: variable_set('field_extra_weights', $extra_weights); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field_attach_delete_bundle. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked after the field module has performed the operation. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $bundle danielebarchiesi@0: * The bundle that was just deleted. danielebarchiesi@0: * @param $instances danielebarchiesi@0: * An array of all instances that existed for the bundle before it was danielebarchiesi@0: * deleted. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_attach_delete_bundle($entity_type, $bundle, $instances) { danielebarchiesi@0: // Remove the extra weights variable information for this bundle. danielebarchiesi@0: $extra_weights = variable_get('field_extra_weights', array()); danielebarchiesi@0: if (isset($extra_weights[$entity_type][$bundle])) { danielebarchiesi@0: unset($extra_weights[$entity_type][$bundle]); danielebarchiesi@0: variable_set('field_extra_weights', $extra_weights); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "defgroup field_attach". danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @addtogroup field_storage danielebarchiesi@0: * @{ danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Expose Field API storage backends. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array describing the storage backends implemented by the module. danielebarchiesi@0: * The keys are storage backend names. To avoid name clashes, storage backend danielebarchiesi@0: * names should be prefixed with the name of the module that exposes them. danielebarchiesi@0: * The values are arrays describing the storage backend, with the following danielebarchiesi@0: * key/value pairs: danielebarchiesi@0: * - label: The human-readable name of the storage backend. danielebarchiesi@0: * - description: A short description for the storage backend. danielebarchiesi@0: * - settings: An array whose keys are the names of the settings available danielebarchiesi@0: * for the storage backend, and whose values are the default values for danielebarchiesi@0: * those settings. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_info() { danielebarchiesi@0: return array( danielebarchiesi@0: 'field_sql_storage' => array( danielebarchiesi@0: 'label' => t('Default SQL storage'), danielebarchiesi@0: 'description' => t('Stores fields in the local SQL database, using per-field tables.'), danielebarchiesi@0: 'settings' => array(), danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on Field API storage types. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * Array of informations on storage types exposed by danielebarchiesi@0: * hook_field_field_storage_info() implementations. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_info_alter(&$info) { danielebarchiesi@0: // Add a setting to a storage type. danielebarchiesi@0: $info['field_sql_storage']['settings'] += array( danielebarchiesi@0: 'mymodule_additional_setting' => 'default value', danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Reveal the internal details about the storage for a field. danielebarchiesi@0: * danielebarchiesi@0: * For example, an SQL storage module might return the Schema API structure for danielebarchiesi@0: * the table. A key/value storage module might return the server name, danielebarchiesi@0: * authentication credentials, and bin name. danielebarchiesi@0: * danielebarchiesi@0: * Field storage modules are not obligated to implement this hook. Modules danielebarchiesi@0: * that rely on these details must only use them for read operations. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * A field structure. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array of details. danielebarchiesi@0: * - The first dimension is a store type (sql, solr, etc). danielebarchiesi@0: * - The second dimension indicates the age of the values in the store danielebarchiesi@0: * FIELD_LOAD_CURRENT or FIELD_LOAD_REVISION. danielebarchiesi@0: * - Other dimensions are specific to the field storage module. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_storage_details_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_details($field) { danielebarchiesi@0: $details = array(); danielebarchiesi@0: danielebarchiesi@0: // Add field columns. danielebarchiesi@0: foreach ((array) $field['columns'] as $column_name => $attributes) { danielebarchiesi@0: $real_name = _field_sql_storage_columnname($field['field_name'], $column_name); danielebarchiesi@0: $columns[$column_name] = $real_name; danielebarchiesi@0: } danielebarchiesi@0: return array( danielebarchiesi@0: 'sql' => array( danielebarchiesi@0: FIELD_LOAD_CURRENT => array( danielebarchiesi@0: _field_sql_storage_tablename($field) => $columns, danielebarchiesi@0: ), danielebarchiesi@0: FIELD_LOAD_REVISION => array( danielebarchiesi@0: _field_sql_storage_revision_tablename($field) => $columns, danielebarchiesi@0: ), danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform alterations on Field API storage details. danielebarchiesi@0: * danielebarchiesi@0: * @param $details danielebarchiesi@0: * An array of storage details for fields as exposed by danielebarchiesi@0: * hook_field_storage_details() implementations. danielebarchiesi@0: * @param $field danielebarchiesi@0: * A field structure. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_storage_details() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_details_alter(&$details, $field) { danielebarchiesi@0: if ($field['field_name'] == 'field_of_interest') { danielebarchiesi@0: $columns = array(); danielebarchiesi@0: foreach ((array) $field['columns'] as $column_name => $attributes) { danielebarchiesi@0: $columns[$column_name] = $column_name; danielebarchiesi@0: } danielebarchiesi@0: $details['drupal_variables'] = array( danielebarchiesi@0: FIELD_LOAD_CURRENT => array( danielebarchiesi@0: 'moon' => $columns, danielebarchiesi@0: ), danielebarchiesi@0: FIELD_LOAD_REVISION => array( danielebarchiesi@0: 'mars' => $columns, danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Load field data for a set of entities. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_attach_load() to ask the field storage danielebarchiesi@0: * module to load field data. danielebarchiesi@0: * danielebarchiesi@0: * Modules implementing this hook should load field values and add them to danielebarchiesi@0: * objects in $entities. Fields with no values should be added as empty danielebarchiesi@0: * arrays. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of entity, such as 'node' or 'user'. danielebarchiesi@0: * @param $entities danielebarchiesi@0: * The array of entity objects to add fields to, keyed by entity ID. danielebarchiesi@0: * @param $age danielebarchiesi@0: * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or danielebarchiesi@0: * FIELD_LOAD_REVISION to load the version indicated by each entity. danielebarchiesi@0: * @param $fields danielebarchiesi@0: * An array listing the fields to be loaded. The keys of the array are field danielebarchiesi@0: * IDs, and the values of the array are the entity IDs (or revision IDs, danielebarchiesi@0: * depending on the $age parameter) to add each field to. danielebarchiesi@0: * @param $options danielebarchiesi@0: * An associative array of additional options, with the following keys: danielebarchiesi@0: * - deleted: If TRUE, deleted fields should be loaded as well as danielebarchiesi@0: * non-deleted fields. If unset or FALSE, only non-deleted fields should be danielebarchiesi@0: * loaded. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_load($entity_type, $entities, $age, $fields, $options) { danielebarchiesi@0: $load_current = $age == FIELD_LOAD_CURRENT; danielebarchiesi@0: danielebarchiesi@0: foreach ($fields as $field_id => $ids) { danielebarchiesi@0: // By the time this hook runs, the relevant field definitions have been danielebarchiesi@0: // populated and cached in FieldInfo, so calling field_info_field_by_id() danielebarchiesi@0: // on each field individually is more efficient than loading all fields in danielebarchiesi@0: // memory upfront with field_info_field_by_ids(). danielebarchiesi@0: $field = field_info_field_by_id($field_id); danielebarchiesi@0: $field_name = $field['field_name']; danielebarchiesi@0: $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field); danielebarchiesi@0: danielebarchiesi@0: $query = db_select($table, 't') danielebarchiesi@0: ->fields('t') danielebarchiesi@0: ->condition('entity_type', $entity_type) danielebarchiesi@0: ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN') danielebarchiesi@0: ->condition('language', field_available_languages($entity_type, $field), 'IN') danielebarchiesi@0: ->orderBy('delta'); danielebarchiesi@0: danielebarchiesi@0: if (empty($options['deleted'])) { danielebarchiesi@0: $query->condition('deleted', 0); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $results = $query->execute(); danielebarchiesi@0: danielebarchiesi@0: $delta_count = array(); danielebarchiesi@0: foreach ($results as $row) { danielebarchiesi@0: if (!isset($delta_count[$row->entity_id][$row->language])) { danielebarchiesi@0: $delta_count[$row->entity_id][$row->language] = 0; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) { danielebarchiesi@0: $item = array(); danielebarchiesi@0: // For each column declared by the field, populate the item danielebarchiesi@0: // from the prefixed database column. danielebarchiesi@0: foreach ($field['columns'] as $column => $attributes) { danielebarchiesi@0: $column_name = _field_sql_storage_columnname($field_name, $column); danielebarchiesi@0: $item[$column] = $row->$column_name; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Add the item to the field values for the entity. danielebarchiesi@0: $entities[$row->entity_id]->{$field_name}[$row->language][] = $item; danielebarchiesi@0: $delta_count[$row->entity_id][$row->language]++; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Write field data for an entity. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_attach_insert() and field_attach_update(), danielebarchiesi@0: * to ask the field storage module to save field data. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The entity type of entity, such as 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity on which to operate. danielebarchiesi@0: * @param $op danielebarchiesi@0: * FIELD_STORAGE_UPDATE when updating an existing entity, danielebarchiesi@0: * FIELD_STORAGE_INSERT when inserting a new entity. danielebarchiesi@0: * @param $fields danielebarchiesi@0: * An array listing the fields to be written. The keys and values of the danielebarchiesi@0: * array are field IDs. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_write($entity_type, $entity, $op, $fields) { danielebarchiesi@0: list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); danielebarchiesi@0: if (!isset($vid)) { danielebarchiesi@0: $vid = $id; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: foreach ($fields as $field_id) { danielebarchiesi@0: $field = field_info_field_by_id($field_id); danielebarchiesi@0: $field_name = $field['field_name']; danielebarchiesi@0: $table_name = _field_sql_storage_tablename($field); danielebarchiesi@0: $revision_name = _field_sql_storage_revision_tablename($field); danielebarchiesi@0: danielebarchiesi@0: $all_languages = field_available_languages($entity_type, $field); danielebarchiesi@0: $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name)); danielebarchiesi@0: danielebarchiesi@0: // Delete and insert, rather than update, in case a value was added. danielebarchiesi@0: if ($op == FIELD_STORAGE_UPDATE) { danielebarchiesi@0: // Delete languages present in the incoming $entity->$field_name. danielebarchiesi@0: // Delete all languages if $entity->$field_name is empty. danielebarchiesi@0: $languages = !empty($entity->$field_name) ? $field_languages : $all_languages; danielebarchiesi@0: if ($languages) { danielebarchiesi@0: db_delete($table_name) danielebarchiesi@0: ->condition('entity_type', $entity_type) danielebarchiesi@0: ->condition('entity_id', $id) danielebarchiesi@0: ->condition('language', $languages, 'IN') danielebarchiesi@0: ->execute(); danielebarchiesi@0: db_delete($revision_name) danielebarchiesi@0: ->condition('entity_type', $entity_type) danielebarchiesi@0: ->condition('entity_id', $id) danielebarchiesi@0: ->condition('revision_id', $vid) danielebarchiesi@0: ->condition('language', $languages, 'IN') danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Prepare the multi-insert query. danielebarchiesi@0: $do_insert = FALSE; danielebarchiesi@0: $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language'); danielebarchiesi@0: foreach ($field['columns'] as $column => $attributes) { danielebarchiesi@0: $columns[] = _field_sql_storage_columnname($field_name, $column); danielebarchiesi@0: } danielebarchiesi@0: $query = db_insert($table_name)->fields($columns); danielebarchiesi@0: $revision_query = db_insert($revision_name)->fields($columns); danielebarchiesi@0: danielebarchiesi@0: foreach ($field_languages as $langcode) { danielebarchiesi@0: $items = (array) $entity->{$field_name}[$langcode]; danielebarchiesi@0: $delta_count = 0; danielebarchiesi@0: foreach ($items as $delta => $item) { danielebarchiesi@0: // We now know we have someting to insert. danielebarchiesi@0: $do_insert = TRUE; danielebarchiesi@0: $record = array( danielebarchiesi@0: 'entity_type' => $entity_type, danielebarchiesi@0: 'entity_id' => $id, danielebarchiesi@0: 'revision_id' => $vid, danielebarchiesi@0: 'bundle' => $bundle, danielebarchiesi@0: 'delta' => $delta, danielebarchiesi@0: 'language' => $langcode, danielebarchiesi@0: ); danielebarchiesi@0: foreach ($field['columns'] as $column => $attributes) { danielebarchiesi@0: $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL; danielebarchiesi@0: } danielebarchiesi@0: $query->values($record); danielebarchiesi@0: if (isset($vid)) { danielebarchiesi@0: $revision_query->values($record); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) { danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Execute the query if we have values to insert. danielebarchiesi@0: if ($do_insert) { danielebarchiesi@0: $query->execute(); danielebarchiesi@0: $revision_query->execute(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Delete all field data for an entity. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_attach_delete() to ask the field storage danielebarchiesi@0: * module to delete field data. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The entity type of entity, such as 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity on which to operate. danielebarchiesi@0: * @param $fields danielebarchiesi@0: * An array listing the fields to delete. The keys and values of the danielebarchiesi@0: * array are field IDs. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_delete($entity_type, $entity, $fields) { danielebarchiesi@0: list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); danielebarchiesi@0: danielebarchiesi@0: foreach (field_info_instances($entity_type, $bundle) as $instance) { danielebarchiesi@0: if (isset($fields[$instance['field_id']])) { danielebarchiesi@0: $field = field_info_field_by_id($instance['field_id']); danielebarchiesi@0: field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Delete a single revision of field data for an entity. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_attach_delete_revision() to ask the field danielebarchiesi@0: * storage module to delete field revision data. danielebarchiesi@0: * danielebarchiesi@0: * Deleting the current (most recently written) revision is not danielebarchiesi@0: * allowed as has undefined results. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The entity type of entity, such as 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity on which to operate. The revision to delete is danielebarchiesi@0: * indicated by the entity's revision ID property, as identified by danielebarchiesi@0: * hook_fieldable_info() for $entity_type. danielebarchiesi@0: * @param $fields danielebarchiesi@0: * An array listing the fields to delete. The keys and values of the danielebarchiesi@0: * array are field IDs. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_delete_revision($entity_type, $entity, $fields) { danielebarchiesi@0: list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); danielebarchiesi@0: danielebarchiesi@0: if (isset($vid)) { danielebarchiesi@0: foreach ($fields as $field_id) { danielebarchiesi@0: $field = field_info_field_by_id($field_id); danielebarchiesi@0: $revision_name = _field_sql_storage_revision_tablename($field); danielebarchiesi@0: db_delete($revision_name) danielebarchiesi@0: ->condition('entity_type', $entity_type) danielebarchiesi@0: ->condition('entity_id', $id) danielebarchiesi@0: ->condition('revision_id', $vid) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Execute an EntityFieldQuery. danielebarchiesi@0: * danielebarchiesi@0: * This hook is called to find the entities having certain entity and field danielebarchiesi@0: * conditions and sort them in the given field order. If the field storage danielebarchiesi@0: * engine also handles property sorts and orders, it should unset those danielebarchiesi@0: * properties in the called object to signal that those have been handled. danielebarchiesi@0: * danielebarchiesi@0: * @param EntityFieldQuery $query danielebarchiesi@0: * An EntityFieldQuery. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * See EntityFieldQuery::execute() for the return values. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_query($query) { danielebarchiesi@0: $groups = array(); danielebarchiesi@0: if ($query->age == FIELD_LOAD_CURRENT) { danielebarchiesi@0: $tablename_function = '_field_sql_storage_tablename'; danielebarchiesi@0: $id_key = 'entity_id'; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $tablename_function = '_field_sql_storage_revision_tablename'; danielebarchiesi@0: $id_key = 'revision_id'; danielebarchiesi@0: } danielebarchiesi@0: $table_aliases = array(); danielebarchiesi@0: // Add tables for the fields used. danielebarchiesi@0: foreach ($query->fields as $key => $field) { danielebarchiesi@0: $tablename = $tablename_function($field); danielebarchiesi@0: // Every field needs a new table. danielebarchiesi@0: $table_alias = $tablename . $key; danielebarchiesi@0: $table_aliases[$key] = $table_alias; danielebarchiesi@0: if ($key) { danielebarchiesi@0: $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"); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $select_query = db_select($tablename, $table_alias); danielebarchiesi@0: $select_query->addTag('entity_field_access'); danielebarchiesi@0: $select_query->addMetaData('base_table', $tablename); danielebarchiesi@0: $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle')); danielebarchiesi@0: $field_base_table = $table_alias; danielebarchiesi@0: } danielebarchiesi@0: if ($field['cardinality'] != 1) { danielebarchiesi@0: $select_query->distinct(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Add field conditions. danielebarchiesi@0: foreach ($query->fieldConditions as $key => $condition) { danielebarchiesi@0: $table_alias = $table_aliases[$key]; danielebarchiesi@0: $field = $condition['field']; danielebarchiesi@0: // Add the specified condition. danielebarchiesi@0: $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']); danielebarchiesi@0: $query->addCondition($select_query, $sql_field, $condition); danielebarchiesi@0: // Add delta / language group conditions. danielebarchiesi@0: foreach (array('delta', 'language') as $column) { danielebarchiesi@0: if (isset($condition[$column . '_group'])) { danielebarchiesi@0: $group_name = $condition[$column . '_group']; danielebarchiesi@0: if (!isset($groups[$column][$group_name])) { danielebarchiesi@0: $groups[$column][$group_name] = $table_alias; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column"); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (isset($query->deleted)) { danielebarchiesi@0: $select_query->condition("$field_base_table.deleted", (int) $query->deleted); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Is there a need to sort the query by property? danielebarchiesi@0: $has_property_order = FALSE; danielebarchiesi@0: foreach ($query->order as $order) { danielebarchiesi@0: if ($order['type'] == 'property') { danielebarchiesi@0: $has_property_order = TRUE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if ($query->propertyConditions || $has_property_order) { danielebarchiesi@0: if (empty($query->entityConditions['entity_type']['value'])) { danielebarchiesi@0: throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.'); danielebarchiesi@0: } danielebarchiesi@0: $entity_type = $query->entityConditions['entity_type']['value']; danielebarchiesi@0: $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table); danielebarchiesi@0: $query->entityConditions['entity_type']['operator'] = '='; danielebarchiesi@0: foreach ($query->propertyConditions as $property_condition) { danielebarchiesi@0: $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: foreach ($query->entityConditions as $key => $condition) { danielebarchiesi@0: $query->addCondition($select_query, "$field_base_table.$key", $condition); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Order the query. danielebarchiesi@0: foreach ($query->order as $order) { danielebarchiesi@0: if ($order['type'] == 'entity') { danielebarchiesi@0: $key = $order['specifier']; danielebarchiesi@0: $select_query->orderBy("$field_base_table.$key", $order['direction']); danielebarchiesi@0: } danielebarchiesi@0: elseif ($order['type'] == 'field') { danielebarchiesi@0: $specifier = $order['specifier']; danielebarchiesi@0: $field = $specifier['field']; danielebarchiesi@0: $table_alias = $table_aliases[$specifier['index']]; danielebarchiesi@0: $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']); danielebarchiesi@0: $select_query->orderBy($sql_field, $order['direction']); danielebarchiesi@0: } danielebarchiesi@0: elseif ($order['type'] == 'property') { danielebarchiesi@0: $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $query->finishQuery($select_query, $id_key); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on creation of a new field. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_create_field() to ask the field storage danielebarchiesi@0: * module to save field information and prepare for storing field instances. danielebarchiesi@0: * If there is a problem, the field storage module should throw an exception. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field structure being created. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_create_field($field) { danielebarchiesi@0: $schema = _field_sql_storage_schema($field); danielebarchiesi@0: foreach ($schema as $name => $table) { danielebarchiesi@0: db_create_table($name, $table); danielebarchiesi@0: } danielebarchiesi@0: drupal_get_schema(NULL, TRUE); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on deletion of a field. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_delete_field() to ask the field storage danielebarchiesi@0: * module to mark all information stored in the field for deletion. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field being deleted. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_delete_field($field) { danielebarchiesi@0: // Mark all data associated with the field for deletion. danielebarchiesi@0: $field['deleted'] = 0; danielebarchiesi@0: $table = _field_sql_storage_tablename($field); danielebarchiesi@0: $revision_table = _field_sql_storage_revision_tablename($field); danielebarchiesi@0: db_update($table) danielebarchiesi@0: ->fields(array('deleted' => 1)) danielebarchiesi@0: ->execute(); danielebarchiesi@0: danielebarchiesi@0: // Move the table to a unique name while the table contents are being deleted. danielebarchiesi@0: $field['deleted'] = 1; danielebarchiesi@0: $new_table = _field_sql_storage_tablename($field); danielebarchiesi@0: $revision_new_table = _field_sql_storage_revision_tablename($field); danielebarchiesi@0: db_rename_table($table, $new_table); danielebarchiesi@0: db_rename_table($revision_table, $revision_new_table); danielebarchiesi@0: drupal_get_schema(NULL, TRUE); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on deletion of a field instance. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_delete_instance() to ask the field storage danielebarchiesi@0: * module to mark all information stored for the field instance for deletion. danielebarchiesi@0: * danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance being deleted. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_delete_instance($instance) { danielebarchiesi@0: $field = field_info_field($instance['field_name']); danielebarchiesi@0: $table_name = _field_sql_storage_tablename($field); danielebarchiesi@0: $revision_name = _field_sql_storage_revision_tablename($field); danielebarchiesi@0: db_update($table_name) danielebarchiesi@0: ->fields(array('deleted' => 1)) danielebarchiesi@0: ->condition('entity_type', $instance['entity_type']) danielebarchiesi@0: ->condition('bundle', $instance['bundle']) danielebarchiesi@0: ->execute(); danielebarchiesi@0: db_update($revision_name) danielebarchiesi@0: ->fields(array('deleted' => 1)) danielebarchiesi@0: ->condition('entity_type', $instance['entity_type']) danielebarchiesi@0: ->condition('bundle', $instance['bundle']) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act before the storage backends load field data. danielebarchiesi@0: * danielebarchiesi@0: * This hook allows modules to load data before the Field Storage API, danielebarchiesi@0: * optionally preventing the field storage module from doing so. danielebarchiesi@0: * danielebarchiesi@0: * This lets 3rd party modules override, mirror, shard, or otherwise store a danielebarchiesi@0: * subset of fields in a different way than the current storage engine. danielebarchiesi@0: * Possible use cases include per-bundle storage, per-combo-field storage, etc. danielebarchiesi@0: * danielebarchiesi@0: * Modules implementing this hook should load field values and add them to danielebarchiesi@0: * objects in $entities. Fields with no values should be added as empty danielebarchiesi@0: * arrays. In addition, fields loaded should be added as keys to $skip_fields. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of entity, such as 'node' or 'user'. danielebarchiesi@0: * @param $entities danielebarchiesi@0: * The array of entity objects to add fields to, keyed by entity ID. danielebarchiesi@0: * @param $age danielebarchiesi@0: * FIELD_LOAD_CURRENT to load the most recent revision for all fields, or danielebarchiesi@0: * FIELD_LOAD_REVISION to load the version indicated by each entity. danielebarchiesi@0: * @param $skip_fields danielebarchiesi@0: * An array keyed by field IDs whose data has already been loaded and danielebarchiesi@0: * therefore should not be loaded again. Add a key to this array to indicate danielebarchiesi@0: * that your module has already loaded a field. danielebarchiesi@0: * @param $options danielebarchiesi@0: * An associative array of additional options, with the following keys: danielebarchiesi@0: * - field_id: The field ID that should be loaded. If unset, all fields danielebarchiesi@0: * should be loaded. danielebarchiesi@0: * - deleted: If TRUE, deleted fields should be loaded as well as danielebarchiesi@0: * non-deleted fields. If unset or FALSE, only non-deleted fields should be danielebarchiesi@0: * loaded. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_pre_load($entity_type, $entities, $age, &$skip_fields, $options) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act before the storage backends insert field data. danielebarchiesi@0: * danielebarchiesi@0: * This hook allows modules to store data before the Field Storage API, danielebarchiesi@0: * optionally preventing the field storage module from doing so. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity with fields to save. danielebarchiesi@0: * @param $skip_fields danielebarchiesi@0: * An array keyed by field IDs whose data has already been written and danielebarchiesi@0: * therefore should not be written again. The values associated with these danielebarchiesi@0: * keys are not specified. danielebarchiesi@0: * @return danielebarchiesi@0: * Saved field IDs are set set as keys in $skip_fields. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_pre_insert($entity_type, $entity, &$skip_fields) { danielebarchiesi@0: if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) { danielebarchiesi@0: $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp')); danielebarchiesi@0: foreach ($entity->taxonomy_forums as $language) { danielebarchiesi@0: foreach ($language as $delta) { danielebarchiesi@0: $query->values(array( danielebarchiesi@0: 'nid' => $entity->nid, danielebarchiesi@0: 'title' => $entity->title, danielebarchiesi@0: 'tid' => $delta['value'], danielebarchiesi@0: 'sticky' => $entity->sticky, danielebarchiesi@0: 'created' => $entity->created, danielebarchiesi@0: 'comment_count' => 0, danielebarchiesi@0: 'last_comment_timestamp' => $entity->created, danielebarchiesi@0: )); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: $query->execute(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act before the storage backends update field data. danielebarchiesi@0: * danielebarchiesi@0: * This hook allows modules to store data before the Field Storage API, danielebarchiesi@0: * optionally preventing the field storage module from doing so. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The entity with fields to save. danielebarchiesi@0: * @param $skip_fields danielebarchiesi@0: * An array keyed by field IDs whose data has already been written and danielebarchiesi@0: * therefore should not be written again. The values associated with these danielebarchiesi@0: * keys are not specified. danielebarchiesi@0: * @return danielebarchiesi@0: * Saved field IDs are set set as keys in $skip_fields. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_pre_update($entity_type, $entity, &$skip_fields) { danielebarchiesi@0: $first_call = &drupal_static(__FUNCTION__, array()); danielebarchiesi@0: danielebarchiesi@0: if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) { danielebarchiesi@0: // We don't maintain data for old revisions, so clear all previous values danielebarchiesi@0: // from the table. Since this hook runs once per field, per entity, make danielebarchiesi@0: // sure we only wipe values once. danielebarchiesi@0: if (!isset($first_call[$entity->nid])) { danielebarchiesi@0: $first_call[$entity->nid] = FALSE; danielebarchiesi@0: db_delete('forum_index')->condition('nid', $entity->nid)->execute(); danielebarchiesi@0: } danielebarchiesi@0: // Only save data to the table if the node is published. danielebarchiesi@0: if ($entity->status) { danielebarchiesi@0: $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp')); danielebarchiesi@0: foreach ($entity->taxonomy_forums as $language) { danielebarchiesi@0: foreach ($language as $delta) { danielebarchiesi@0: $query->values(array( danielebarchiesi@0: 'nid' => $entity->nid, danielebarchiesi@0: 'title' => $entity->title, danielebarchiesi@0: 'tid' => $delta['value'], danielebarchiesi@0: 'sticky' => $entity->sticky, danielebarchiesi@0: 'created' => $entity->created, danielebarchiesi@0: 'comment_count' => 0, danielebarchiesi@0: 'last_comment_timestamp' => $entity->created, danielebarchiesi@0: )); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: $query->execute(); danielebarchiesi@0: // The logic for determining last_comment_count is fairly complex, so danielebarchiesi@0: // call _forum_update_forum_index() too. danielebarchiesi@0: _forum_update_forum_index($entity->nid); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Returns the maximum weight for the entity components handled by the module. danielebarchiesi@0: * danielebarchiesi@0: * Field API takes care of fields and 'extra_fields'. This hook is intended for danielebarchiesi@0: * third-party modules adding other entity components (e.g. field_group). danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of entity; e.g. 'node' or 'user'. danielebarchiesi@0: * @param $bundle danielebarchiesi@0: * The bundle name. danielebarchiesi@0: * @param $context danielebarchiesi@0: * The context for which the maximum weight is requested. Either 'form', or danielebarchiesi@0: * the name of a view mode. danielebarchiesi@0: * @return danielebarchiesi@0: * The maximum weight of the entity's components, or NULL if no components danielebarchiesi@0: * were found. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_info_max_weight($entity_type, $bundle, $context) { danielebarchiesi@0: $weights = array(); danielebarchiesi@0: danielebarchiesi@0: foreach (my_module_entity_additions($entity_type, $bundle, $context) as $addition) { danielebarchiesi@0: $weights[] = $addition['weight']; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $weights ? max($weights) : NULL; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alters the display settings of a field before it gets displayed. danielebarchiesi@0: * danielebarchiesi@0: * Note that instead of hook_field_display_alter(), which is called for all danielebarchiesi@0: * fields on all entity types, hook_field_display_ENTITY_TYPE_alter() may be danielebarchiesi@0: * used to alter display settings for fields on a specific entity type only. danielebarchiesi@0: * danielebarchiesi@0: * This hook is called once per field per displayed entity. If the result of the danielebarchiesi@0: * hook involves reading from the database, it is highly recommended to danielebarchiesi@0: * statically cache the information. danielebarchiesi@0: * danielebarchiesi@0: * @param $display danielebarchiesi@0: * The display settings that will be used to display the field values, as danielebarchiesi@0: * found in the 'display' key of $instance definitions. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The entity type; e.g., 'node' or 'user'. danielebarchiesi@0: * - field: The field being rendered. danielebarchiesi@0: * - instance: The instance being rendered. danielebarchiesi@0: * - entity: The entity being rendered. danielebarchiesi@0: * - view_mode: The view mode, e.g. 'full', 'teaser'... danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_display_ENTITY_TYPE_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_display_alter(&$display, $context) { danielebarchiesi@0: // Leave field labels out of the search index. danielebarchiesi@0: // Note: The check against $context['entity_type'] == 'node' could be avoided danielebarchiesi@0: // by using hook_field_display_node_alter() instead of danielebarchiesi@0: // hook_field_display_alter(), resulting in less function calls when danielebarchiesi@0: // rendering non-node entities. danielebarchiesi@0: if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') { danielebarchiesi@0: $display['label'] = 'hidden'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alters the display settings of a field on a given entity type before it gets displayed. danielebarchiesi@0: * danielebarchiesi@0: * Modules can implement hook_field_display_ENTITY_TYPE_alter() to alter display danielebarchiesi@0: * settings for fields on a specific entity type, rather than implementing danielebarchiesi@0: * hook_field_display_alter(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is called once per field per displayed entity. If the result of the danielebarchiesi@0: * hook involves reading from the database, it is highly recommended to danielebarchiesi@0: * statically cache the information. danielebarchiesi@0: * danielebarchiesi@0: * @param $display danielebarchiesi@0: * The display settings that will be used to display the field values, as danielebarchiesi@0: * found in the 'display' key of $instance definitions. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The entity type; e.g., 'node' or 'user'. danielebarchiesi@0: * - field: The field being rendered. danielebarchiesi@0: * - instance: The instance being rendered. danielebarchiesi@0: * - entity: The entity being rendered. danielebarchiesi@0: * - view_mode: The view mode, e.g. 'full', 'teaser'... danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_display_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_display_ENTITY_TYPE_alter(&$display, $context) { danielebarchiesi@0: // Leave field labels out of the search index. danielebarchiesi@0: if ($context['view_mode'] == 'search_index') { danielebarchiesi@0: $display['label'] = 'hidden'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alters the display settings of pseudo-fields before an entity is displayed. danielebarchiesi@0: * danielebarchiesi@0: * This hook is called once per displayed entity. If the result of the hook danielebarchiesi@0: * involves reading from the database, it is highly recommended to statically danielebarchiesi@0: * cache the information. danielebarchiesi@0: * danielebarchiesi@0: * @param $displays danielebarchiesi@0: * An array of display settings for the pseudo-fields in the entity, keyed danielebarchiesi@0: * by pseudo-field names. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The entity type; e.g., 'node' or 'user'. danielebarchiesi@0: * - bundle: The bundle name. danielebarchiesi@0: * - view_mode: The view mode, e.g. 'full', 'teaser'... danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_extra_fields_display_alter(&$displays, $context) { danielebarchiesi@0: if ($context['entity_type'] == 'taxonomy_term' && $context['view_mode'] == 'full') { danielebarchiesi@0: $displays['description']['visible'] = FALSE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alters the widget properties of a field instance on a given entity type danielebarchiesi@0: * before it gets displayed. danielebarchiesi@0: * danielebarchiesi@0: * Modules can implement hook_field_widget_properties_ENTITY_TYPE_alter() to danielebarchiesi@0: * alter the widget properties for fields on a specific entity type, rather than danielebarchiesi@0: * implementing hook_field_widget_properties_alter(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is called once per field per displayed widget entity. If the result danielebarchiesi@0: * of the hook involves reading from the database, it is highly recommended to danielebarchiesi@0: * statically cache the information. danielebarchiesi@0: * danielebarchiesi@0: * @param $widget danielebarchiesi@0: * The instance's widget properties. danielebarchiesi@0: * @param $context danielebarchiesi@0: * An associative array containing: danielebarchiesi@0: * - entity_type: The entity type; e.g., 'node' or 'user'. danielebarchiesi@0: * - entity: The entity object. danielebarchiesi@0: * - field: The field that the widget belongs to. danielebarchiesi@0: * - instance: The instance of the field. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_field_widget_properties_alter() danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_widget_properties_ENTITY_TYPE_alter(&$widget, $context) { danielebarchiesi@0: // Change a widget's type according to the time of day. danielebarchiesi@0: $field = $context['field']; danielebarchiesi@0: if ($field['field_name'] == 'field_foo') { danielebarchiesi@0: $time = date('H'); danielebarchiesi@0: $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "addtogroup field_storage". danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @addtogroup field_crud danielebarchiesi@0: * @{ danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a field being created. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_create_field() after the field is created, to danielebarchiesi@0: * allow modules to act on field creation. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field just created. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_create_field($field) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a field instance being created. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_create_instance() after the instance record danielebarchiesi@0: * is saved, so it cannot be used to modify the instance itself. danielebarchiesi@0: * danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance just created. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_create_instance($instance) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Forbid a field update from occurring. danielebarchiesi@0: * danielebarchiesi@0: * Any module may forbid any update for any reason. For example, the danielebarchiesi@0: * field's storage module might forbid an update if it would change danielebarchiesi@0: * the storage schema while data for the field exists. A field type danielebarchiesi@0: * module might forbid an update if it would change existing data's danielebarchiesi@0: * semantics, or if there are external dependencies on field settings danielebarchiesi@0: * that cannot be updated. danielebarchiesi@0: * danielebarchiesi@0: * To forbid the update from occurring, throw a FieldUpdateForbiddenException. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field as it will be post-update. danielebarchiesi@0: * @param $prior_field danielebarchiesi@0: * The field as it is pre-update. danielebarchiesi@0: * @param $has_data danielebarchiesi@0: * Whether any data already exists for this field. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_update_forbid($field, $prior_field, $has_data) { danielebarchiesi@0: // A 'list' field stores integer keys mapped to display values. If danielebarchiesi@0: // the new field will have fewer values, and any data exists for the danielebarchiesi@0: // abandoned keys, the field will have no way to display them. So, danielebarchiesi@0: // forbid such an update. danielebarchiesi@0: if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) { danielebarchiesi@0: // Identify the keys that will be lost. danielebarchiesi@0: $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values'])); danielebarchiesi@0: // If any data exist for those keys, forbid the update. danielebarchiesi@0: $query = new EntityFieldQuery(); danielebarchiesi@0: $found = $query danielebarchiesi@0: ->fieldCondition($prior_field['field_name'], 'value', $lost_keys) danielebarchiesi@0: ->range(0, 1) danielebarchiesi@0: ->execute(); danielebarchiesi@0: if ($found) { danielebarchiesi@0: throw new FieldUpdateForbiddenException("Cannot update a list field not to include keys with existing data"); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a field being updated. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked just after field is updated in field_update_field(). danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field as it is post-update. danielebarchiesi@0: * @param $prior_field danielebarchiesi@0: * The field as it was pre-update. danielebarchiesi@0: * @param $has_data danielebarchiesi@0: * Whether any data already exists for this field. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_update_field($field, $prior_field, $has_data) { danielebarchiesi@0: // Reset the static value that keeps track of allowed values for list fields. danielebarchiesi@0: drupal_static_reset('list_allowed_values'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a field being deleted. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked just after a field is deleted by field_delete_field(). danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field just deleted. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_delete_field($field) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a field instance being updated. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_update_instance() after the instance record danielebarchiesi@0: * is saved, so it cannot be used by a module to modify the instance itself. danielebarchiesi@0: * danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance as it is post-update. danielebarchiesi@0: * @param $prior_instance danielebarchiesi@0: * The instance as it was pre-update. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_update_instance($instance, $prior_instance) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a field instance being deleted. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_delete_instance() after the instance is danielebarchiesi@0: * deleted. danielebarchiesi@0: * danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance just deleted. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_delete_instance($instance) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on field records being read from the database. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_read_fields() on each field being read. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field record just read from the database. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_read_field($field) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a field record being read from the database. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_read_instances() on each instance being read. danielebarchiesi@0: * danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance record just read from the database. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_read_instance($instance) { danielebarchiesi@0: // @todo Needs function body. danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Acts when a field record is being purged. danielebarchiesi@0: * danielebarchiesi@0: * In field_purge_field(), after the field configuration has been danielebarchiesi@0: * removed from the database, the field storage module has had a chance to danielebarchiesi@0: * run its hook_field_storage_purge_field(), and the field info cache danielebarchiesi@0: * has been cleared, this hook is invoked on all modules to allow them to danielebarchiesi@0: * respond to the field being purged. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field being purged. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_purge_field($field) { danielebarchiesi@0: db_delete('my_module_field_info') danielebarchiesi@0: ->condition('id', $field['id']) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Acts when a field instance is being purged. danielebarchiesi@0: * danielebarchiesi@0: * In field_purge_instance(), after the field instance has been danielebarchiesi@0: * removed from the database, the field storage module has had a chance to danielebarchiesi@0: * run its hook_field_storage_purge_instance(), and the field info cache danielebarchiesi@0: * has been cleared, this hook is invoked on all modules to allow them to danielebarchiesi@0: * respond to the field instance being purged. danielebarchiesi@0: * danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance being purged. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_purge_instance($instance) { danielebarchiesi@0: db_delete('my_module_field_instance_info') danielebarchiesi@0: ->condition('id', $instance['id']) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Remove field storage information when a field record is purged. danielebarchiesi@0: * danielebarchiesi@0: * Called from field_purge_field() to allow the field storage module danielebarchiesi@0: * to remove field information when a field is being purged. danielebarchiesi@0: * danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field being purged. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_purge_field($field) { danielebarchiesi@0: $table_name = _field_sql_storage_tablename($field); danielebarchiesi@0: $revision_name = _field_sql_storage_revision_tablename($field); danielebarchiesi@0: db_drop_table($table_name); danielebarchiesi@0: db_drop_table($revision_name); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Remove field storage information when a field instance is purged. danielebarchiesi@0: * danielebarchiesi@0: * Called from field_purge_instance() to allow the field storage module danielebarchiesi@0: * to remove field instance information when a field instance is being danielebarchiesi@0: * purged. danielebarchiesi@0: * danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The instance being purged. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_purge_field_instance($instance) { danielebarchiesi@0: db_delete('my_module_field_instance_info') danielebarchiesi@0: ->condition('id', $instance['id']) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Remove field storage information when field data is purged. danielebarchiesi@0: * danielebarchiesi@0: * Called from field_purge_data() to allow the field storage danielebarchiesi@0: * module to delete field data information. danielebarchiesi@0: * danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * The pseudo-entity whose field data to delete. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The (possibly deleted) field whose data is being purged. danielebarchiesi@0: * @param $instance danielebarchiesi@0: * The deleted field instance whose data is being purged. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_storage_purge($entity_type, $entity, $field, $instance) { danielebarchiesi@0: list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); danielebarchiesi@0: danielebarchiesi@0: $table_name = _field_sql_storage_tablename($field); danielebarchiesi@0: $revision_name = _field_sql_storage_revision_tablename($field); danielebarchiesi@0: db_delete($table_name) danielebarchiesi@0: ->condition('entity_type', $entity_type) danielebarchiesi@0: ->condition('entity_id', $id) danielebarchiesi@0: ->execute(); danielebarchiesi@0: db_delete($revision_name) danielebarchiesi@0: ->condition('entity_type', $entity_type) danielebarchiesi@0: ->condition('entity_id', $id) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "addtogroup field_crud". danielebarchiesi@0: */ danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Determine whether the user has access to a given field. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from field_access() to let modules block access to danielebarchiesi@0: * operations on fields. If no module returns FALSE, the operation is allowed. danielebarchiesi@0: * danielebarchiesi@0: * @param $op danielebarchiesi@0: * The operation to be performed. Possible values: 'edit', 'view'. danielebarchiesi@0: * @param $field danielebarchiesi@0: * The field on which the operation is to be performed. danielebarchiesi@0: * @param $entity_type danielebarchiesi@0: * The type of $entity; for example, 'node' or 'user'. danielebarchiesi@0: * @param $entity danielebarchiesi@0: * (optional) The entity for the operation. danielebarchiesi@0: * @param $account danielebarchiesi@0: * (optional) The account to check; if not given use currently logged in user. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the operation is allowed, and FALSE if the operation is denied. danielebarchiesi@0: */ danielebarchiesi@0: function hook_field_access($op, $field, $entity_type, $entity, $account) { danielebarchiesi@0: if ($field['field_name'] == 'field_of_interest' && $op == 'edit') { danielebarchiesi@0: return user_access('edit field of interest', $account); danielebarchiesi@0: } danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "addtogroup hooks". danielebarchiesi@0: */