Problem/Motivation

Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated in Drupal\taxonomy\TermForm->buildEntity()

Steps to reproduce

1. Add a field that uses ajax in its widget to a vocabulary (for example file/image or a field with unlimited cardinality)
2. Go to the add term form
3. Trigger an ajax operation without entering a term name

Proposed resolution

Change \Drupal\taxonomy\Entity\Term::getName to conform to the interface and always return a string.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3277238

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir created an issue. See original summary.

Berdir’s picture

Issue summary: View changes
andregp’s picture

Status: Active » Needs review
FileSize
701 bytes

Here is a patch :)

hmendes’s picture

Status: Needs review » Reviewed & tested by the community

Tested the patch here and it worked for me, and the code follows the "Proposed resolution".
Steps:

  • Opened an Drupal environment with PHP 8.1
  • Went to /admin/structure/taxonomy/manage/tags/overview/fields, added an image field with all default configurations
  • Went to /admin/structure/taxonomy/manage/tags/add to add a tag
  • Before applying the patch:
  • When i add an image before setting the term name, an message shows up (same error of the IS)
  • After applying the patch:
  • I can add an image without getting the message

Changing this to RTBC.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: 3277238-3.patch, failed testing. View results

Berdir’s picture

Status: Needs work » Reviewed & tested by the community

1) Drupal\Tests\layout_builder\FunctionalJavascript\BlockFormMessagesTest::testValidationMessage
Failed asserting that a NULL is not empty.

/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:121
/var/www/html/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:55
/var/www/html/core/modules/layout_builder/tests/src/FunctionalJavascript/BlockFormMessagesTest.php:65
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:726

Looks like a random fail.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: 3277238-3.patch, failed testing. View results

ravi.shankar’s picture

Status: Needs work » Reviewed & tested by the community

Retested patch #3 on Drupal 9.5.x. It's green so back to RTBC as per comment #4 and #7.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Novice +Needs tests

Given we obviously have no test coverage of this I feel it is worth adding some.

We should be able to use \Drupal\Tests\taxonomy\Functional\TaxonomyImageTest as the basis for a test - we could make it a FunctionalJavascript test and go from there.

joachim’s picture

That test got removed in #3305410: remove TaxonomyImageTest -- someone working on this will need to retrieve the test class from git history (removal commit is 0cf5ca1 on 10.1.x).

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

plopesc’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
1.73 KB
2.41 KB

New patch including tests.

Instead of replicating the scenario in a new FunctionJavascript test, it makes the form validation less restrictive to make the bug visible.

The last submitted patch, 14: 3277238-14-test-only.patch, failed testing. View results

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Confirmed the issue described and the patch #14 solves it.

And the failing test shows the issue is being solved.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 3277238-14.patch, failed testing. View results

Ajeet Tiwari’s picture

Status: Needs work » Needs review
FileSize
807 bytes

Please review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Removing credit for #18
As there was no interdiff
No explanation of change
And reroll removed most of the changes.

Failure in #14 seems random. Retesting but going to mark too.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
@@ -45,4 +45,8 @@ function taxonomy_test_form_taxonomy_term_form_alter(&$form, FormStateInterface
+    $form['name']['widget'][0]['value']['#required'] = FALSE;

I don't think allowing to save an invalid term is best way to test this because it's certainly not a behavior we want to have to support.

Berdir’s picture

I'm unsure, I agree it's somewhat uncommon, but that's already a fairly common scenario that we do "support", the way it works is that you then generate a node title/term name based on some other fields, for example with https://www.drupal.org/project/auto_entitylabel. Typically you hide the field then though (and can do that with nodes through the UI) but that's a minor difference. The same would indeed also happen there.

lauriii’s picture

If the module wanted to ensure that the entity is only saved if it's valid, it should generate the title before saving it. Looks like it's currently done in hook_entity_insert() which means it's done after it has been saved. 🤷‍♂️

lauriii’s picture

Status: Needs work » Needs review
FileSize
2.91 KB
3.59 KB
4.02 KB

Here's a test that uses the steps from the issue summary.

The last submitted patch, 23: 3277238-23-test-only.patch, failed testing. View results

Berdir’s picture

Status: Needs review » Needs work
+++ b/core/modules/taxonomy/tests/src/Functional/TaxonomyImageTest.php
@@ -0,0 +1,95 @@
+      'name[0][value]' => $this->randomMachineName(),
+      'field_test[0][alt]' => $this->randomMachineName(),
+    ];
+    $this->submitForm($edit, 'Save');
+    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $edit['name[0][value]']]);
+    $term = reset($terms);
+    $this->assertSession()->pageTextContains('Created new term ' . $term->getName() . '.');
+  }

