You are here

function price_form_views_ui_config_item_form_alter in Price 2.0.x

Implements hook_form_FORM_ID_alter().

Removes the "Calculated price" formatter from views field options, if it is not applicable. Since the formatter is entity specific, this prevents it from accidentally being used on other entity types.

@todo Remove when https://www.drupal.org/node/2991660 gets fixed.

File

./price.module, line 71
Defines the functionalities for the 'price' module.

Code

function 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() != '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']['price_calculated']);
    }
  }
}