You are here

public function Unit::formatEventValue in Booking and Availability Management Tools for Drupal 8

Parameters

$event_type:

$value:

Return value

mixed

File

modules/bat_unit/src/Entity/Unit.php, line 287
Contains \Drupal\bat_unit\Entity\Unit.

Class

Unit
Defines the Unit entity.

Namespace

Drupal\bat_unit\Entity

Code

public function formatEventValue($event_type, $value) {
  $bat_type = $this->unit_type_id->entity;
  $field = $bat_type
    ->getEventValueDefaultField($event_type);
  $field_info = FieldStorageConfig::loadByName('bat_unit_type', $field);
  $field_info_instance = FieldConfig::loadByName('bat_unit_type', $bat_type
    ->bundle(), $field);
  $temp_bat_type = clone $bat_type;
  if ($field_info
    ->getType() == 'commerce_price') {
    $currency_code = 'USD';
    if ($default_value = $field_info_instance
      ->get('default_value') && !empty($default_value)) {
      $currency_code = $default_value[0]['currency_code'];
    }
    elseif ($available_currencies = $field_info_instance
      ->getSetting('available_currencies')) {
      $available_currencies = array_filter($available_currencies);
      $currency_code = reset($available_currencies);
    }
    $price = new Price($value, $currency_code);
    $temp_bat_type
      ->set($field, $price);
    $elements = $temp_bat_type->{$field}
      ->view([
      'label' => 'hidden',
    ]);
    $field_view_value = \Drupal::service('renderer')
      ->renderPlain($elements);
  }
  else {
    $temp_bat_type
      ->set($field, $value);
    $elements = $temp_bat_type->{$field}
      ->view([
      'label' => 'hidden',
    ]);
    $field_view_value = \Drupal::service('renderer')
      ->renderPlain($elements);
  }
  return trim(strip_tags($field_view_value
    ->__toString()));
}