Return value of getLabel() must be of the type string

How to debug a problem in Shopware where getLabel() returns null instead of string.

100 views
d

By. Jacob

Edited: 2022-06-23 16:48

Return value of Shopware\Core\Framework\Plugin\PluginEntity::getLabel() must be of the type string, null returned

I recently had this error when running a plugin:list command for my local Shopware installation in Docker, the problem seem to happen if a plugin is not properly installed. In my case, Somehow I seem to have messed up my local installation.

Running plugin:refresh should fix the problem. E.g:

sudo -u www-data bin/console plugin:refresh

Debugging

When running the command with the -vv option it tells us the specific file where the error occurred:

Shopware\Core\Framework\Plugin\PluginEntity->getLabel() at /srv/shopware/vendor/shopware/core/Framework/Plugin/Command/Lifecycle/AbstractPluginLifecycleCommand.php:230

This shows that the problem happens because the getLabel() method returnes null instead of the expected type, string.

To further debug the problem I tried wrapping it in a try .. catch block, filling out the label with the specific error message on problematic plugins. E.g.:

try {
 $pluginLabel = $plugin->getLabel();
} catch (\Throwable $th) {
 $pluginLabel = $th->getMessage();
}

Then I simply replaced $plugin->getLabel() with $pluginLabel where it was used.

Doing this finally allowed Shopware's plugin:list command to run, outputting the specific error message as label, if missing. But this was not very helpful to do, because I already knew what the error was; it did however send me down the right track in the debugging process, because I could see exactly which plugins had the problem.

Next I tried reading the composer.json file of the problematic plugins, but oddly, the label was not missing in the composer file, so this was clearly not the issue; this was then I realised something must have gone wrong in my Shopware installation.

The fix for the error turned out to be surprisingly simple; I just had to run a plugin:refresh command. Doing this somehow reintroduced or reloaded the plugin labels, and I was able to revert my changes to the core files.

Note. Obviously it is bad to change the core files, but it is totally fine to make temporary changes for debugging purposes.

With the console commmands, remember you can use echo, print and var_dump to show output in the terminal when running the command.

Tell us what you think:

  1. Sometimes we may to manually clear cached Shopware files to fix namespace issues.
  2. How to obtain the currently selected API language from Shopware vue components.
  3. How to access file system paths from storefront twig files in Shopware.
  4. How to get or change the currently selected sales channel in an Shopware sw-sales-channel-switch component.
  5. In this tutorial you will learn how to work with Shopware entities in a generic way from PHP, without having to specifically inject the repository in your services.xml file.

More in: Shopware