protected function EntityconnectTestBase::addContentEntityReferenceField in Entity connect 8.2
Add an entity reference field to the target content type.
Parameters
string $field_name: Name of the reference field.
string $field_bundle: Bundle on which to add the reference field.
array|null $target_bundles: The target bundle(s) of the reference field.
Return value
\Drupal\field\Entity\FieldConfig The created/existing field config object.
Throws
\Drupal\Core\Entity\EntityStorageException
2 calls to EntityconnectTestBase::addContentEntityReferenceField()
- EntityconnectTestBase::setUp in tests/
src/ Functional/ EntityconnectTestBase.php - EntityconnectTestBase::updateEntityReferenceFieldTargets in tests/
src/ Functional/ EntityconnectTestBase.php - Update the target bundles of the test entity reference field.
File
- tests/
src/ Functional/ EntityconnectTestBase.php, line 107
Class
- EntityconnectTestBase
- EntityConnect Test Base.
Namespace
Drupal\Tests\entityconnect\FunctionalCode
protected function addContentEntityReferenceField($field_name = 'entity_reference', $field_bundle = NULL, $target_bundles = NULL) {
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
$field = FieldConfig::loadByName('node', $field_bundle ?? $this->testContentType
->id(), $field_name);
if (empty($field)) {
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => 1,
]);
$field_storage
->save();
}
$field = FieldConfig::create([
'field_storage' => $field_storage,
'label' => $field_name,
'bundle' => $field_bundle ?? $this->testContentType
->id(),
'settings' => [
'handler_settings' => [
'target_bundles' => $target_bundles ?? [
$this->testContentType
->id(),
],
],
],
]);
$field
->save();
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('node', $field_bundle ?? $this->testContentType
->id(), 'default');
$form_display
->setComponent($field_name, [
'type' => 'options_select',
])
->save();
// Show on default display and teaser.
\Drupal::service('entity_display.repository')
->getViewDisplay('node', $field_bundle ?? $this->testContentType
->id())
->setComponent($field_name, [
'type' => 'entity_reference_label',
])
->save();
}
return $field;
}