class FieldUninstallValidator

Same name in this branch
  1. 8.9.x core/modules/field/src/ProxyClass/FieldUninstallValidator.php \Drupal\field\ProxyClass\FieldUninstallValidator
Same name and namespace in other branches
  1. 9 core/modules/field/src/ProxyClass/FieldUninstallValidator.php \Drupal\field\ProxyClass\FieldUninstallValidator
  2. 9 core/modules/field/src/FieldUninstallValidator.php \Drupal\field\FieldUninstallValidator
  3. 10 core/modules/field/src/ProxyClass/FieldUninstallValidator.php \Drupal\field\ProxyClass\FieldUninstallValidator
  4. 10 core/modules/field/src/FieldUninstallValidator.php \Drupal\field\FieldUninstallValidator
  5. 11.x core/modules/field/src/ProxyClass/FieldUninstallValidator.php \Drupal\field\ProxyClass\FieldUninstallValidator
  6. 11.x core/modules/field/src/FieldUninstallValidator.php \Drupal\field\FieldUninstallValidator

Prevents uninstallation of modules providing active field storage.

Hierarchy

Expanded class hierarchy of FieldUninstallValidator

1 string reference to 'FieldUninstallValidator'
field.services.yml in core/modules/field/field.services.yml
core/modules/field/field.services.yml
1 service uses FieldUninstallValidator
field.uninstall_validator in core/modules/field/field.services.yml
Drupal\field\FieldUninstallValidator

File

core/modules/field/src/FieldUninstallValidator.php, line 14

Namespace

Drupal\field
View source
class FieldUninstallValidator implements ModuleUninstallValidatorInterface {
    use StringTranslationTrait;
    
    /**
     * The field storage config storage.
     *
     * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
     */
    protected $fieldStorageConfigStorage;
    
    /**
     * The field type plugin manager.
     *
     * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
     */
    protected $fieldTypeManager;
    
    /**
     * Constructs a new FieldUninstallValidator.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     *   The string translation service.
     * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
     *   The field type plugin manager.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, FieldTypePluginManagerInterface $field_type_manager) {
        $this->fieldStorageConfigStorage = $entity_type_manager->getStorage('field_storage_config');
        $this->stringTranslation = $string_translation;
        $this->fieldTypeManager = $field_type_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($module) {
        $reasons = [];
        if ($field_storages = $this->getFieldStoragesByModule($module)) {
            // Provide an explanation message (only mention pending deletions if there
            // remain no actual, non-deleted fields.)
            $fields_in_use = [];
            foreach ($field_storages as $field_storage) {
                if (!$field_storage->isDeleted()) {
                    $fields_in_use[$field_storage->getType()][] = $field_storage->getLabel();
                }
            }
            if (!empty($fields_in_use)) {
                foreach ($fields_in_use as $field_type => $field_storages) {
                    $field_type_label = $this->getFieldTypeLabel($field_type);
                    $reasons[] = $this->formatPlural(count($fields_in_use[$field_type]), 'The %field_type_label field type is used in the following field: @fields', 'The %field_type_label field type is used in the following fields: @fields', [
                        '%field_type_label' => $field_type_label,
                        '@fields' => implode(', ', $field_storages),
                    ]);
                }
            }
            else {
                $reasons[] = $this->t('Fields pending deletion');
            }
        }
        return $reasons;
    }
    
    /**
     * Returns all field storages for a specified module.
     *
     * @param string $module
     *   The module to filter field storages by.
     *
     * @return \Drupal\field\FieldStorageConfigInterface[]
     *   An array of field storages for a specified module.
     */
    protected function getFieldStoragesByModule($module) {
        return $this->fieldStorageConfigStorage
            ->loadByProperties([
            'module' => $module,
            'include_deleted' => TRUE,
        ]);
    }
    
    /**
     * Returns the label for a specified field type.
     *
     * @param string $field_type
     *   The field type.
     *
     * @return string
     *   The field type label.
     */
    protected function getFieldTypeLabel($field_type) {
        return $this->fieldTypeManager
            ->getDefinitions()[$field_type]['label'];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
FieldUninstallValidator::$fieldStorageConfigStorage protected property The field storage config storage.
FieldUninstallValidator::$fieldTypeManager protected property The field type plugin manager.
FieldUninstallValidator::getFieldStoragesByModule protected function Returns all field storages for a specified module.
FieldUninstallValidator::getFieldTypeLabel protected function Returns the label for a specified field type.
FieldUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
FieldUninstallValidator::__construct public function Constructs a new FieldUninstallValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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