function StringFormatterTest::testLinkToEntityEditForm

Same name and namespace in other branches
  1. main core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php \Drupal\Tests\field\Kernel\KernelString\StringFormatterTest::testLinkToEntityEditForm()

Test linking to an entity's edit form.

File

core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php, line 258

Class

StringFormatterTest
Tests the creation of text fields.

Namespace

Drupal\Tests\field\Kernel\KernelString

Code

public function testLinkToEntityEditForm() : void {
  $this->installEntitySchema('entity_test');
  $field_name = 'test_field_name';
  $entity_type = $bundle = 'entity_test';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'string',
  ]);
  $field_storage->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $entity_type,
    'label' => $this->randomMachineName(),
  ]);
  $instance->save();
  $display = \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $bundle)
    ->setComponent($field_name, [
    'type' => 'string',
    'settings' => [
      'link_to_entity' => TRUE,
      'link_rel' => 'edit-form',
    ],
    'region' => 'content',
  ]);
  $display->save();
  $value = $this->randomMachineName();
  $entity = EntityTest::create([
    'name' => 'test',
    $field_name => $value,
  ]);
  $entity->save();
  $this->assertStringContainsString((string) $entity->toLink($value, 'edit-form')
    ->toString(), $this->renderEntityFields($entity, $display));
}

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