You are here

class MenuItemExtrasUninstallValidator in Menu Item Extras 8.2

Same name in this branch
  1. 8.2 src/MenuItemExtrasUninstallValidator.php \Drupal\menu_item_extras\MenuItemExtrasUninstallValidator
  2. 8.2 src/ProxyClass/MenuItemExtrasUninstallValidator.php \Drupal\menu_item_extras\ProxyClass\MenuItemExtrasUninstallValidator

Prevents uninstall menu item extras module if there are extra data.

Hierarchy

Expanded class hierarchy of MenuItemExtrasUninstallValidator

1 string reference to 'MenuItemExtrasUninstallValidator'
menu_item_extras.services.yml in ./menu_item_extras.services.yml
menu_item_extras.services.yml
1 service uses MenuItemExtrasUninstallValidator
menu_item_extras.uninstall_validator in ./menu_item_extras.services.yml
Drupal\menu_item_extras\MenuItemExtrasUninstallValidator

File

src/MenuItemExtrasUninstallValidator.php, line 14

Namespace

Drupal\menu_item_extras
View source
class MenuItemExtrasUninstallValidator implements ModuleUninstallValidatorInterface {
  use StringTranslationTrait;

  /**
   * The current database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs a new MenuItemExtrasUninstallValidator.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The current database connection.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(Connection $connection, TranslationInterface $string_translation) {
    $this->connection = $connection;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function validate($module) {
    if ($module === 'menu_item_extras' && $this
      ->hasExtraData()) {
      $reasons = [];
      $reasons[] = $this
        ->t('There are extra data for menus. <a href=":url">Remove extra data</a>.', [
        ':url' => Url::fromRoute('menu_item_extras.clear_all_extra_data')
          ->toString(),
      ]);
      return $reasons;
    }
  }

  /**
   * Determines if there is any extra data for menu or not.
   *
   * @return bool
   *   TRUE if there are extra data for menus, FALSE otherwise.
   */
  protected function hasExtraData() {
    $results = $this->connection
      ->select('menu_link_content_data', 'mlcd')
      ->fields('mlcd', [
      'view_mode',
    ])
      ->isNotNull('view_mode')
      ->execute()
      ->fetchAll();
    return !empty($results);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuItemExtrasUninstallValidator::$connection protected property The current database connection.
MenuItemExtrasUninstallValidator::hasExtraData protected function Determines if there is any extra data for menu or not.
MenuItemExtrasUninstallValidator::validate public function Determines the reasons a module can not be uninstalled. Overrides ModuleUninstallValidatorInterface::validate
MenuItemExtrasUninstallValidator::__construct public function Constructs a new MenuItemExtrasUninstallValidator.
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.