Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(AssetDefinition): prevent PHP warning about missing translation on create #17084

Conversation

stonebuzz
Copy link
Contributor

@stonebuzz stonebuzz commented May 6, 2024

Fix this :
image

[2024-05-06 14:10:20] glpiphplog.WARNING:   *** PHP User Warning (512): Invalid `translations` value (``). in /home/stanislas/TECLIB/DEV/GLPI/main/src/Asset/AssetDefinition.php at line 1002
  Backtrace :
  src/Asset/AssetDefinition.php:1002                 trigger_error()
  src/Asset/AssetDefinition.php:654                  Glpi\Asset\AssetDefinition->getDecodedTranslationsField()
  src/Asset/AssetDefinition.php:82                   Glpi\Asset\AssetDefinition->getTranslatedName()
  src/CommonDBTM.php:5948                            Glpi\Asset\AssetDefinition->computeFriendlyName()
  src/CommonDBTM.php:3690                            CommonDBTM->getFriendlyName()
  src/CommonGLPI.php:1225                            CommonDBTM->getName()
  .../twig/twig/src/Extension/CoreExtension.php:1635 CommonGLPI->getHeaderName()
  ...ates/83/83cb1d5dbb7dc4e31a22a897fd38ba3f.php:48 twig_get_attribute()
  vendor/twig/twig/src/Template.php:394              __TwigTemplate_a1d641ae134360cd1fb6af8c3d282bf8->doDisplay()
  vendor/twig/twig/src/Template.php:367              Twig\Template->displayWithErrorHandling()
  vendor/twig/twig/src/Template.php:379              Twig\Template->display()
  vendor/twig/twig/src/TemplateWrapper.php:38        Twig\Template->render()
  .../twig/twig/src/Extension/CoreExtension.php:1347 Twig\TemplateWrapper->render()
  ...tes/5b/5b0c5b08cf4633d57fd30cce258c96d9.php:119 twig_include()
  vendor/twig/twig/src/Template.php:394              __TwigTemplate_7a30caf14155d653e8b66e3023434fad->doDisplay()
  vendor/twig/twig/src/Template.php:367              Twig\Template->displayWithErrorHandling()
  vendor/twig/twig/src/Template.php:379              Twig\Template->display()
  vendor/twig/twig/src/TemplateWrapper.php:38        Twig\Template->render()
  .../twig/twig/src/Extension/CoreExtension.php:1347 Twig\TemplateWrapper->render()
  ...ates/bb/bb9a33248aefcf5fe6ab393447b28839.php:64 twig_include()
  vendor/twig/twig/src/Template.php:394              __TwigTemplate_02cbc319cab622925b02080ec3a01f21->doDisplay()
  vendor/twig/twig/src/Template.php:367              Twig\Template->displayWithErrorHandling()
  ...ates/8f/8ff920457e8b0138eae24562cf3d538e.php:48 Twig\Template->display()
  vendor/twig/twig/src/Template.php:394              __TwigTemplate_d7bc727e1d310d71ed83da0568816170->doDisplay()
  vendor/twig/twig/src/Template.php:367              Twig\Template->displayWithErrorHandling()
  vendor/twig/twig/src/TemplateWrapper.php:45        Twig\Template->display()
  src/Application/View/TemplateRenderer.php:185      Twig\TemplateWrapper->display()
  src/Asset/AssetDefinition.php:191                  Glpi\Application\View\TemplateRenderer->display()
  src/CommonGLPI.php:701                             Glpi\Asset\AssetDefinition->showForm()
  ajax/common.tabs.php:114                           CommonGLPI::displayStandardTab()
  public/index.php:83                                require()
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #number

@stonebuzz stonebuzz self-assigned this May 6, 2024
@stonebuzz stonebuzz added the bug label May 6, 2024
@stonebuzz stonebuzz added this to the 11.0.0 milestone May 6, 2024
Comment on lines 994 to 998
// prevent warning when no translation are available (create step)
if (empty($this->fields['translations'])) {
$this->fields['translations'] = '[]'; // prevent warning to be triggered on each method call
return [];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may hide unexpected empty value on existing items.
Another possible fix is to check if item is new in the getTranslatedName() method:

    public function getTranslatedName(int $count = 1): string
    {
        if ($this->isNewItem()) {
            return '';
        }

By the way, I do not even see where the translated name would be displayed in the creation form.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cedric-anne cedric-anne merged commit e4d8778 into glpi-project:main May 7, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants