protected function StockLevelFieldCreationTrait::configureViewDisplay in Commerce Stock 8
Set, update and configure the widget for the stock level field.
Parameters
string $formatter_id: The id of the formatter.
array $formatter_settings: A list of formatter settings that will be added to the widget defaults.
string $entity_type: The entity type.
string $bundle: The bundle that this field will be added to.
1 call to StockLevelFieldCreationTrait::configureViewDisplay()
- StockLevelFieldCreationTrait::createStockLevelField in modules/
field/ tests/ src/ Kernel/ StockLevelFieldCreationTrait.php - Creates a new stock level field.
File
- modules/
field/ tests/ src/ Kernel/ StockLevelFieldCreationTrait.php, line 144
Class
- StockLevelFieldCreationTrait
- Provides methods to attach and configure a stock level field.
Namespace
Drupal\Tests\commerce_stock_field\KernelCode
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(),
]);
}