public function ImageFieldTestBase::uploadNodeImage in Drupal 8
Same name in this branch
- 8 core/modules/image/src/Tests/ImageFieldTestBase.php \Drupal\image\Tests\ImageFieldTestBase::uploadNodeImage()
- 8 core/modules/image/tests/src/Functional/ImageFieldTestBase.php \Drupal\Tests\image\Functional\ImageFieldTestBase::uploadNodeImage()
Upload an image to a node.
Parameters
$image: A file object representing the image to upload.
$field_name: Name of the image field the image should be attached to.
$type: The type of node to create.
$alt: The alt text for the image. Use if the field settings require alt text.
File
- core/
modules/ image/ src/ Tests/ ImageFieldTestBase.php, line 91
Class
- ImageFieldTestBase
- This class provides methods specifically for testing Image's field handling.
Namespace
Drupal\image\TestsCode
public function uploadNodeImage($image, $field_name, $type, $alt = '') {
$edit = [
'title[0][value]' => $this
->randomMachineName(),
];
$edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')
->realpath($image->uri);
$this
->drupalPostForm('node/add/' . $type, $edit, t('Save'));
if ($alt) {
// Add alt text.
$this
->drupalPostForm(NULL, [
$field_name . '[0][alt]' => $alt,
], t('Save'));
}
// Retrieve ID of the newly created node from the current URL.
$matches = [];
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
return isset($matches[1]) ? $matches[1] : FALSE;
}