You are here

protected function FileFieldCreationTrait::createFileField in Insert 8

Creates a new file field.

Parameters

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

string $type_name: The node type that this field will be added to.

array [$storage_settings=array()]: A list of field storage settings that will be added to the defaults.

array [$field_settings=array()]: A list of instance settings that will be added to the instance defaults.

array [$widget_settings=array()]: Widget settings to be added to the widget defaults.

array [$formatter_settings=array()]: Formatter settings to be added to the formatter defaults.

string [$description='']: A description for the field. Defaults to ''.

Return value

\Drupal\Core\Entity\EntityInterface

8 calls to FileFieldCreationTrait::createFileField()
InsertFileWidgetTest::testAbsoluteUrlSetting in tests/src/FunctionalJavaScript/InsertFileWidgetTest.php
InsertFileWidgetTest::testAdditionalCssClassesSetting in tests/src/FunctionalJavaScript/InsertFileWidgetTest.php
InsertFileWidgetTest::testDefaultWidgetElement in tests/src/FunctionalJavaScript/InsertFileWidgetTest.php
InsertFileWidgetTest::testDescriptionField in tests/src/FunctionalJavaScript/InsertFileWidgetTest.php
InsertFileWidgetTest::testFocus in tests/src/FunctionalJavaScript/InsertFileWidgetTest.php

... See full list

File

tests/src/FunctionalJavaScript/FileFieldCreationTrait.php, line 36

Class

FileFieldCreationTrait
Provides a helper method for creating File fields having assigned the Insert File widget.

Namespace

Drupal\Tests\insert\FunctionalJavascript

Code

protected function createFileField($name, $type_name, $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') {
  FieldStorageConfig::create([
    'field_name' => $name,
    'entity_type' => 'node',
    'type' => 'file',
    'settings' => $storage_settings,
    'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
  ])
    ->save();
  $field_config = FieldConfig::create([
    'field_name' => $name,
    'label' => $name,
    'entity_type' => 'node',
    'bundle' => $type_name,
    'required' => !empty($field_settings['required']),
    'settings' => $field_settings,
    'description' => $description,
  ]);
  $field_config
    ->save();

  /** @var EntityDisplayInterface $entity */
  $entity = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('node.' . $type_name . '.default');
  $entity
    ->setComponent($name, [
    'type' => 'insert_file',
    'settings' => $widget_settings,
  ])
    ->save();

  /** @var EntityDisplayInterface $entity */
  $entity = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->load('node.' . $type_name . '.default');
  $entity
    ->setComponent($name, [
    'type' => 'file_default',
    'settings' => $formatter_settings,
  ])
    ->save();
  return $field_config;
}