You are here

public function WebformSubmissionStorage::getSourceEntitiesAsOptions in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionStorage.php \Drupal\webform\WebformSubmissionStorage::getSourceEntitiesAsOptions()

Get source entities as options for a specified webform.

Parameters

\Drupal\webform\WebformInterface $webform: A webform.

Return value

array An associative array contain source entities as options for a specified webform.

Overrides WebformSubmissionStorageInterface::getSourceEntitiesAsOptions

File

src/WebformSubmissionStorage.php, line 361

Class

WebformSubmissionStorage
Defines the webform submission storage.

Namespace

Drupal\webform

Code

public function getSourceEntitiesAsOptions(WebformInterface $webform) {
  $options = [];
  $source_entities = $this
    ->getSourceEntities($webform);
  foreach ($source_entities as $entity_type => $entity_ids) {
    $optgroup = (string) $this->entityTypeManager
      ->getDefinition($entity_type)
      ->getCollectionLabel();
    $entities = $this->entityTypeManager
      ->getStorage($entity_type)
      ->loadMultiple($entity_ids);
    foreach ($entities as $entity_id => $entity) {
      if ($entity instanceof TranslatableInterface && $entity
        ->hasTranslation($this->languageManager
        ->getCurrentLanguage()
        ->getId())) {
        $entity = $entity
          ->getTranslation($this->languageManager
          ->getCurrentLanguage()
          ->getId());
      }
      $option_value = "{$entity_type}:{$entity_id}";
      $option_text = $entity
        ->label();
      $options[$optgroup][$option_value] = $option_text;
    }
    if (isset($options[$optgroup])) {
      asort($options[$optgroup]);
    }
  }
  return count($options) === 1 ? reset($options) : $options;
}