You are here

protected function EntityClassFormatterTest::createField in Entity Class Formatter 8

Creates a field and sets the formatter.

Parameters

string $field_type: The type of field.

string $display_attr: The display attribute name.

Return value

\Drupal\field\Entity\FieldConfig The newly created field.

3 calls to EntityClassFormatterTest::createField()
EntityClassFormatterTest::testAttrValue in tests/src/Functional/EntityClassFormatterTest.php
EntityClassFormatterTest::testEntityFieldClass in tests/src/Functional/EntityClassFormatterTest.php
EntityClassFormatterTest::testStringFieldClass in tests/src/Functional/EntityClassFormatterTest.php

File

tests/src/Functional/EntityClassFormatterTest.php, line 142

Class

EntityClassFormatterTest
@group entity_class_formatter

Namespace

Drupal\Tests\entity_class_formatter\Functional

Code

protected function createField($field_type, $display_attr = '') {
  $entity_type = 'node';
  $bundle = 'page';
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $field_storage = FieldStorageConfig::create([
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'type' => $field_type,
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_storage
    ->save();
  $field_config = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $bundle,
  ]);
  $field_config
    ->save();
  $display = EntityViewDisplay::create([
    'targetEntityType' => $entity_type,
    'bundle' => $bundle,
    'mode' => 'full',
    'status' => TRUE,
  ]);
  $display
    ->setComponent($field_name, [
    'type' => 'entity_class_formatter',
    'settings' => [
      'prefix' => self::CLASS_PREFIX,
      'suffix' => self::CLASS_SUFFIX,
      'attr' => $display_attr,
    ],
  ]);
  $display
    ->save();
  return $field_config;
}