createImageField example


  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Create an image field 'foo' having the image style 'medium' as widget     // preview and as formatter.     $this->createImageField('foo', 'page', [][]['preview_image_style' => 'medium']['image_style' => 'medium']);
  }

  /** * Tests image style deletion. */
  public function testDelete() {
    $this->drupalGet('admin/config/media/image-styles/manage/medium/delete');
    // Checks that the 'replacement' select element is displayed.     $this->assertSession()->fieldExists('replacement');
    // Checks that UI messages are correct.     $this->assertSession()->pageTextContains("If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted. If no replacement style is selected, the dependent configurations might need manual reconfiguration.");
    

    $field_settings['default_image'] = [
      'uuid' => $default_images['field']->uuid(),
      'alt' => '',
      'title' => '',
      'width' => 0,
      'height' => 0,
    ];
    $widget_settings = [
      'preview_image_style' => 'medium',
    ];
    $field = $this->createImageField($field_name, 'article', $storage_settings$field_settings$widget_settings);

    // The field default image id should be 2.     $this->assertEquals($default_images['field']->uuid()$field->getSetting('default_image')['uuid']);

    // Also test \Drupal\field\Entity\FieldConfig::getSettings().     $this->assertEquals($default_images['field']->uuid()$field->getSettings()['default_image']['uuid']);

    $field_storage = $field->getFieldStorageDefinition();

    // The field storage default image id should be 1.     $this->assertEquals($default_images['field_storage']->uuid()$field_storage->getSetting('default_image')['uuid']);

    

  public function testWidgetElement() {
    // Check for image widget in add/node/article page     $field_name = strtolower($this->randomMachineName());
    $min_resolution = 50;
    $max_resolution = 100;
    $field_settings = [
      'max_resolution' => $max_resolution . 'x' . $max_resolution,
      'min_resolution' => $min_resolution . 'x' . $min_resolution,
      'alt_field' => 0,
    ];
    $this->createImageField($field_name, 'article', []$field_settings[][], 'Image test on [site:name]');
    $this->drupalGet('node/add/article');
    // Verify that the image field widget is found on add/node page.     $this->assertSession()->elementExists('xpath', '//div[contains(@class, "field--widget-image-image")]');
    // Verify that the image field widget limits accepted files.     $this->assertSession()->elementExists('xpath', '//input[contains(@accept, "image/*")]');
    $this->assertSession()->pageTextNotContains('Image test on [site:name]');

    // Check for allowed image file extensions - default.     $this->assertSession()->pageTextContains('Allowed types: png gif jpg jpeg.');

    // Try adding to the field config an unsupported extension, should not

  protected $defaultTheme = 'stark';

  /** * Tests image validity. */
  public function testValid() {
    $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');
    
->save();
    $this->createTextField(
      'node', 'painting',
      'colors', 'Colors',
      FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
    );
    $this->createTextField(
      'node', 'painting',
      'shapes', 'Shapes',
      FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
    );
    $this->createImageField('photo', 'painting');
  }

  /** * Creates painting nodes. */
  protected function savePaintings($paintings) {
    foreach ($paintings as $painting) {
      Node::create(array_merge([
        'type' => 'painting',
      ]$painting))->save();
    }
  }
'node',
      'article',
      'field_tags',
      'Tags',
      'taxonomy_term',
      'default',
      ['target_bundles' => ['tags']],
      FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
    );
    $this->createTextField('node', 'article', 'body', 'Body');

    $this->createImageField('field_image', 'article');

    $this->user = User::create([
      'name' => 'user1',
      'mail' => 'user@localhost',
    ]);
    $this->user2 = User::create([
      'name' => 'user2',
      'mail' => 'user2@localhost',
    ]);

    $this->user->save();
    
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests the validation message is displayed only once for ajax uploads. */
  public function testAJAXValidationMessage() {
    $field_name = strtolower($this->randomMachineName());
    $this->createImageField($field_name, 'article', ['cardinality' => -1]);

    $this->drupalGet('node/add/article');
    /** @var \Drupal\file\FileInterface[] $text_files */
    $text_files = $this->drupalGetTestFiles('text');
    $text_file = reset($text_files);

    $field = $this->getSession()->getPage()->findField('files[' . $field_name . '_0][]');
    $field->attachFile($this->container->get('file_system')->realpath($text_file->uri));
    $this->assertSession()->waitForElement('css', '.messages--error');

    // Verify that Ajax validation messages are displayed only once.
$config->setLanguageAlterable(TRUE);
    $config->save();

    $content_translation_manager = $this->container->get('content_translation.manager');
    $content_translation_manager->setEnabled('node', 'article', TRUE);
    $content_translation_manager->setBundleTranslationSettings('node', 'article', [
      'untranslatable_fields_hide' => FALSE,
    ]);

    $this->rebuildContainer();

    $this->createImageField('field_image_field', 'article');
  }

  /** * Tests that the source language is properly set when changing. */
  public function testLanguageChange() {
    // Create a node in English.     $this->drupalGet('node/add/article');
    $edit = [
      'title[0][value]' => 'english_title',
    ];
    

    $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);
  }

  /** * Sample 'default value' callback. */
