Issue #3095895 by amateescu, alexpott: FieldTypePluginManager::getPluginClass() should throw an exception when called for a field type that doesn't exist

merge-requests/2419/head
Alex Pott 2019-11-26 09:04:27 +00:00
parent e3873ccd41
commit a6477d5904
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
5 changed files with 19 additions and 10 deletions

View File

@ -2,6 +2,7 @@
namespace Drupal\Core\Field; namespace Drupal\Core\Field;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\Entity\ConfigEntityStorage; use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
@ -22,10 +23,12 @@ abstract class FieldConfigStorageBase extends ConfigEntityStorage {
*/ */
protected function mapFromStorageRecords(array $records) { protected function mapFromStorageRecords(array $records) {
foreach ($records as $id => &$record) { foreach ($records as $id => &$record) {
$class = $this->fieldTypeManager->getPluginClass($record['field_type']); try {
if (empty($class)) { $class = $this->fieldTypeManager->getPluginClass($record['field_type']);
}
catch (PluginNotFoundException $e) {
$config_id = $this->getPrefix() . $id; $config_id = $this->getPrefix() . $id;
throw new \RuntimeException("Unable to determine class for field type '{$record['field_type']}' found in the '$config_id' configuration"); throw new PluginNotFoundException($record['field_type'], "Unable to determine class for field type '{$record['field_type']}' found in the '$config_id' configuration", $e->getCode(), $e);
} }
$record['settings'] = $class::fieldSettingsFromConfigData($record['settings']); $record['settings'] = $class::fieldSettingsFromConfigData($record['settings']);
} }

View File

@ -167,8 +167,7 @@ class FieldTypePluginManager extends DefaultPluginManager implements FieldTypePl
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPluginClass($type) { public function getPluginClass($type) {
$plugin_definition = $this->getDefinition($type, FALSE); return $this->getDefinition($type)['class'];
return $plugin_definition['class'];
} }
} }

View File

@ -112,6 +112,9 @@ interface FieldTypePluginManagerInterface extends PluginManagerInterface, Catego
* *
* @return string * @return string
* Field type plugin class name. * Field type plugin class name.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* Thrown if the field type plugin name is invalid.
*/ */
public function getPluginClass($type); public function getPluginClass($type);

View File

@ -2,6 +2,7 @@
namespace Drupal\field; namespace Drupal\field;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Uuid\UuidInterface; use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface; use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage; use Drupal\Core\Config\Entity\ConfigEntityStorage;
@ -158,10 +159,12 @@ class FieldStorageConfigStorage extends ConfigEntityStorage {
*/ */
protected function mapFromStorageRecords(array $records) { protected function mapFromStorageRecords(array $records) {
foreach ($records as $id => &$record) { foreach ($records as $id => &$record) {
$class = $this->fieldTypeManager->getPluginClass($record['type']); try {
if (empty($class)) { $class = $this->fieldTypeManager->getPluginClass($record['type']);
}
catch (PluginNotFoundException $e) {
$config_id = $this->getPrefix() . $id; $config_id = $this->getPrefix() . $id;
throw new \RuntimeException("Unable to determine class for field type '{$record['type']}' found in the '$config_id' configuration"); throw new PluginNotFoundException($record['type'], "Unable to determine class for field type '{$record['type']}' found in the '$config_id' configuration", $e->getCode(), $e);
} }
$record['settings'] = $class::storageSettingsFromConfigData($record['settings']); $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
} }

View File

@ -2,6 +2,7 @@
namespace Drupal\KernelTests\Core\Field; namespace Drupal\KernelTests\Core\Field;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\entity_test\Entity\EntityTestMulRev; use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
@ -60,7 +61,7 @@ class FieldMissingTypeTest extends EntityKernelTestBase {
* @see \Drupal\field\FieldStorageConfigStorage::mapFromStorageRecords() * @see \Drupal\field\FieldStorageConfigStorage::mapFromStorageRecords()
*/ */
public function testFieldStorageMissingType() { public function testFieldStorageMissingType() {
$this->expectException(\RuntimeException::class); $this->expectException(PluginNotFoundException::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->expectExceptionMessage("Unable to determine class for field type 'foo_field_storage' found in the 'field.storage.entity_test_mulrev.{$this->fieldName}' configuration");
$entity = EntityTestMulRev::create([ $entity = EntityTestMulRev::create([
'name' => $this->randomString(), 'name' => $this->randomString(),
@ -80,7 +81,7 @@ class FieldMissingTypeTest extends EntityKernelTestBase {
* @see \Drupal\field\FieldConfigStorageBase::mapFromStorageRecords() * @see \Drupal\field\FieldConfigStorageBase::mapFromStorageRecords()
*/ */
public function testFieldMissingType() { public function testFieldMissingType() {
$this->expectException(\RuntimeException::class); $this->expectException(PluginNotFoundException::class);
$this->expectExceptionMessage("Unable to determine class for field type 'foo_field' found in the 'field.field.entity_test_mulrev.entity_test_mulrev.{$this->fieldName}' configuration"); $this->expectExceptionMessage("Unable to determine class for field type 'foo_field' found in the 'field.field.entity_test_mulrev.entity_test_mulrev.{$this->fieldName}' configuration");
$entity = EntityTestMulRev::create([ $entity = EntityTestMulRev::create([
'name' => $this->randomString(), 'name' => $this->randomString(),