function commerce_price_form_views_ui_config_item_form_alter in Commerce Core 8.2
Implements hook_form_FORM_ID_alter().
Removes the "Calculated price" formatter from views field options, if it is not applicable. Since the formatter is product variation specific, this prevents it from accidentally being used on other entity types.
@todo Remove when https://www.drupal.org/node/2991660 gets fixed.
File
- modules/
price/ commerce_price.module, line 55 - Defines the Currency entity and the Price field.
Code
function commerce_price_form_views_ui_config_item_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\views\Plugin\views\field\EntityField $handler */
$handler = $form_state
->get('handler');
if ($handler instanceof EntityField && !empty($handler->definition['entity_type'])) {
$entity_type_id = $handler->definition['entity_type'];
$field_name = $handler->definition['field_name'];
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
$field_manager = \Drupal::service('entity_field.manager');
$field_definitions = $field_manager
->getFieldStorageDefinitions($entity_type_id);
$field_definition = $field_definitions[$field_name] ?? NULL;
if (!$field_definition || $field_definition
->getType() != 'commerce_price') {
return;
}
// Remove the formatter from configurable fields, and non-applicable ones.
if (!$field_definition instanceof BaseFieldDefinition || !PriceCalculatedFormatter::isApplicable($field_definition)) {
unset($form['options']['type']['#options']['commerce_price_calculated']);
}
}
}