function LayoutBuilderDefaultValuesTest::addImageFields

Same name and namespace in other branches
  1. 10 core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderDefaultValuesTest::addImageFields()
  2. 11.x core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderDefaultValuesTest::addImageFields()

Helper function to add image fields.

1 call to LayoutBuilderDefaultValuesTest::addImageFields()
LayoutBuilderDefaultValuesTest::setUp in core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php

File

core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php, line 242

Class

LayoutBuilderDefaultValuesTest
Tests rendering default field values in Layout Builder.

Namespace

Drupal\Tests\layout_builder\Functional

Code

protected function addImageFields() {
    // Create files to use as the default images.
    $files = $this->drupalGetTestFiles('image');
    $images = [];
    for ($i = 1; $i <= 3; $i++) {
        $filename = "test-file-{$i}";
        $desired_filepath = 'public://' . $filename;
        \Drupal::service('file_system')->copy($files[0]->uri, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
        $file = File::create([
            'uri' => $desired_filepath,
            'filename' => $filename,
            'name' => $filename,
        ]);
        $file->save();
        $images[] = $file;
    }
    $field_name = 'field_image_with_default';
    $storage_settings['default_image'] = [
        'uuid' => $images[0]->uuid(),
        'alt' => 'My default alt text',
        'title' => '',
        'width' => 0,
        'height' => 0,
    ];
    $field_settings['default_image'] = [
        'uuid' => $images[0]->uuid(),
        'alt' => 'My default alt text',
        'title' => '',
        'width' => 0,
        'height' => 0,
    ];
    $widget_settings = [
        'preview_image_style' => 'medium',
    ];
    $this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
    $field_name = 'field_image_no_default';
    $storage_settings = [];
    $field_settings = [];
    $widget_settings = [
        'preview_image_style' => 'medium',
    ];
    $this->createImageField($field_name, 'test_node_type', $storage_settings, $field_settings, $widget_settings);
}

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