You are here

public function FileFieldPathsTestBase::createFileField in File (Field) Paths 8

Creates a new file field.

Parameters

string $name: The name of the new field (all lowercase), exclude the "field_" prefix.

string $entity_type: The entity type.

string $bundle: The bundle that this field will be added to.

array $storage_settings: A list of field storage settings that will be added to the defaults.

array $field_settings: A list of instance settings that will be added to the instance defaults.

array $third_party_settings: Third party settings.

array $widget_settings: A list of widget settings that will be added to the widget defaults.

Throws

\Behat\Mink\Exception\ResponseTextException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

Overrides FileFieldCreationTrait::createFileField

16 calls to FileFieldPathsTestBase::createFileField()
FileFieldPathsGeneralTest::testAddField in tests/src/Functional/FileFieldPathsGeneralTest.php
Test that the File (Field) Paths UI works as expected.
FileFieldPathsGeneralTest::testFileUsage in tests/src/Functional/FileFieldPathsGeneralTest.php
Test a file usage of a basic file upload with File (Field) Paths.
FileFieldPathsGeneralTest::testLongPath in tests/src/Functional/FileFieldPathsGeneralTest.php
Test File (Field) Paths with a very long path.
FileFieldPathsGeneralTest::testNoFile in tests/src/Functional/FileFieldPathsGeneralTest.php
Test File (Field) Paths works as normal when no file uploaded.
FileFieldPathsGeneralTest::testProgrammaticAttach in tests/src/Functional/FileFieldPathsGeneralTest.php
Test File (Field) Paths on a programmatically added file.

... See full list

File

tests/src/Functional/FileFieldPathsTestBase.php, line 76

Class

FileFieldPathsTestBase
Base class for File (Field) Paths tests.

Namespace

Drupal\Tests\filefield_paths\Functional

Code

public function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $third_party_settings = [], $widget_settings = []) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $field_storage = $entity_type_manager
    ->getStorage('field_storage_config')
    ->create([
    'entity_type' => $entity_type,
    'field_name' => $name,
    'type' => 'file',
    'settings' => $storage_settings,
    'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
  ]);
  $field_storage
    ->save();
  $field = [
    'field_name' => $name,
    'label' => $name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'required' => !empty($field_settings['required']),
    'settings' => $field_settings,
    'third_party_settings' => $third_party_settings,
  ];
  $entity_type_manager
    ->getStorage('field_config')
    ->create($field)
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $edr */
  $edr = \Drupal::service('entity_display.repository');
  $edr
    ->getFormDisplay($entity_type, $bundle, 'default')
    ->setComponent($name, [
    'type' => 'file_generic',
    'settings' => $widget_settings,
  ])
    ->save();

  // Assign display settings.
  $edr
    ->getViewDisplay($entity_type, $bundle, 'default')
    ->setComponent($name, [
    'label' => 'hidden',
    'type' => 'file_default',
  ])
    ->save();
  $this
    ->drupalGet("admin/structure/types/manage/{$this->contentType}/fields/node.{$this->contentType}.{$name}");
  $this
    ->submitForm([], 'Save settings');
  $this
    ->assertSession()
    ->pageTextContains("Saved {$name} configuration");

  // Clear field cache in order to avoid stale cache values.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
}