loading the term by name to display it's name is a bit unecessary. imho it's sufficient to just check the success message based on $edit directly, we don't need to check that the term really exists?

lauriii’s picture

Status: Needs work » Needs review
FileSize
3.37 KB
1.08 KB

Addressed #25 👍

lauriii’s picture

Now that I'm looking at this again, it looks like \Drupal\taxonomy\TermInterface::getName is supposed to always return string but \Drupal\Core\Entity\EntityInterface::label might return string|\Drupal\Core\StringTranslation\TranslatableMarkup|null. I think the right fix would be for \Drupal\taxonomy\Entity\Term::getName to return empty string when \Drupal\Core\Entity\ContentEntityBase::label returns null.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Makes sense to me and can see the test-only patch fail in 23.

Ran locally too
Exception : Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated
Drupal\taxonomy\TermForm->buildEntity()() (Line: 152)

quietone’s picture

Title: Null is passed to trim() in TermForm::buildEntity() when using an ajax operation without entering a term name » Fix \Drupal\taxonomy\Entity\Term::getName() to conform to the interface
Issue summary: View changes
Status: Reviewed & tested by the community » Needs review
FileSize
965 bytes
3.16 KB

The issue summary is clear and there is a proposed resolution. I read the comments and all questions have been answered. However, I notice that the last comment has changed the solution making the proposed resolution incorrect. And the title isn't quite right any more.

I looked at the patch and noticed a doc block missing, the use statements were not sorted, $module should have an @inheritdoc. I have made a patch for those.

I have updated the proposed resolution and tried for an better title.

Status: Needs review » Needs work

The last submitted patch, 29: 3277238-29.patch, failed testing. View results

Bhanu951’s picture

Status: Needs work » Needs review

Got hit by this issue on 9.5.x . But patch from #29 did not fix it.

Seems Patch from #29 has random failure, rerunning the tests.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems changes in #29 are good/all green

@Bhanu951 can you post the error/steps you're taking to get this issue?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 29: 3277238-29.patch, failed testing. View results

quietone’s picture

Retesting.

lauriii’s picture

Status: Needs work » Reviewed & tested by the community

Moved #29 into MR. Looks like the CI is passing so moving back to RTBC.

xjm’s picture

xjm’s picture

I queued the test-only job. Results:

) Drupal\Tests\block\Kernel\Migrate\d6\MigrateBlockTest::testBlockMigration
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'Schema errors for block.block.aggregator with the following errors: block.block.aggregator:settings.block_count missing schema, block.block.aggregator:settings.feed missing schema, 0 [settings.block_count] 'block_count' is not a supported key., 1 [settings.feed] 'feed' is not a supported key.'
+'Schema errors for block.block.aggregator with the following errors: block.block.aggregator:settings.block_count missing schema, block.block.aggregator:settings.feed missing schema, 0 [settings] 'block_count' is not a supported key., 1 [settings] 'feed' is not a supported key.'
/builds/project/drupal/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:122
/builds/project/drupal/vendor/phpunit/phpunit/src/Framework/Constraint/IsIdentical.php:79
/builds/project/drupal/core/modules/block/tests/src/Kernel/Migrate/d6/MigrateBlockTest.php:313
/builds/project/drupal/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
FAILURES!
Tests: 1, Assertions: 123, Failures: 1.

That is not the failure I expected.

xjm’s picture

https://www.drupal.org/pift-ci-job/2723504 documents the expected test-only result, so the issue with the test-only job is not blocking. I reached out to the DA and committer team about it.

xjm credited fjgarlin.

xjm’s picture

I fixed a few nitpicks. Apparently the weird failure from the test-only job might be due to the MR not being rebased, so also fixed that. (Adding credit for @fjgarlin for identifying that issue.)

Meanwhile, saving issue credits.

xjm’s picture

The rebase did yield the correct test-only result:

There was 1 error:
1) Drupal\Tests\taxonomy\Functional\TaxonomyImageTest::testTaxonomyImageUpload
Exception: Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated
Drupal\taxonomy\TermForm->buildEntity()() (Line: 158)
/builds/project/drupal/core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php:55

  • xjm committed bd38a5a1 on 11.x
    Issue #3277238 by lauriii, xjm, plopesc, quietone, andregp, Berdir,...

  • xjm committed 95715641 on 10.2.x
    Issue #3277238 by lauriii, xjm, plopesc, quietone, andregp, Berdir,...

  • xjm committed 8b02a7cb on 10.1.x
    Issue #3277238 by lauriii, xjm, plopesc, quietone, andregp, Berdir,...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

I could not think of any way that no longer returning an invalid NULL from this would be particularly disruptive, so I committed this to 11.x, 10.2.x, and 10.1.x. Thanks!

xjm’s picture

Version: 11.x-dev » 10.1.x-dev

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.