You are here

function webform_query_entity_reference_alter in Webform 6.x

Same name and namespace in other branches
  1. 8.5 includes/webform.query.inc \webform_query_entity_reference_alter()

Implements hook_query_TAG_alter().

File

includes/webform.query.inc, line 39
Webform module query hooks.

Code

function webform_query_entity_reference_alter(AlterableInterface $query) {

  /** @var \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection $handler */
  $handler = $query
    ->getMetaData('entity_reference_selection_handler');

  // Get webform settings used to limit and randomize results.
  // @see \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::getTestValues
  // @see \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::setOptions
  // @see \Drupal\webform\Element\WebformEntityTrait::setOptions
  $configuration = $handler
    ->getConfiguration() + [
    '_webform_settings' => [],
  ];
  $settings = $configuration['_webform_settings'];
  if (!empty($settings['random'])) {
    $query
      ->orderRandom();
  }
  if (!empty($settings['limit'])) {
    $query
      ->range(0, $settings['limit']);
  }
}