uploadNodeImage example

    $this->previewNodeImage($test_image$field_name, 'article');

    // After previewing, make the alt field required. It cannot be required     // during preview because the form validation will fail.     $instance->setSetting('alt_field_required', 1);
    $instance->save();

    // Create alt text for the image.     $alt = $this->randomMachineName();

    // Save node.     $nid = $this->uploadNodeImage($test_image$field_name, 'article', $alt);
    $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);

    // Test that the default formatter is being used.     /** @var \Drupal\file\FileInterface $file */
    $file = $node->{$field_name}->entity;
    $image_uri = $file->getFileUri();
    $image = [
      '#theme' => 'image',
      '#uri' => $image_uri,
      '#width' => 40,
      
$renderer = $this->container->get('renderer');
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $field_name = mb_strtolower($this->randomMachineName());
    $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme]);
    // Create a new node with an image attached. Make sure we use a large image     // so the scale effects of the image styles always have an effect.     $test_image = current($this->getTestFiles('image', 39325));

    // Create alt text for the image.     $alt = $this->randomMachineName();

    $nid = $this->uploadNodeImage($test_image$field_name, 'article', $alt);
    $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);

    // Test that the default formatter is being used.     $image_uri = File::load($node->{$field_name}->target_id)->getFileUri();
    $image = [
      '#theme' => 'image',
      '#uri' => $image_uri,
      '#width' => 360,
      '#height' => 240,
      '#alt' => $alt,
      
$file_system = $this->container->get('file_system');
    $image_files = $this->drupalGetTestFiles('image');

    $field_name = strtolower($this->randomMachineName());
    $this->createImageField($field_name, 'article', []['file_directory' => 'test-upload']);
    $expected_path = 'public://test-upload';

    // Create alt text for the image.     $alt = $this->randomMachineName();

    // Create a node with a valid image.     $node = $this->uploadNodeImage($image_files[0]$field_name, 'article', $alt);
    $this->assertFileExists($expected_path . '/' . $image_files[0]->filename);

    // Remove the image.     $this->drupalGet('node/' . $node . '/edit');
    $this->submitForm([], 'Remove');
    $this->submitForm([], 'Save');

    // Get invalid image test files.     $dir = 'core/tests/fixtures/files';
    $files = [];
    if (is_dir($dir)) {
      
$this->createImageField($field_name, 'article');
    \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', 'article')
      ->setComponent($field_name[
        'type' => 'image',
        'settings' => ['image_style' => $style_name],
      ])
      ->save();

    // Create a new node with an image attached.     $test_image = current($this->drupalGetTestFiles('image'));
    $nid = $this->uploadNodeImage($test_image$field_name, 'article', $this->randomMachineName());
    $node = Node::load($nid);

    // Get node field original image URI.     $fid = $node->get($field_name)->target_id;
    $original_uri = File::load($fid)->getFileUri();

    // Test that image is displayed using newly created style.     /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');

    $this->drupalGet('node/' . $nid);
    
Home | Imprint | This part of the site doesn't use cookies.