Problem/Motivation

\Drupal\Core\Field\FieldTypePluginManager::getPluginClass() calls getDefinition() with $exception_on_invalid = FALSE for.. historical reasons. It was initially introduced like that in #2293773: Field allowed values use dots in key names - not allowed in config probably because field widgets and formatters were also using $exception_on_invalid = FALSE, but the difference is that those two plugin types can fall back to a default widget/formatter, while a field type has no fallback.

Proposed resolution

Make \Drupal\Core\Field\FieldTypePluginManager::getPluginClass() throw the standard plugin not found exception.

This also fixes PHP 7.4 compatibility problems because the return statement of getPluginClass() will never be reached with an empty $plugin_definition variable.

Remaining tasks

Review.

User interface changes

Nope.

API changes

Nope.

Data model changes

Nope.

Release notes snippet

Nope.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amateescu created an issue. See original summary.

amateescu’s picture

Status: Active » Needs review
Issue tags: +Workflow Initiative
FileSize
4.25 KB

This should do it.

alexpott’s picture

+++ b/core/tests/Drupal/KernelTests/Core/Field/FieldMissingTypeTest.php
@@ -60,8 +61,8 @@ protected function setUp() {
   public function testFieldStorageMissingType() {
-    $this->expectException(\RuntimeException::class);
-    $this->expectExceptionMessage("Unable to determine class for field type 'foo_field_storage' found in the 'field.storage.entity_test_mulrev.{$this->fieldName}' configuration");
+    $this->expectException(PluginNotFoundException::class);
+    $this->expectExceptionMessage('The "foo_field_storage" plugin does not exist.');

The exception is way way less helpful though. Imo we should preserve the helpful exception somehow. Catching and re-throwing come to mind.

amateescu’s picture

@alexpott I considered that when writing the patch, but the current exception only works for configurable fields and not for base fields, so I think that's also confusing rather than helpful. In turn, with the current patch, we get the standard plugin exception that people are used to see when dealing with any kind of plugin..

alexpott’s picture

Well we're mapping from storage records so I'm not sure when you'd see the current exception for base fields? I.e. I think it is okay to catch and rethrow in :: mapFromStorageRecords() to improve the exception message.

hchonov’s picture

amateescu’s picture

@alexpott, ok, let's do that :)

alexpott’s picture

+1 to updating the exception types to the more meaningful PluginNotFoundException.

+++ b/core/lib/Drupal/Core/Field/FieldTypePluginManager.php
@@ -167,7 +167,7 @@ public function getPreconfiguredOptions($field_type) {
    * {@inheritdoc}
    */
   public function getPluginClass($type) {
-    $plugin_definition = $this->getDefinition($type, FALSE);
+    $plugin_definition = $this->getDefinition($type);
     return $plugin_definition['class'];
   }

Let's update the interface docs with an @throws...
For example:

   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   Thrown if $type is invalid.

I'd also be tempted to make this a one-liner. Ie. return $this->getDefinition($type)['class'];

amateescu’s picture

Sure, make sense!

alexpott’s picture

Thanks @amateescu - this looks good to me. Will leave for someone else to rtbc so I can commit.

hchonov’s picture

Status: Needs review » Reviewed & tested by the community

There is simply nothing to add.

alexpott’s picture

Version: 8.9.x-dev » 8.8.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed and pushed a6477d5904 to 9.0.x and d5e903d1bd to 8.9.x. Thanks!

  • alexpott committed a6477d5 on 9.0.x
    Issue #3095895 by amateescu, alexpott: FieldTypePluginManager::...

  • alexpott committed d5e903d on 8.9.x
    Issue #3095895 by amateescu, alexpott: FieldTypePluginManager::...
alexpott’s picture

Status: Patch (to be ported) » Fixed

As this is part of the PHP7.4 critical cherry-picked this back to 8.8.x. @catch +1'd the backport.

  • alexpott committed cb5cc6b on 8.8.x
    Issue #3095895 by amateescu, alexpott: FieldTypePluginManager::...

Status: Fixed » Closed (fixed)

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