public function TestImageFieldTrait::createImageFieldEntity in FillPDF 5.0.x
Same name and namespace in other branches
- 8.4 tests/src/Traits/TestImageFieldTrait.php \Drupal\Tests\fillpdf\Traits\TestImageFieldTrait::createImageFieldEntity()
Create an entity with an image.
This is our own version of ImageFieldTestBase::uploadNodeImage() only that it supports creating fieldable entities other than nodes.
Parameters
\Drupal\file\Entity\File $image: A file object representing the image to be added.
string $field_name: Name of the image field the image should be attached to.
string $entity_type: The entity type.
string $bundle: The bundle.
string $alt: (optional) Alt text for the image. Use if required by field settings.
Return value
int Entity ID of the created entity.
See also
\Drupal\Tests\image\Functional\ImageFieldTestBase::uploadNodeImage()
1 call to TestImageFieldTrait::createImageFieldEntity()
- PdfPopulationTest::testImageStamping in tests/
src/ Functional/ PdfPopulationTest.php - Tests Core image stamping.
File
- tests/
src/ Traits/ TestImageFieldTrait.php, line 37
Class
- TestImageFieldTrait
- Provides helper methods for creating Image fields.
Namespace
Drupal\Tests\fillpdf\TraitsCode
public function createImageFieldEntity(File $image, $field_name, $entity_type, $bundle, $alt = '') {
if (empty($image
->id())) {
$image
->save();
}
$type_manager = \Drupal::entityTypeManager();
$definition = $type_manager
->getDefinition($entity_type);
$values = [];
if ($bundle_key = $definition
->getKey('bundle')) {
$values[$bundle_key] = $bundle;
}
$label_key = $entity_type == 'user' ? 'name' : $definition
->getKey('label');
if ($label_key) {
$values[$label_key] = $this
->randomMachineName();
}
$values[$field_name] = [
'target_id' => $image
->id(),
'alt' => $alt,
];
$entity = $type_manager
->getStorage($entity_type)
->create($values);
$entity
->save();
return $entity
->id();
}