The \Drupal\Core\Field\PluginSettingsBase::getThirdPartySettings method returns null if the module does not exist instead of empty array.

The description of the method in the ThirdPartySettingsInterface states:

/**
   * Gets all third-party settings of a given module.
   *
   * @param string $module
   *   The module providing the third-party settings.
   *
   * @return array
   *   An array of key-value pairs.
   */
  public function getThirdPartySettings($module);

Changing the implementation is better here because

  • By default this code return an empty array if no module is passed in and there is third party settings.
  • The correlating code in ConfigEntityBase::getThirdPartySettings($module) returns an empty array if the module has no settings.

So returning an empty array is more consistent than returning a NULL. Also if there is data for the module in any of the above cases it will be an array.

CommentFileSizeAuthor
#21 2632314-21.patch1.9 KBalexpott
#21 2632314-21.test-only.patch1.26 KBalexpott
#7 2632314-7.patch655 bytesamateescu
pluginbase-thirdpartysettings.patch467 bytesAnonymous (not verified)

Comments

Anonymous’s picture

ivanjaros created an issue. See original summary.

Anonymous’s picture

Status: Active » Needs review
amateescu’s picture

Status: Needs review » Reviewed & tested by the community

\Drupal\Core\Config\Entity\ConfigEntityBase::getThirdPartySettings() (correctly) returns an array as well, nice find :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, pluginbase-thirdpartysettings.patch, failed testing.

amateescu’s picture

Status: Needs work » Reviewed & tested by the community

The patch still applies fine locally on 8.0.x, must have been a bot fluke.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, pluginbase-thirdpartysettings.patch, failed testing.

amateescu’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new655 bytes

I don't get it :/ Let's try a reroll..

swentel’s picture

Hmm, this might break implementations in contrib/custom because it might rely on the fact that it runs NULL ? Tricky.
OTOH, this would make it consistent with getThirdPartySettings in ConfigEntityBase

amateescu’s picture

Well, the interface says that the return type is an array, so if anyone relies on the current NULL return value I'd say it is a bug in their code..

swentel’s picture

Hmm yeah, I also think there won't be that many implementations yet using third party settings on fields anyway, so +1 from me.

catch’s picture

There's potential breakage both directions:

- if someone relies on the interface return value, and we change it, we'll break that
- if someone relies on the current implementation, and we change it, we'll break that.

Both are extremely unlikely though, so I think we should just change to whichever makes most sense. Also the quicker we change it the less likely that either consuming or implementing code will be doing either.

swentel’s picture

Status: Reviewed & tested by the community » Needs review

Well given how #2625138: EntityRepository::loadEntityByUuid() implementation does not match EntityRepositoryInterface::loadEntityByUuid() @return documentation turned out, our safe bet is probably to change the docs, that should normally break 'less'.

catch’s picture

Hmm nothing broke there, just realised it wasn't as straightforward as I thought after committing.

swentel’s picture

Oh wait, looking at the last patch there, it actually proposes to fix the implementation, so, I guess the patch we have here follows that pattern. However, it's silent now over there for a month and a half :)

eric_a’s picture

Version: 8.0.x-dev » 8.1.x-dev

I guess this one is very, very similar to the fix from #2625138: EntityRepository::loadEntityByUuid() implementation does not match EntityRepositoryInterface::loadEntityByUuid() @return documentation that got into 8.1.x and 8.2.x. Moving to 8.1.x.

eric_a’s picture

Status: Needs review » Reviewed & tested by the community

So this issue respects the contract and improves consistency by fixing the return value for an empty collection from NULL to empty array and the other issue made the implementation respect the contract and improved consistency by changing the return value for trying to load a non-existing object from FALSE to NULL. Potential breakage is extremely unlikely and the sooner fixed the better.
Patch in #7 passes tests for all branches.

alexpott’s picture

Issue summary: View changes

Updated issue summary to make a rationale for the approach. I guess there is a question about whether or not we should test this.

alexpott’s picture

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

Let's take the opportunity to add a unit test of PluginSettingsBase::getThirdPartySettings()

xjm’s picture

Title: PluginSettingsBase::getThirdPartySettings returns null instead of array » PluginSettingsBase::getThirdPartySettings() returns null instead of array
alexpott’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.26 KB
new1.9 KB

Here's a test.

The last submitted patch, 21: 2632314-21.test-only.patch, failed testing.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Looks perfect for me. I hope people never rely on it to check whether a value exists or not.

  • catch committed c6f9c5a on 8.2.x
    Issue #2632314 by alexpott, amateescu, ivanjaros: PluginSettingsBase::...

  • catch committed a020b15 on 8.1.x
    Issue #2632314 by alexpott, amateescu, ivanjaros: PluginSettingsBase::...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!

Status: Fixed » Closed (fixed)

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