You are here

protected function StockLevelFieldCreationTrait::createStockLevelField in Commerce Stock 8

Creates a new stock level field.

Parameters

string $entity_type: The entity type.

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

string $widget_id: The id of the widget which should be used.

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 $widget_settings: A list of widget settings that will be added to the widget defaults.

string $formatter_id: The id of the formatter.

array $formatter_settings: A list of formatter settings that will be added to the formatter defaults.

Return value

\Drupal\Core\Entity\EntityInterface|\Drupal\field\Entity\FieldStorageConfig The field configuration.

Throws

\Drupal\Core\Entity\EntityStorageException

2 calls to StockLevelFieldCreationTrait::createStockLevelField()
StockLevelFieldTestBase::setup in modules/field/tests/src/Functional/StockLevelFieldTestBase.php
Setting up the test.
StockLevelTest::setUp in modules/field/tests/src/Kernel/StockLevelTest.php

File

modules/field/tests/src/Kernel/StockLevelFieldCreationTrait.php, line 46

Class

StockLevelFieldCreationTrait
Provides methods to attach and configure a stock level field.

Namespace

Drupal\Tests\commerce_stock_field\Kernel

Code

protected function createStockLevelField($entity_type, $bundle, $widget_id, array $storage_settings = [], array $field_settings = [], array $widget_settings = [], $formatter_id = 'commerce_stock_level_simple', array $formatter_settings = []) {
  $field_name = $this
    ->getFieldname();
  $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
  if (!$field_storage) {
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'type' => 'commerce_stock_level',
      'settings' => $storage_settings,
    ]);
  }
  $field_storage
    ->save();
  $this
    ->attachStockLevelField($field_storage, $bundle, $field_settings);
  $this
    ->configureFormDisplay($widget_id, $widget_settings, $entity_type, $bundle);
  $this
    ->configureViewDisplay($formatter_id, $formatter_settings, $entity_type, $bundle);
  return $field_storage;
}