Validates and upcasts request attributes.
protected function validateAndUpcastRequestAttributes(Request $request) {
// Load the entity.
if (!is_object($entity = $request->attributes
->get('entity'))) {
$entity_id = $entity;
$entity_type = $request->attributes
->get('entity_type');
if (!$entity_type || !entity_get_info($entity_type)) {
throw new NotFoundHttpException();
}
$entity = entity_load($entity_type, $entity_id);
if (!$entity) {
throw new NotFoundHttpException();
}
$request->attributes
->set('entity', $entity);
}
// Validate the field name and language.
$field_name = $request->attributes
->get('field_name');
if (!$field_name || !field_info_instance($entity
->entityType(), $field_name, $entity
->bundle())) {
throw new NotFoundHttpException();
}
$langcode = $request->attributes
->get('langcode');
if (!$langcode || field_valid_language($langcode) !== $langcode) {
throw new NotFoundHttpException();
}
}