function field_sync_field_status

Refreshes the 'active' and 'storage[active]' values for fields.

Related topics

4 calls to field_sync_field_status()
field_cron in drupal/core/modules/field/field.module
Implements hook_cron().
field_modules_disabled in drupal/core/modules/field/field.module
Implements hook_modules_disabled().
field_modules_enabled in drupal/core/modules/field/field.module
Implements hook_modules_enabled().
field_rebuild in drupal/core/modules/field/field.module
Implements hook_rebuild().

File

drupal/core/modules/field/field.module, line 402
Attach custom data fields to Drupal entities.

Code

function field_sync_field_status() {
  $module_handler = Drupal::moduleHandler();
  $state = Drupal::state();

  // Get both deleted and non-deleted field definitions.
  $fields = array();
  foreach (config_get_storage_names_with_prefix('field.field') as $name) {
    $field = Drupal::config($name)
      ->get();
    $fields[$field['uuid']] = $field;
  }
  $deleted_fields = $state
    ->get('field.field.deleted') ?: array();
  $fields += $deleted_fields;
  if (empty($fields)) {
    return;
  }

  // Set the 'module' and 'active' values for the current set of enabled
  // modules.
  $changed = array();
  $modules = $module_handler
    ->getModuleList();
  foreach ($modules as $module => $module_info) {

    // Collect field types and storage backends exposed by the module.
    $field_types = (array) $module_handler
      ->invoke($module, 'field_info');
    $storage_types = (array) $module_handler
      ->invoke($module, 'field_storage_info');
    if ($field_types || $storage_types) {
      foreach ($fields as $uuid => &$field) {

        // Associate field types.
        if (isset($field_types[$field['type']]) && ($field['module'] !== $module || !$field['active'])) {
          $field['module'] = $module;
          $field['active'] = TRUE;
          $changed[$uuid] = $field;
        }

        // Associate storage backends.
        if (isset($storage_types[$field['storage']['type']]) && ($field['storage']['module'] !== $module || !$field['storage']['active'])) {
          $field['storage']['module'] = $module;
          $field['storage']['active'] = TRUE;
          $changed[$uuid] = $field;
        }
      }
    }
  }

  // Set fields with missing field type or storage modules to inactive.
  foreach ($fields as $uuid => &$field) {
    if (!isset($modules[$field['module']]) && $field['active']) {
      $field['active'] = FALSE;
      $changed[$uuid] = $field;
    }
    if (!isset($modules[$field['storage']['module']]) && $field['storage']['active']) {
      $field['storage']['active'] = FALSE;
      $changed[$uuid] = $field;
    }
  }

  // Store the updated field definitions.
  foreach ($changed as $uuid => $field) {
    if (!empty($field['deleted'])) {
      $deleted_fields[$uuid] = $field;
    }
    else {
      Drupal::config('field.field.' . $field['id'])
        ->set('module', $field['module'])
        ->set('active', $field['active'])
        ->set('storage.module', $field['storage']['module'])
        ->set('storage.active', $field['storage']['active'])
        ->save();
    }
  }
  $state
    ->set('field.field.deleted', $deleted_fields);
  field_cache_clear();
}