You are here

class LocalStockUninstallValidator in Commerce Stock 8

Provides link to delete commerce_stock_local field values.

Hierarchy

Expanded class hierarchy of LocalStockUninstallValidator

1 string reference to 'LocalStockUninstallValidator'
commerce_stock_local.services.yml in modules/local_storage/commerce_stock_local.services.yml
modules/local_storage/commerce_stock_local.services.yml
1 service uses LocalStockUninstallValidator
commerce_stock_local_uninstall_validator in modules/local_storage/commerce_stock_local.services.yml
Drupal\commerce_stock_local\LocalStockUninstallValidator

File

modules/local_storage/src/LocalStockUninstallValidator.php, line 17

Namespace

Drupal\commerce_stock_local
View source
class LocalStockUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

  /**
   * The entity_type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity_field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Constructs a new LocalStockUninstallValidator.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity_field manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, EntityFieldManagerInterface $entity_field_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($module) {
    if ($module !== 'commerce_stock_local') {
      return [];
    }
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    $reasons = [];
    foreach ($entity_types as $entity_type_id => $entity_type) {
      if ($entity_type
        ->entityClassImplements('\\Drupal\\commerce\\PurchasableEntityInterface')) {
        $storage = $this->entityTypeManager
          ->getStorage($entity_type_id);
        if ($storage instanceof SqlContentEntityStorage) {
          foreach ($this->entityFieldManager
            ->getActiveFieldStorageDefinitions($entity_type_id) as $storage_definition) {
            if ($storage_definition
              ->getProvider() == $module && $storage instanceof FieldableEntityStorageInterface && $storage
              ->countFieldData($storage_definition, TRUE)) {
              $reasons[] = $this
                ->t('<a href=":url">Remove field values</a>: @field-name on entity type @entity_type.', [
                '@field-name' => $storage_definition
                  ->getName(),
                '@entity_type' => $entity_type
                  ->getLabel(),
                ':url' => Url::fromRoute('commerce_stock_local.prepare_module_uninstall')
                  ->toString(),
              ]);
            }
          }
        }
      }
    }
    return $reasons;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LocalStockUninstallValidator::$entityFieldManager protected property The entity_field manager.
LocalStockUninstallValidator::$entityTypeManager protected property The entity_type manager.
LocalStockUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
LocalStockUninstallValidator::__construct public function Constructs a new LocalStockUninstallValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.