You are here

public function RouteParametersWebformSourceEntity::getSourceEntity in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformSourceEntity/RouteParametersWebformSourceEntity.php \Drupal\webform\Plugin\WebformSourceEntity\RouteParametersWebformSourceEntity::getSourceEntity()

Detect and return a source entity from current context.

Parameters

string[] $ignored_types: Entity types that may not be used as a source entity.

Return value

\Drupal\Core\Entity\EntityInterface|null Source entity or NULL when no source entity is found.

Overrides WebformSourceEntityInterface::getSourceEntity

File

src/Plugin/WebformSourceEntity/RouteParametersWebformSourceEntity.php, line 63

Class

RouteParametersWebformSourceEntity
Detect source entity by examining route parameters.

Namespace

Drupal\webform\Plugin\WebformSourceEntity

Code

public function getSourceEntity(array $ignored_types) {

  // Use current account when viewing a user's submissions.
  // @see \Drupal\webform\WebformSubmissionListBuilder
  if ($this->routeMatch
    ->getRouteName() === 'entity.webform_submission.user') {
    return NULL;
  }

  // Get the most specific source entity available in the current route's
  // parameters.
  $parameters = $this->routeMatch
    ->getParameters()
    ->all();
  $parameters = array_reverse($parameters);
  if (!empty($ignored_types)) {
    $parameters = array_diff_key($parameters, array_flip($ignored_types));
  }
  foreach ($parameters as $value) {
    if ($value instanceof EntityInterface) {
      return $value;
    }
  }
  return NULL;
}