Commit 375a7081 authored by alexpott's avatar alexpott

Issue #2067127 by yched: Reorganize the contents of field.* files.

parent 9ef3a52f
This diff is collapsed.
This diff is collapsed.
......@@ -94,6 +94,17 @@ function theme_field_multiple_value_form($variables) {
return $output;
}
/**
* Callback for usort() within theme_field_multiple_value_form().
*
* Sorts using ['_weight']['#value']
*/
function _field_sort_items_value_helper($a, $b) {
$a_weight = (is_array($a) && isset($a['_weight']['#value']) ? $a['_weight']['#value'] : 0);
$b_weight = (is_array($b) && isset($b['_weight']['#value']) ? $b['_weight']['#value'] : 0);
return $a_weight - $b_weight;
}
/**
* After-build callback for field elements in a form.
*
......
This diff is collapsed.
......@@ -9,18 +9,18 @@
use Drupal\Core\Template\Attribute;
use Drupal\field\FieldInterface;
use Drupal\field\FieldInstanceInterface;
use Drupal\Core\Entity\EntityNG;
/*
* Load all public Field API functions. Drupal currently has no
* mechanism for auto-loading core APIs, so we have to load them on
* every page request.
*/
require_once __DIR__ . '/field.crud.inc';
require_once __DIR__ . '/field.info.inc';
require_once __DIR__ . '/field.multilingual.inc';
require_once __DIR__ . '/field.attach.inc';
require_once __DIR__ . '/field.form.inc';
require_once __DIR__ . '/field.purge.inc';
require_once __DIR__ . '/field.deprecated.inc';
/**
* @defgroup field Field API
......@@ -332,46 +332,61 @@ function field_field_widget_info_alter(&$info) {
}
/**
* Applies language fallback rules to the fields attached to the given entity.
*
* Core language fallback rules simply check if fields have a field translation
* for the requested language code. If so, the requested language is returned,
* otherwise all the fallback candidates are inspected to see if there is a
* field translation available in another language.
* By default this is called by field_field_language_alter(), but this
* behavior can be disabled by setting the 'field.settings.language_fallback'
* variable to FALSE.
*
* @param $field_langcodes
* A reference to an array of language codes keyed by field name.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be displayed.
* @param $langcode
* The language code $entity has to be displayed in.
* Implements hook_entity_bundle_create().
*/
function field_language_fallback(&$field_langcodes, EntityInterface $entity, $langcode) {
// Lazily init fallback candidates to avoid unnecessary calls.
$fallback_candidates = NULL;
foreach ($field_langcodes as $field_name => $field_langcode) {
// If the requested language is defined for the current field use it,
// otherwise search for a fallback value among the fallback candidates.
if (isset($entity->{$field_name}[$langcode])) {
$field_langcodes[$field_name] = $langcode;
}
elseif (!empty($entity->{$field_name})) {
if (!isset($fallback_candidates)) {
require_once DRUPAL_ROOT . '/core/includes/language.inc';
$fallback_candidates = language_fallback_get_candidates();
}
foreach ($fallback_candidates as $fallback_langcode) {
if (isset($entity->{$field_name}[$fallback_langcode])) {
$field_langcodes[$field_name] = $fallback_langcode;
break;
}
}
function field_entity_bundle_create($entity_type, $bundle) {
// Clear the cache.
field_cache_clear();
}
/**
* Implements hook_entity_bundle_rename().
*/
function field_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
$instances = field_read_instances();
foreach ($instances as $instance) {
if ($instance->entity_type == $entity_type && $instance->bundle == $bundle_old) {
$id_new = $instance['entity_type'] . '.' . $bundle_new . '.' . $instance['field_name'];
$instance->id = $id_new;
$instance->bundle = $bundle_new;
$instance->allowBundleRename();
$instance->save();
}
}
// Clear the field cache.
field_cache_clear();
// Update bundle settings.
$settings = variable_get('field_bundle_settings_' . $entity_type . '__' . $bundle_old, array());
variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle_new, $settings);
variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle_old);
}
/**
* Implements hook_entity_bundle_delete().
*
* This deletes the data for the field instances as well as the field instances
* themselves. This function actually just marks the data and field instances as
* deleted, leaving the garbage collection for a separate process, because it is
* not always possible to delete this much data in a single page request
* (particularly since for some field types, the deletion is more than just a
* simple DELETE query).
*/
function field_entity_bundle_delete($entity_type, $bundle) {
// Get the instances on the bundle. field_read_instances() must be used
// here since field_info_instances() does not return instances for disabled
// entity types or bundles.
$instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle), array('include_inactive' => TRUE));
foreach ($instances as $instance) {
$instance->delete();
}
// Clear the cache.
field_cache_clear();
// Clear bundle display settings.
variable_del('field_bundle_settings_' . $entity_type . '__' . $bundle);
}
/**
......@@ -501,17 +516,6 @@ function field_get_default_value(EntityInterface $entity, $field, $instance, $la
return $items;
}
/**
* Callback for usort() within theme_field_multiple_value_form().
*
* Sorts using ['_weight']['#value']
*/
function _field_sort_items_value_helper($a, $b) {
$a_weight = (is_array($a) && isset($a['_weight']['#value']) ? $a['_weight']['#value'] : 0);
$b_weight = (is_array($b) && isset($b['_weight']['#value']) ? $b['_weight']['#value'] : 0);
return $a_weight - $b_weight;
}
/**
* Gets or sets administratively defined bundle settings.
*
......@@ -827,29 +831,6 @@ function field_view_field(EntityInterface $entity, $field_name, $display_options
return $output;
}
/**
* Returns the field items in the language they currently would be displayed.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity containing the data to be displayed.
* @param $field_name
* The field to be displayed.
* @param $langcode
* (optional) The language code $entity->{$field_name} has to be displayed in.
* Defaults to the current language.
*
* @return
* An array with available field items keyed by delta.
*
* @deprecated as of Drupal 8.0. Use
* $entity->getTranslation($langcode)->{$field_name}
*/
function field_get_items(EntityInterface $entity, $field_name, $langcode = NULL) {
$entity = $entity->getBCEntity();
$langcode = field_language($entity, $field_name, $langcode);
return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
}
/**
* Determines whether the user has access to a given field.
*
......
<?php
use Drupal\Core\Entity\EntityInterface;
/**
* @file
* Functions implementing Field API multilingual support.
*/
use Drupal\Core\Language\Language;
use Drupal\Core\Entity\EntityInterface;
/**
* @defgroup field_language Field Language API
......@@ -64,6 +63,50 @@
* the Field API.
*/
/**
* Applies language fallback rules to the fields attached to the given entity.
*
* Core language fallback rules simply check if fields have a field translation
* for the requested language code. If so, the requested language is returned,
* otherwise all the fallback candidates are inspected to see if there is a
* field translation available in another language.
* By default this is called by field_field_language_alter(), but this
* behavior can be disabled by setting the 'field.settings.language_fallback'
* variable to FALSE.
*
* @param $field_langcodes
* A reference to an array of language codes keyed by field name.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be displayed.
* @param $langcode
* The language code $entity has to be displayed in.
*/
function field_language_fallback(&$field_langcodes, EntityInterface $entity, $langcode) {
// Lazily init fallback candidates to avoid unnecessary calls.
$fallback_candidates = NULL;
foreach ($field_langcodes as $field_name => $field_langcode) {
// If the requested language is defined for the current field use it,
// otherwise search for a fallback value among the fallback candidates.
if (isset($entity->{$field_name}[$langcode])) {
$field_langcodes[$field_name] = $langcode;
}
elseif (!empty($entity->{$field_name})) {
if (!isset($fallback_candidates)) {
require_once DRUPAL_ROOT . '/core/includes/language.inc';
$fallback_candidates = language_fallback_get_candidates();
}
foreach ($fallback_candidates as $fallback_langcode) {
if (isset($entity->{$field_name}[$fallback_langcode])) {
$field_langcodes[$field_name] = $fallback_langcode;
break;
}
}
}
}
}
/**
* Collects the available language codes for the given entity type and field.
*
......
......@@ -7,151 +7,8 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Entity\Field;
use Drupal\field\Entity\FieldInstance;
use Drupal\field\FieldException;
/**
* @defgroup field_crud Field CRUD API
* @{
* Creates, updates, and deletes Field API fields, bundles, and instances.
*
* Modules use this API, often in hook_install(), to create custom data
* structures. UI modules will use it to create a user interface.
*
* The Field CRUD API uses @link field Field API data structures @endlink.
*
* See @link field Field API @endlink for information about the other parts of
* the Field API.
*/
/**
* Reads a single field record directly from the database.
*
* Generally, you should use the field_info_field() instead.
*
* This function will not return deleted fields. Use field_read_fields() instead
* for this purpose.
*
* @param $field_name
* The field name to read.
* @param array $include_additional
* The default behavior of this function is to not return a field that is
* inactive. Setting $include_additional['include_inactive'] to TRUE will
* override this behavior.
*
* @return
* A field definition array, or FALSE.
*
* @deprecated as of Drupal 8.0. Use
* entity_load('field_entity', 'field_name').
*/
function field_read_field($field_name, $include_additional = array()) {
$fields = field_read_fields(array('field_name' => $field_name), $include_additional);
return $fields ? current($fields) : FALSE;
}
/**
* Reads in fields that match an array of conditions.
*
* @param array $conditions
* An array of conditions to match against. Keys are names of properties found
* in field configuration files, and values are conditions to match.
* @param array $include_additional
* The default behavior of this function is to not return fields that are
* inactive or have been deleted. Setting
* $include_additional['include_inactive'] or
* $include_additional['include_deleted'] to TRUE will override this behavior.
*
* @return
* An array of fields matching $params. If
* $include_additional['include_deleted'] is TRUE, the array is keyed by
* field ID, otherwise it is keyed by field name.
*
* @deprecated as of Drupal 8.0. Use
* entity_load_multiple_by_properties('field_entity', $conditions).
*/
function field_read_fields($conditions = array(), $include_additional = array()) {
// Include inactive fields if specified in the $include_additional parameter.
$include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive'];
// Include deleted fields if specified either in the $include_additional or
// the $conditions parameters.
$include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($conditions['deleted']) && $conditions['deleted']);
// Pass include_inactive and include_deleted to the $conditions array.
$conditions['include_inactive'] = $include_inactive;
$conditions['include_deleted'] = $include_deleted;
return entity_load_multiple_by_properties('field_entity', $conditions);
}
/**
* Reads a single instance record from the database.
*
* Generally, you should use field_info_instance() instead, as it provides
* caching and allows other modules the opportunity to append additional
* formatters, widgets, and other information.
*
* @param $entity_type
* The type of entity to which the field is bound.
* @param $field_name
* The field name to read.
* @param $bundle
* The bundle to which the field is bound.
* @param array $include_additional
* The default behavior of this function is to not return an instance that has
* been deleted, or whose field is inactive. Setting
* $include_additional['include_inactive'] or
* $include_additional['include_deleted'] to TRUE will override this behavior.
*
* @return
* An instance structure, or FALSE.
*
* @deprecated as of Drupal 8.0. Use
* entity_load('field_instance', 'field_name').
*/
function field_read_instance($entity_type, $field_name, $bundle, $include_additional = array()) {
$instances = field_read_instances(array('entity_type' => $entity_type, 'field_name' => $field_name, 'bundle' => $bundle), $include_additional);
return $instances ? current($instances) : FALSE;
}
/**
* Reads in field instances that match an array of conditions.
*
* @param $param
* An array of properties to use in selecting a field instance. Keys are names
* of properties found in field instance configuration files, and values are
* conditions to match.
* @param $include_additional
* The default behavior of this function is to not return field instances that
* have been marked deleted, or whose field is inactive. Setting
* $include_additional['include_inactive'] or
* $include_additional['include_deleted'] to TRUE will override this behavior.
*
* @return
* An array of instances matching the arguments.
*
* @deprecated as of Drupal 8.0. Use
* entity_load_multiple_by_properties('field_instance', $conditions).
*/
function field_read_instances($conditions = array(), $include_additional = array()) {
// Include instances of inactive fields if specified in the
// $include_additional parameter.
$include_inactive = isset($include_additional['include_inactive']) && $include_additional['include_inactive'];
// Include deleted instances if specified either in the $include_additional
// or the $conditions parameters.
$include_deleted = (isset($include_additional['include_deleted']) && $include_additional['include_deleted']) || (isset($conditions['deleted']) && $conditions['deleted']);
// Pass include_inactive and include_deleted to the $conditions array.
$conditions['include_inactive'] = $include_inactive;
$conditions['include_deleted'] = $include_deleted;
return entity_load_multiple_by_properties('field_instance', $conditions);
}
/**
* @} End of "defgroup field_crud".
*/
/**
* @defgroup field_purge Field API bulk data deletion
* @{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment