Change PluginNotFoundException to an error message when not use the plugin

Created on 31 July 2023, over 1 year ago
Updated 1 August 2023, over 1 year ago

Problem/Motivation

When user update drupal, they often get a fatal error PluginNotFoundException,
it is a fatal error , prevent user visit the modules page, which prevent them install a new module that can fix the PluginNotFoundException.

Maybe drupal core can do it very well to prevent this case. But there are so many contributed modules, many of them provide custom plugin type, and may make changes to their plugins. When user update it, get a fatal error.

Only throw PluginNotFoundException when use it, other case show user an error message instead.

Feature request
Status

Active

Version

11.0 🔥

Component
Plugin 

Last updated 3 days ago

Created by

🇨🇳China g089h515r806

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @g089h515r806
  • 🇦🇺Australia larowlan 🇦🇺🏝.au GMT+10

    This exception is only thrown when Drupal tries to us the plugin, when something in config or code references it

  • 🇨🇳China g089h515r806

    thanks your reply, This exception is only thrown when Drupal tries to use the plugin, it can be divided into 2 case:
    1, Drupal plugin system find and cache the plugin info
    2, when the plugin been called, running.

    Drupal core throw an exception at 1. my suggestion is only throw it at case 2.
    At case 1, throw an error message instead. it can make update easy.

    Another solution to fixed it :
    check if it in maintenance mode. sample code:

      if($maintenance_mode===FALSE){
        throw PluginNotFoundException
      }else{
      show "PluginNotFound error message"
    }
    
  • 🇨🇳China g089h515r806

    I am the author of contributed Field validation module.
    Field validation define a plugin "FieldValidationRule", At version field validation 8.x-1.1, it does not support Drupal core's Constraint. Since version 3.0.0, it support Drupal core's Constraint, the old ways FieldValidationRule plugin still works very well at 3.0.0. I move the old plugins to a sub module field_validation_legacy.

    When install field validation 8.x-1.1, it works very well.
    When install field validation 3.0.0, it works very well.

    But update from 8.x-1.1 to 3.0.0, not very well.

    How to reproduce it:
    1, install field validation 8.x-1.1
    2, add validation rule set for Node article.
    3, add a field to Node article content type, for example field_name.
    4, add validation rule for field "field_name", use "length_field_validation_rule" plugin
    5, the validation rule works very well.

    After that:
    6, replace it with field validation 3.0.0-alpha1.
    7, clear the cache.

    Get a fatal error:
    Drupal\Component\Plugin\Exception\PluginException: Plugin (length_field_validation_rule) instance class "Drupal\field_validation\Plugin\FieldValidationRule\LengthFieldValidationRule" does not exist. in Drupal\Component\Plugin\Factory\DefaultFactory::getPluginClass() (line 97 of core\lib\Drupal\Component\Plugin\Factory\DefaultFactory.php).

    It prevent me visit pages:
    /admin/modules
    /admin/config/development/performance

    I need write a doc to tell field validation users, "Do not clear the cache, goto '/admin/modules' and enable field_validation_legacy first".

    When they get this fatal error, they can not visit admin page again. every admin pages can not visit. if they set ['system.logging']['error_level'] to "hide", which is default ,they did not know what happened :

    The website encountered an unexpected error. Please try again later.

    There are a lot method to fix it. For example " install composer, install drush, clear cache", I think the easy method is:
    1, when PluginNotFound happened, allow user still can visit the "/admin/modules" and "/admin/config/development/performance"

    "length_field_validation_rule" plugin only used at node/add/article page, every admin page can not be visited if it lost. it is very unfriendly for normal user.

    In Drupal 7, there are also a lot plugin types,provide by Ctools (Earl Miles), when plugin is not found, it mark it broken and show an error message.

  • 🇨🇳China g089h515r806

    After hack some Drupal core's code, I make it possible.

    some code changes:

    Drupal\Component\Plugin\Factory\DefaultFactory

        else {
          $plugin_definition_type = is_object($plugin_definition) ? get_class($plugin_definition) : gettype($plugin_definition);
          //throw new PluginException(sprintf('%s can only handle plugin definitions that are arrays or that implement %s, but %s given.', __CLASS__, PluginDefinitionInterface::class, $plugin_definition_type));
          return NULL;
    	}
    
        if (!class_exists($class)) {
          //throw new PluginException(sprintf('Plugin (%s) instance class "%s" does not exist.', $plugin_id, $class));
    	  return NULL;
        }
    

    replece the throw with return NULL;

    Drupal\Core\Plugin\Factory\ContainerFactory;
    add code:

    	if(empty($plugin_class)){
    		return null;
    	}
    

    Drupal\Component\Plugin\PluginManagerBase

      public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
       // return $this->getDiscovery()->getDefinition($plugin_id, $exception_on_invalid);
        return $this->getDiscovery()->getDefinition($plugin_id, FALSE);
      }
    

    Drupal\Component\Plugin\Discovery\DiscoveryTrait;

      protected function doGetDefinition(array $definitions, $plugin_id, $exception_on_invalid) {
        // Avoid using a ternary that would create a copy of the array.
    	$exception_on_invalid = FALSE;
    

    and make some change of field validation module, it works when plugin not found, user can visit admin pages now.

    It seems that doGetDefinition(array $definitions, $plugin_id, $exception_on_invalid) have exception_on_invalid support, but it not used.

  • 🇺🇸United States fortran77

    Hello,

    when trying to update the database of a live site after Drupal 10 core updates, I get from the Watchdog:

    Drupal\Component\Plugin\Exception\PluginNotFoundException: The "" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 139 of [...]/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php)

    I set up remote debugging but even so I am not clear as to what to do to fix the issue.

    My question however is: just for the sake of performing the database update after the Drupal 10 core codebase update, do I risk any major issues if I bypass the exception and just log an error message? And then restore the original exception-throwing code after the update is complete?

Production build 0.71.5 2024