'Tags',
        'taxonomy_term',
        'default',
        [
          'target_bundles' => [
            'tags' => 'tags',
          ],
          'auto_create' => TRUE,
        ],
        FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
      );
      $this->createImageField('field_image', 'article');
      $this->createImageField('field_heroless', 'article');
    }

    FieldStorageConfig::create([
      'field_name' => 'field_link',
      'entity_type' => 'node',
      'type' => 'link',
      'settings' => [],
      'cardinality' => 1,
    ])->save();

    

  protected function doTestResponsiveImageFieldFormatters($scheme$empty_styles = FALSE) {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $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);

    


  /** * Tests image formatters on node display. */
  public function _testImageFieldFormatters($scheme) {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $field_name = strtolower($this->randomMachineName());
    $field_settings = ['alt_field_required' => 0];
    $instance = $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme]$field_settings);

    // Go to manage display page.     $this->drupalGet("admin/structure/types/manage/article/display");

    // Test for existence of link to image styles configuration.     $this->submitForm([], "{$field_name}_settings_edit");
    $this->assertSession()->linkByHrefExists(Url::fromRoute('entity.image_style.collection')->toString(), 0, 'Link to image styles configuration is found');

    // Remove 'administer image styles' permission from testing admin user.     $admin_user_roles = $this->adminUser->getRoles(TRUE);
    user_role_change_permissions(reset($admin_user_roles)['administer image styles' => FALSE]);

    

  public function testWidgetElementMultipleUploads(): void {
    $image_factory = \Drupal::service('image.factory');
    $file_system = \Drupal::service('file_system');
    $web_driver = $this->getSession()->getDriver();

    $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
    $field_name = 'images';
    $storage_settings = ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED];
    $field_settings = ['alt_field_required' => 0];
    $this->createImageField($field_name, 'article', $storage_settings$field_settings);
    $this->drupalLogin($this->drupalCreateUser(['access content', 'create article content']));
    $this->drupalGet('node/add/article');
    $this->assertSession()->fieldExists('title[0][value]')->setValue('Test');

    $images = $this->getTestFiles('image');
    $images = array_slice($images, 0, 5);

    $paths = [];
    foreach ($images as $image) {
      $paths[] = $file_system->realpath($image->uri);
    }

    

  public function testStyleReplacement() {
    // Create a new style.     $style_name = strtolower($this->randomMachineName(10));
    $style_label = $this->randomString();
    $style = ImageStyle::create(['name' => $style_name, 'label' => $style_label]);
    $style->save();
    $style_path = 'admin/config/media/image-styles/manage/';

    // Create an image field that uses the new style.     $field_name = strtolower($this->randomMachineName(10));
    $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());
    
// This test expects unused managed files to be marked as a temporary file.     $this->config('file.settings')->set('make_unused_managed_files_temporary', TRUE)->save();

    // Create the "Basic page" node type.     // @todo Remove the disabling of new revision creation in     // https://www.drupal.org/node/1239558.     $this->drupalCreateContentType(['type' => 'basicpage', 'name' => 'Basic page', 'new_revision' => FALSE]);

    // Create an image field on the "Basic page" node type.     $this->fieldName = strtolower($this->randomMachineName());
    $this->createImageField($this->fieldName, 'basicpage', []['title_field' => 1]);

    // Create and log in user.     $permissions = [
      'access administration pages',
      'administer content translation',
      'administer content types',
      'administer languages',
      'administer node fields',
      'create content translations',
      'create basicpage content',
      'edit any basicpage content',
      
Home | Imprint | This part of the site doesn't use cookies.