You are here

function tome_sync_field_config_delete in Tome 8

Implements hook_ENTITY_TYPE_delete().

Removes the field directly from content exports. Can move to a batch process similar to the field module if needed.

File

modules/tome_sync/tome_sync.module, line 58
Keeps content, config, and files in sync.

Code

function tome_sync_field_config_delete(FieldConfig $field) {
  if (\Drupal::isConfigSyncing()) {
    return;
  }

  /** @var \Drupal\Core\Config\StorageInterface $content_storage */
  $content_storage = \Drupal::service('tome_sync.storage.content');
  $storage = \Drupal::entityTypeManager()
    ->getStorage($field
    ->getTargetEntityTypeId());
  $target_bundle = $field
    ->getTargetBundle();
  $bundle_key = $storage
    ->getEntityType()
    ->getKey('bundle');
  foreach ($content_storage
    ->listAll($field
    ->getTargetEntityTypeId() . '.') as $name) {
    if ($data = $content_storage
      ->read($name)) {
      if ($target_bundle && $bundle_key && isset($data[$bundle_key][0]['target_id']) && $data[$bundle_key][0]['target_id'] !== $target_bundle) {
        continue;
      }
      if (isset($data[$field
        ->getName()])) {
        unset($data[$field
          ->getName()]);
        $content_storage
          ->write($name, $data);
      }
    }
  }
}