You are here

public function BundleFieldItemList::setValue in CiviCRM Entity 8.3

This sets the bundle property if Drupal sets a value to `bundle`, which means we have to transliterate the options and convert a machine name to the option key.

Overrides ComputedItemListTrait::setValue

File

src/Plugin/Field/BundleFieldItemList.php, line 44

Class

BundleFieldItemList
Computed field item list for the bundle property.

Namespace

Drupal\civicrm_entity\Plugin\Field

Code

public function setValue($values, $notify = TRUE) {
  $entity = $this
    ->getEntity();
  assert($entity instanceof CivicrmEntity);
  $civicrm_bundle_property = $entity
    ->getEntityType()
    ->get('civicrm_bundle_property');
  $civicrm_entity_name = $entity
    ->getEntityType()
    ->get('civicrm_entity');

  /** @var \Drupal\civicrm_entity\CiviCrmApiInterface $civicrm_api */
  $civicrm_api = \Drupal::service('civicrm_entity.api');
  $options = $civicrm_api
    ->getOptions($civicrm_entity_name, $civicrm_bundle_property);
  $transliteration = \Drupal::transliteration();
  $options = array_map(static function ($value) use ($transliteration) {
    return SupportedEntities::optionToMachineName($value, $transliteration);
  }, $options);
  $options = array_flip($options);
  if (!is_array($values)) {
    $entity
      ->get($civicrm_bundle_property)
      ->setValue($options[$values]);
  }
  parent::setValue($values, $notify);
  $this->valueComputed = TRUE;
}