You are here

protected function AmpTestBase::createEntityReferenceField in Accelerated Mobile Pages (AMP) 8.3

Adds a reference field to the test content type.

1 call to AmpTestBase::createEntityReferenceField()
AmpFormatterMediaImageTest::setUp in tests/src/Functional/AmpFormatterMediaImageTest.php

File

tests/src/Functional/AmpTestBase.php, line 301

Class

AmpTestBase
Base AMP testing setup.

Namespace

Drupal\Tests\amp\Functional

Code

protected function createEntityReferenceField($field_name, $target_entity_type, array $target_bundles, $formatter_type = 'entity_reference_label', array $formatter_settings = []) {
  FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'translatable' => FALSE,
    'settings' => [
      'target_type' => $target_entity_type,
    ],
  ])
    ->save();
  FieldConfig::create([
    'field_name' => $field_name,
    'bundle' => $this->contentType,
    'entity_type' => 'node',
    'settings' => [
      'handler' => 'default',
      'handler_settings' => [
        'target_bundles' => $target_bundles,
      ],
    ],
  ])
    ->save();
  $this->displayRepository
    ->getFormDisplay('node', $this->contentType)
    ->setComponent($field_name, [
    'type' => 'entity_reference_autocomplete',
  ])
    ->save();
  $this->displayRepository
    ->getViewDisplay('node', $this->contentType)
    ->setComponent($field_name, [
    'type' => $formatter_type,
    'settings' => $formatter_settings,
  ])
    ->save();
}