You are here

public function WebformSubmissionStorage::getSourceEntityAsOptions in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/WebformSubmissionStorage.php \Drupal\webform\WebformSubmissionStorage::getSourceEntityAsOptions()

Get webform submission source entities as options.

Parameters

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

string $entity_type: A source entity type.

Return value

array An array of source entities as options that the webform has been submitted from.

Overrides WebformSubmissionStorageInterface::getSourceEntityAsOptions

File

src/WebformSubmissionStorage.php, line 518

Class

WebformSubmissionStorage
Defines the webform submission storage.

Namespace

Drupal\webform

Code

public function getSourceEntityAsOptions(WebformInterface $webform, $entity_type) {
  $entity_ids = Database::getConnection()
    ->select('webform_submission', 's')
    ->fields('s', [
    'entity_id',
  ])
    ->condition('s.webform_id', $webform
    ->id())
    ->condition('s.entity_type', $entity_type)
    ->execute()
    ->fetchCol();

  // Limit the number of source entities loaded to 100.
  if (empty($entity_ids) || count($entity_ids) > 100) {
    return [];
  }
  $entities = $this->entityTypeManager
    ->getStorage($entity_type)
    ->loadMultiple($entity_ids);
  $options = [];
  foreach ($entities as $entity_id => $entity) {
    $options[$entity_id] = $entity
      ->label();
  }
  asort($options);
  return $options;
}