function FileMediaFormatterTestBase::createMediaField

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php \Drupal\Tests\file\Functional\Formatter\FileMediaFormatterTestBase::createMediaField()
  2. 10 core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php \Drupal\Tests\file\Functional\Formatter\FileMediaFormatterTestBase::createMediaField()
  3. 11.x core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php \Drupal\Tests\file\Functional\Formatter\FileMediaFormatterTestBase::createMediaField()

Creates a file field and set's the correct formatter.

Parameters

string $formatter: The formatter ID.

string $file_extensions: The file extensions of the new field.

array $formatter_settings: Settings for the formatter.

Return value

\Drupal\field\Entity\FieldConfig Newly created file field.

3 calls to FileMediaFormatterTestBase::createMediaField()
FileAudioFormatterTest::testRender in core/modules/file/tests/src/Functional/Formatter/FileAudioFormatterTest.php
@covers ::viewElements
FileVideoFormatterTest::testAttributes in core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php
Tests that the attributes added to the formatter are applied on render.
FileVideoFormatterTest::testRender in core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php
@covers ::viewElements

File

core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php, line 47

Class

FileMediaFormatterTestBase
Provides methods specifically for testing File module's media formatter's.

Namespace

Drupal\Tests\file\Functional\Formatter

Code

protected function createMediaField($formatter, $file_extensions, array $formatter_settings = []) {
    $entity_type = $bundle = 'entity_test';
    $field_name = mb_strtolower($this->randomMachineName());
    FieldStorageConfig::create([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'type' => 'file',
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ])->save();
    $field_config = FieldConfig::create([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'bundle' => $bundle,
        'settings' => [
            'file_extensions' => trim($file_extensions),
        ],
    ]);
    $field_config->save();
    $this->container
        ->get('entity_display.repository')
        ->getViewDisplay('entity_test', 'entity_test', 'full')
        ->setComponent($field_name, [
        'type' => $formatter,
        'settings' => $formatter_settings,
    ])
        ->save();
    return $field_config;
}

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