Issue #2018731 by swentel: Move field_has_data() to Field::hasData().

8.0.x
Alex Pott 2013-06-17 02:22:30 +02:00
parent cf8f023c02
commit 30f6062a18
18 changed files with 74 additions and 88 deletions

View File

@ -115,7 +115,7 @@ function datetime_field_info() {
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function datetime_field_settings_form($field, $instance, $has_data) { function datetime_field_settings_form($field, $instance) {
$settings = $field['settings']; $settings = $field['settings'];
$form['datetime_type'] = array( $form['datetime_type'] = array(

View File

@ -143,7 +143,7 @@ function entity_reference_field_validate(EntityInterface $entity = NULL, $field,
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function entity_reference_field_settings_form($field, $instance, $has_data) { function entity_reference_field_settings_form($field, $instance) {
// Select the target entity type. // Select the target entity type.
$entity_type_options = array(); $entity_type_options = array();
foreach (entity_get_info() as $entity_type => $entity_info) { foreach (entity_get_info() as $entity_type => $entity_info) {
@ -161,7 +161,7 @@ function entity_reference_field_settings_form($field, $instance, $has_data) {
'#options' => $entity_type_options, '#options' => $entity_type_options,
'#default_value' => $field['settings']['target_type'], '#default_value' => $field['settings']['target_type'],
'#required' => TRUE, '#required' => TRUE,
'#disabled' => $has_data, '#disabled' => $field->hasData(),
'#size' => 1, '#size' => 1,
); );
@ -173,7 +173,7 @@ function entity_reference_field_settings_form($field, $instance, $has_data) {
* *
* Reset the instance handler settings, when the target type is changed. * Reset the instance handler settings, when the target type is changed.
*/ */
function entity_reference_field_update_field($field, $prior_field, $has_data) { function entity_reference_field_update_field($field, $prior_field) {
if ($field['type'] != 'entity_reference') { if ($field['type'] != 'entity_reference') {
// Not an entity reference field. // Not an entity reference field.
return; return;

View File

@ -541,11 +541,9 @@ function hook_field_update(\Drupal\Core\Entity\EntityInterface $entity, $field,
* The updated field structure to be saved. * The updated field structure to be saved.
* @param $prior_field * @param $prior_field
* The previously-saved field structure. * The previously-saved field structure.
* @param $has_data
* TRUE if the field has data in storage currently.
*/ */
function hook_field_storage_update_field($field, $prior_field, $has_data) { function hook_field_storage_update_field($field, $prior_field) {
if (!$has_data) { if (!$field->hasData()) {
// There is no data. Re-create the tables completely. // There is no data. Re-create the tables completely.
$prior_schema = _field_sql_storage_schema($prior_field); $prior_schema = _field_sql_storage_schema($prior_field);
foreach ($prior_schema as $name => $table) { foreach ($prior_schema as $name => $table) {
@ -1853,15 +1851,13 @@ function hook_field_create_instance($instance) {
* The field as it will be post-update. * The field as it will be post-update.
* @param $prior_field * @param $prior_field
* The field as it is pre-update. * The field as it is pre-update.
* @param $has_data
* Whether any data already exists for this field.
*/ */
function hook_field_update_forbid($field, $prior_field, $has_data) { function hook_field_update_forbid($field, $prior_field) {
// A 'list' field stores integer keys mapped to display values. If // A 'list' field stores integer keys mapped to display values. If
// the new field will have fewer values, and any data exists for the // the new field will have fewer values, and any data exists for the
// abandoned keys, the field will have no way to display them. So, // abandoned keys, the field will have no way to display them. So,
// forbid such an update. // forbid such an update.
if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) { if ($field->hasData() && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) {
// Identify the keys that will be lost. // Identify the keys that will be lost.
$lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values'])); $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values']));
// If any data exist for those keys, forbid the update. // If any data exist for those keys, forbid the update.
@ -1887,10 +1883,8 @@ function hook_field_update_forbid($field, $prior_field, $has_data) {
* The field as it is post-update. * The field as it is post-update.
* @param $prior_field * @param $prior_field
* The field as it was pre-update. * The field as it was pre-update.
* @param $has_data
* Whether any data already exists for this field.
*/ */
function hook_field_update_field($field, $prior_field, $has_data) { function hook_field_update_field($field, $prior_field) {
// Reset the static value that keeps track of allowed values for list fields. // Reset the static value that keeps track of allowed values for list fields.
drupal_static_reset('list_allowed_values'); drupal_static_reset('list_allowed_values');
} }

View File

@ -885,43 +885,6 @@ function field_get_items(EntityInterface $entity, $field_name, $langcode = NULL)
return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array(); return isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
} }
/**
* Determines whether a field has any data.
*
* @param $field
* A field structure.
*
* @return
* TRUE if the field has data for any entity; FALSE otherwise.
*/
function field_has_data($field) {
$columns = array_keys($field['columns']);
$factory = Drupal::service('entity.query');
foreach ($field['bundles'] as $entity_type => $bundle) {
// Entity Query throws an exception if there is no base table.
$entity_info = entity_get_info($entity_type);
if (!isset($entity_info['base_table'])) {
continue;
}
$query = $factory->get($entity_type);
$group = $query->orConditionGroup();
foreach ($columns as $column) {
$group->exists($field['field_name'] . '.' . $column);
}
$result = $query
->condition($group)
->count()
->accessCheck(FALSE)
->range(0, 1)
->execute();
if ($result) {
return TRUE;
}
}
return FALSE;
}
/** /**
* Determines whether the user has access to a given field. * Determines whether the user has access to a given field.
* *

View File

@ -1,8 +1,8 @@
<?php <?php
/* /**
* @file * @file
* Definition of Drupal\field\FieldUpdateForbiddenException. * Contains \Drupal\field\FieldUpdateForbiddenException.
*/ */
namespace Drupal\field; namespace Drupal\field;

View File

@ -420,17 +420,15 @@ class Field extends ConfigEntityBase implements FieldInterface {
// objects. // objects.
$this->settings += $original->settings; $this->settings += $original->settings;
$has_data = field_has_data($this);
// See if any module forbids the update by throwing an exception. This // See if any module forbids the update by throwing an exception. This
// invokes hook_field_update_forbid(). // invokes hook_field_update_forbid().
$module_handler->invokeAll('field_update_forbid', array($this, $original, $has_data)); $module_handler->invokeAll('field_update_forbid', array($this, $original));
// Tell the storage engine to update the field by invoking the // Tell the storage engine to update the field by invoking the
// hook_field_storage_update_field(). The storage engine can reject the // hook_field_storage_update_field(). The storage engine can reject the
// definition update as invalid by raising an exception, which stops // definition update as invalid by raising an exception, which stops
// execution before the definition is written to config. // execution before the definition is written to config.
$module_handler->invoke($this->storage['module'], 'field_storage_update_field', array($this, $original, $has_data)); $module_handler->invoke($this->storage['module'], 'field_storage_update_field', array($this, $original));
// Save the configuration. // Save the configuration.
$result = parent::save(); $result = parent::save();
@ -438,7 +436,7 @@ class Field extends ConfigEntityBase implements FieldInterface {
// Invoke hook_field_update_field() after the cache is cleared for API // Invoke hook_field_update_field() after the cache is cleared for API
// consistency. // consistency.
$module_handler->invokeAll('field_update_field', array($this, $original, $has_data)); $module_handler->invokeAll('field_update_field', array($this, $original));
return $result; return $result;
} }
@ -724,4 +722,38 @@ class Field extends ConfigEntityBase implements FieldInterface {
return array('deleted'); return array('deleted');
} }
/**
* Determines whether a field has any data.
*
* @return
* TRUE if the field has data for any entity; FALSE otherwise.
*/
public function hasData() {
$storage_details = $this->getSchema();
$columns = array_keys($storage_details['columns']);
$factory = \Drupal::service('entity.query');
foreach ($this->getBundles() as $entity_type => $bundle) {
// Entity Query throws an exception if there is no base table.
$entity_info = \Drupal::entityManager()->getDefinition($entity_type);
if (!isset($entity_info['base_table'])) {
continue;
}
$query = $factory->get($entity_type);
$group = $query->orConditionGroup();
foreach ($columns as $column) {
$group->exists($this->id() . '.' . $column);
}
$result = $query
->condition($group)
->count()
->accessCheck(FALSE)
->range(0, 1)
->execute();
if ($result) {
return TRUE;
}
}
return FALSE;
}
} }

View File

@ -64,7 +64,7 @@ function field_test_field_widget_info_alter(&$info) {
/** /**
* Implements hook_field_update_forbid(). * Implements hook_field_update_forbid().
*/ */
function field_test_field_update_forbid($field, $prior_field, $has_data) { function field_test_field_update_forbid($field, $prior_field) {
if ($field['type'] == 'test_field' && $field['settings']['unchangeable'] != $prior_field['settings']['unchangeable']) { if ($field['type'] == 'test_field' && $field['settings']['unchangeable'] != $prior_field['settings']['unchangeable']) {
throw new FieldException("field_test 'unchangeable' setting cannot be changed'"); throw new FieldException("field_test 'unchangeable' setting cannot be changed'");
} }
@ -145,7 +145,7 @@ function field_test_field_is_empty($item, $field_type) {
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function field_test_field_settings_form($field, $instance, $has_data) { function field_test_field_settings_form($field, $instance) {
$settings = $field['settings']; $settings = $field['settings'];
$form['test_field_setting'] = array( $form['test_field_setting'] = array(

View File

@ -266,8 +266,8 @@ function field_sql_storage_field_create_field($field) {
* Forbids any field update that changes column definitions if there is any * Forbids any field update that changes column definitions if there is any
* data. * data.
*/ */
function field_sql_storage_field_update_forbid($field, $prior_field, $has_data) { function field_sql_storage_field_update_forbid($field, $prior_field) {
if ($has_data && $field['columns'] != $prior_field['columns']) { if ($field->hasData() && $field['columns'] != $prior_field['columns']) {
throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data."); throw new FieldUpdateForbiddenException("field_sql_storage cannot change the schema for an existing field with data.");
} }
} }
@ -275,8 +275,8 @@ function field_sql_storage_field_update_forbid($field, $prior_field, $has_data)
/** /**
* Implements hook_field_storage_update_field(). * Implements hook_field_storage_update_field().
*/ */
function field_sql_storage_field_storage_update_field($field, $prior_field, $has_data) { function field_sql_storage_field_storage_update_field($field, $prior_field) {
if (! $has_data) { if (!$field->hasData()) {
// There is no data. Re-create the tables completely. // There is no data. Re-create the tables completely.
if (Database::getConnection()->supportsTransactionalDDL()) { if (Database::getConnection()->supportsTransactionalDDL()) {

View File

@ -29,13 +29,11 @@
* The field structure being configured. * The field structure being configured.
* @param $instance * @param $instance
* The instance structure being configured. * The instance structure being configured.
* @param $has_data
* TRUE if the field already has data, FALSE if not.
* *
* @return * @return
* The form definition for the field settings. * The form definition for the field settings.
*/ */
function hook_field_settings_form($field, $instance, $has_data) { function hook_field_settings_form($field, $instance) {
$settings = $field['settings']; $settings = $field['settings'];
$form['max_length'] = array( $form['max_length'] = array(
'#type' => 'number', '#type' => 'number',

View File

@ -82,8 +82,7 @@ class FieldEditForm implements FormInterface, ControllerInterface {
// See if data already exists for this field. // See if data already exists for this field.
// If so, prevent changes to the field settings. // If so, prevent changes to the field settings.
$has_data = field_has_data($field); if ($field->hasData()) {
if ($has_data) {
$form['field']['#prefix'] = '<div class="messages messages--error">' . t('There is data for this field in the database. The field settings can no longer be changed.') . '</div>' . $form['field']['#prefix']; $form['field']['#prefix'] = '<div class="messages messages--error">' . t('There is data for this field in the database. The field settings can no longer be changed.') . '</div>' . $form['field']['#prefix'];
} }
@ -135,7 +134,7 @@ class FieldEditForm implements FormInterface, ControllerInterface {
$form['field']['settings'] = array( $form['field']['settings'] = array(
'#weight' => 10, '#weight' => 10,
); );
$additions = \Drupal::moduleHandler()->invoke($field['module'], 'field_settings_form', array($field, $this->instance, $has_data)); $additions = \Drupal::moduleHandler()->invoke($field['module'], 'field_settings_form', array($field, $this->instance));
if (is_array($additions)) { if (is_array($additions)) {
$form['field']['settings'] += $additions; $form['field']['settings'] += $additions;
} }

View File

@ -37,7 +37,7 @@ function file_field_info() {
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function file_field_settings_form($field, $instance, $has_data) { function file_field_settings_form($field, $instance) {
$defaults = field_info_field_settings($field['type']); $defaults = field_info_field_settings($field['type']);
$settings = array_merge($defaults, $field['settings']); $settings = array_merge($defaults, $field['settings']);
@ -71,7 +71,7 @@ function file_field_settings_form($field, $instance, $has_data) {
'#options' => $scheme_options, '#options' => $scheme_options,
'#default_value' => $settings['uri_scheme'], '#default_value' => $settings['uri_scheme'],
'#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'), '#description' => t('Select where the final files should be stored. Private file storage has significantly more overhead than public files, but allows restricted access to files within this field.'),
'#disabled' => $has_data, '#disabled' => $field->hasData(),
); );
return $form; return $form;

View File

@ -364,7 +364,7 @@ function image_field_delete_field($field) {
/** /**
* Implements hook_field_update_field(). * Implements hook_field_update_field().
*/ */
function image_field_update_field($field, $prior_field, $has_data) { function image_field_update_field($field, $prior_field) {
if ($field['type'] != 'image') { if ($field['type'] != 'image') {
return; return;
} }

View File

@ -56,7 +56,7 @@ function number_field_info() {
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function number_field_settings_form($field, $instance, $has_data) { function number_field_settings_form($field, $instance) {
$settings = $field['settings']; $settings = $field['settings'];
$form = array(); $form = array();
@ -67,7 +67,7 @@ function number_field_settings_form($field, $instance, $has_data) {
'#options' => drupal_map_assoc(range(10, 32)), '#options' => drupal_map_assoc(range(10, 32)),
'#default_value' => $settings['precision'], '#default_value' => $settings['precision'],
'#description' => t('The total number of digits to store in the database, including those to the right of the decimal.'), '#description' => t('The total number of digits to store in the database, including those to the right of the decimal.'),
'#disabled' => $has_data, '#disabled' => $field->hasData(),
); );
$form['scale'] = array( $form['scale'] = array(
'#type' => 'select', '#type' => 'select',
@ -75,7 +75,7 @@ function number_field_settings_form($field, $instance, $has_data) {
'#options' => drupal_map_assoc(range(0, 10)), '#options' => drupal_map_assoc(range(0, 10)),
'#default_value' => $settings['scale'], '#default_value' => $settings['scale'],
'#description' => t('The number of digits to the right of the decimal.'), '#description' => t('The number of digits to the right of the decimal.'),
'#disabled' => $has_data, '#disabled' => $field->hasData(),
); );
} }

View File

@ -68,7 +68,7 @@ function options_field_info() {
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function options_field_settings_form($field, $instance, $has_data) { function options_field_settings_form($field, $instance) {
$settings = $field['settings']; $settings = $field['settings'];
switch ($field['type']) { switch ($field['type']) {
@ -81,7 +81,7 @@ function options_field_settings_form($field, $instance, $has_data) {
'#default_value' => options_allowed_values_string($settings['allowed_values']), '#default_value' => options_allowed_values_string($settings['allowed_values']),
'#rows' => 10, '#rows' => 10,
'#element_validate' => array('options_field_settings_form_validate_allowed_values'), '#element_validate' => array('options_field_settings_form_validate_allowed_values'),
'#field_has_data' => $has_data, '#field_has_data' => $field->hasData(),
'#field' => $field, '#field' => $field,
'#field_type' => $field['type'], '#field_type' => $field['type'],
'#access' => empty($settings['allowed_values_function']), '#access' => empty($settings['allowed_values_function']),
@ -217,7 +217,7 @@ function options_field_settings_form_value_boolean_allowed_values($element, $inp
/** /**
* Implements hook_field_update_field(). * Implements hook_field_update_field().
*/ */
function options_field_update_field($field, $prior_field, $has_data) { function options_field_update_field($field, $prior_field) {
drupal_static_reset('options_allowed_values'); drupal_static_reset('options_allowed_values');
} }
@ -368,8 +368,8 @@ function options_allowed_values_string($values) {
/** /**
* Implements hook_field_update_forbid(). * Implements hook_field_update_forbid().
*/ */
function options_field_update_forbid($field, $prior_field, $has_data) { function options_field_update_forbid($field, $prior_field) {
if ($field['module'] == 'options' && $has_data) { if ($field['module'] == 'options' && $field->hasData()) {
// Forbid any update that removes allowed values with actual data. // Forbid any update that removes allowed values with actual data.
$lost_keys = array_diff(array_keys($prior_field['settings']['allowed_values']), array_keys($field['settings']['allowed_values'])); $lost_keys = array_diff(array_keys($prior_field['settings']['allowed_values']), array_keys($field['settings']['allowed_values']));
if (_options_values_in_use($field, $lost_keys)) { if (_options_values_in_use($field, $lost_keys)) {

View File

@ -1053,7 +1053,7 @@ function taxonomy_autocomplete_validate($element, &$form_state) {
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function taxonomy_field_settings_form($field, $instance, $has_data) { function taxonomy_field_settings_form($field, $instance) {
// Get proper values for 'allowed_values_function', which is a core setting. // Get proper values for 'allowed_values_function', which is a core setting.
$vocabularies = taxonomy_vocabulary_load_multiple(); $vocabularies = taxonomy_vocabulary_load_multiple();
$options = array(); $options = array();
@ -1072,7 +1072,7 @@ function taxonomy_field_settings_form($field, $instance, $has_data) {
'#options' => $options, '#options' => $options,
'#required' => TRUE, '#required' => TRUE,
'#description' => t('The vocabulary which supplies the options for this field.'), '#description' => t('The vocabulary which supplies the options for this field.'),
'#disabled' => $has_data, '#disabled' => $field->hasData(),
); );
$form['allowed_values'][$delta]['parent'] = array( $form['allowed_values'][$delta]['parent'] = array(
'#type' => 'value', '#type' => 'value',

View File

@ -84,7 +84,7 @@ function text_field_info() {
/** /**
* Implements hook_field_settings_form(). * Implements hook_field_settings_form().
*/ */
function text_field_settings_form($field, $instance, $has_data) { function text_field_settings_form($field, $instance) {
$settings = $field['settings']; $settings = $field['settings'];
$form = array(); $form = array();
@ -99,7 +99,7 @@ function text_field_settings_form($field, $instance, $has_data) {
'#min' => 1, '#min' => 1,
// @todo: If $has_data, add a validate handler that only allows // @todo: If $has_data, add a validate handler that only allows
// max_length to increase. // max_length to increase.
'#disabled' => $has_data, '#disabled' => $field->hasData(),
); );
} }

View File

@ -346,7 +346,7 @@ function _translation_entity_update_field_translatability($settings) {
$field_operations = array( $field_operations = array(
array('translation_entity_translatable_switch', array($translatable, $field_name)), array('translation_entity_translatable_switch', array($translatable, $field_name)),
); );
if (field_has_data($field)) { if ($field->hasData()) {
$field_operations[] = array('translation_entity_translatable_batch', array($translatable, $field_name)); $field_operations[] = array('translation_entity_translatable_batch', array($translatable, $field_name));
$field_operations = $translatable ? $field_operations : array_reverse($field_operations); $field_operations = $translatable ? $field_operations : array_reverse($field_operations);
} }

View File

@ -810,7 +810,7 @@ function translation_entity_form_field_ui_field_edit_form_alter(array &$form, ar
$translatable = $field['translatable']; $translatable = $field['translatable'];
$label = t('Field translation'); $label = t('Field translation');
if (field_has_data($field)) { if ($field->hasData()) {
$form['field']['translatable'] = array( $form['field']['translatable'] = array(
'#type' => 'item', '#type' => 'item',
'#title' => $label, '#title' => $label,