function LinkFieldWidgetTest::entitySetUp

Initializes a link field on the entity_test entity type.

Parameters

int $linkType: One of the LinkItemInterface type constants.

\Drupal\link\LinkTitleVisibility $titleVisibilitySetting: The title subfield's visibility.

int $cardinality: (optional) The field's cardinality, defaults to 1.

string|null $defaultValue: (optional) A default value for the uri subfield, defaults to null.

Return value

string The generated field name.

File

core/modules/link/tests/src/Kernel/LinkFieldWidgetTest.php, line 168

Class

LinkFieldWidgetTest
Tests link field widgets.

Namespace

Drupal\Tests\link\Kernel

Code

protected function entitySetUp(int $linkType, LinkTitleVisibility $titleVisibilitySetting, int $cardinality = 1, ?string $defaultValue = NULL) : string {
  $fieldName = $this->randomMachineName();
  $fieldStorage = FieldStorageConfig::create([
    'field_name' => $fieldName,
    'entity_type' => 'entity_test',
    'type' => 'link',
    'cardinality' => $cardinality,
  ]);
  $fieldStorage->save();
  $fieldConfig = FieldConfig::create([
    'field_storage' => $fieldStorage,
    'label' => 'Link',
    'bundle' => 'entity_test',
    'settings' => [
      'title' => $titleVisibilitySetting->value,
      'link_type' => $linkType,
    ],
    'description' => $this->fieldDescription,
  ]);
  if ($defaultValue !== NULL) {
    $fieldConfig->setDefaultValue([
      'uri' => $defaultValue,
    ]);
  }
  $fieldConfig->save();
  $this->container
    ->get('entity_display.repository')
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($fieldName, [
    'type' => 'link_default',
  ])
    ->save();
  return $fieldName;
}

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