function FieldTypePluginManagerTest::testCreateInstance

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php \Drupal\Tests\field\Kernel\FieldTypePluginManagerTest::testCreateInstance()
  2. 10 core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php \Drupal\Tests\field\Kernel\FieldTypePluginManagerTest::testCreateInstance()
  3. 11.x core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php \Drupal\Tests\field\Kernel\FieldTypePluginManagerTest::testCreateInstance()

Tests creation of field item instances.

File

core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php, line 35

Class

FieldTypePluginManagerTest
Tests the field type manager.

Namespace

Drupal\Tests\field\Kernel

Code

public function testCreateInstance() {
    
    /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    foreach ([
        'test_field',
        'shape',
        'hidden_test_field',
    ] as $type) {
        $definition = $field_type_manager->getDefinition($type);
        $class = $definition['class'];
        $field_name = 'field_' . $type;
        $field_definition = BaseFieldDefinition::create($type);
        $configuration = [
            'field_definition' => $field_definition,
            'name' => $field_name,
            'parent' => NULL,
        ];
        $instance = $field_type_manager->createInstance($type, $configuration);
        $this->assertInstanceOf($class, $instance);
        $this->assertEqual($field_name, $instance->getName(), new FormattableMarkup('Instance name is @name', [
            '@name' => $field_name,
        ]));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.