View source
<?php
namespace Drupal\Tests\commerce_stock_field\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
trait StockLevelFieldCreationTrait {
protected $fieldName;
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;
}
protected function attachStockLevelField(FieldStorageConfig $field_storage, $bundle, array $field_settings) {
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $bundle,
'label' => $this
->getFieldname(),
'required' => FALSE,
'settings' => $field_settings,
])
->save();
}
protected function configureFormDisplay($widget_id, array $widget_settings, $entity_type, $bundle) {
$entityTypeManager = \Drupal::entityTypeManager();
$form_display = $entityTypeManager
->getStorage('entity_form_display')
->load($entity_type . '.' . $bundle . '.default');
$widget = $form_display
->getComponent($this
->getFieldname());
$widget['type'] = $widget_id;
$widget['settings'] = $widget_settings;
$form_display
->setComponent($this
->getFieldname(), $widget)
->save();
$entityTypeManager
->getStorage('entity_form_display')
->resetCache([
$form_display
->id(),
]);
$entityTypeManager
->clearCachedDefinitions();
}
protected function configureViewDisplay($formatter_id, array $formatter_settings, $entity_type, $bundle) {
$entityTypeManager = \Drupal::entityTypeManager();
$entityTypeManager
->clearCachedDefinitions();
$view_display = $entityTypeManager
->getStorage('entity_view_display');
$product_variation_display = $view_display
->load($entity_type . '.' . $bundle . '.default');
if (!$product_variation_display) {
$product_variation_display = $view_display
->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
$product_variation_display
->setComponent($this->fieldName, [
'type' => 'commerce_stock_level_simple',
]);
$product_variation_display
->save();
$display = $entityTypeManager
->getStorage('entity_view_display');
$view_display = $display
->load($entity_type . '.' . $bundle . '.default');
if (!$view_display) {
$view_display = $display
->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
$formatter = $view_display
->getComponent($this
->getFieldname());
$formatter['type'] = $formatter_id;
$formatter['settings'] = $formatter_settings;
$view_display
->setComponent($this
->getFieldname(), $formatter)
->save();
$entityTypeManager
->getStorage('entity_form_display')
->resetCache([
$view_display
->id(),
]);
}
protected function getFieldname() {
if (!empty($this->fieldName)) {
return $this->fieldName;
}
return mb_strtolower($this
->randomMachineName());
}
}