function FieldHooks::formConfigAdminImportFormAlter

Implements hook_form_FORM_ID_alter().

Adds a warning if field data will be permanently removed by the configuration synchronization.

See also

\Drupal\field\ConfigImporterFieldPurger

File

core/modules/field/src/Hook/FieldHooks.php, line 274

Class

FieldHooks
Hook implementations for field.

Namespace

Drupal\field\Hook

Code

public function formConfigAdminImportFormAlter(&$form, FormStateInterface $form_state) : void {
    // Only display the message when core.extension is available in the source
    // storage and the form is not submitted.
    $user_input = $form_state->getUserInput();
    $storage_comparer = $form_state->get('storage_comparer');
    if ($storage_comparer?->getSourceStorage()->exists('core.extension') && empty($user_input)) {
        $field_storages = ConfigImporterFieldPurger::getFieldStoragesToPurge($storage_comparer->getSourceStorage()
            ->read('core.extension'), $storage_comparer->getChangelist('delete'));
        if ($field_storages) {
            foreach ($field_storages as $field) {
                $field_labels[] = $field->label();
            }
            \Drupal::messenger()->addWarning(\Drupal::translation()->formatPlural(count($field_storages), 'This synchronization will delete data from the field %fields.', 'This synchronization will delete data from the fields: %fields.', [
                '%fields' => implode(', ', $field_labels),
            ]));
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.