Commit cb5cc6bc authored by alexpott's avatar alexpott

Issue #3095895 by amateescu, alexpott:...

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

(cherry picked from commit a6477d59)
parent f2854ac5
......@@ -2,6 +2,7 @@
namespace Drupal\Core\Field;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityInterface;
......@@ -22,10 +23,12 @@ abstract class FieldConfigStorageBase extends ConfigEntityStorage {
*/
protected function mapFromStorageRecords(array $records) {
foreach ($records as $id => &$record) {
$class = $this->fieldTypeManager->getPluginClass($record['field_type']);
if (empty($class)) {
try {
$class = $this->fieldTypeManager->getPluginClass($record['field_type']);
}
catch (PluginNotFoundException $e) {
$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']);
}
......
......@@ -167,8 +167,7 @@ public function getPreconfiguredOptions($field_type) {
* {@inheritdoc}
*/
public function getPluginClass($type) {
$plugin_definition = $this->getDefinition($type, FALSE);
return $plugin_definition['class'];
return $this->getDefinition($type)['class'];
}
}
......@@ -112,6 +112,9 @@ public function getPreconfiguredOptions($field_type);
*
* @return string
* Field type plugin class name.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* Thrown if the field type plugin name is invalid.
*/
public function getPluginClass($type);
......
......@@ -2,6 +2,7 @@
namespace Drupal\field;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
......@@ -165,10 +166,12 @@ public function loadByProperties(array $conditions = []) {
*/
protected function mapFromStorageRecords(array $records) {
foreach ($records as $id => &$record) {
$class = $this->fieldTypeManager->getPluginClass($record['type']);
if (empty($class)) {
try {
$class = $this->fieldTypeManager->getPluginClass($record['type']);
}
catch (PluginNotFoundException $e) {
$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']);
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\KernelTests\Core\Field;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
......@@ -60,7 +61,7 @@ protected function setUp() {
* @see \Drupal\field\FieldStorageConfigStorage::mapFromStorageRecords()
*/
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");
$entity = EntityTestMulRev::create([
'name' => $this->randomString(),
......@@ -80,7 +81,7 @@ public function testFieldStorageMissingType() {
* @see \Drupal\field\FieldConfigStorageBase::mapFromStorageRecords()
*/
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");
$entity = EntityTestMulRev::create([
'name' => $this->randomString(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment