public static function YamlFormManagedFileBase::accessFileDownload in YAML Form 8
Control access to form submission private file downloads.
Parameters
string $uri: The URI of the file.
Return value
mixed Returns NULL is the file is not attached to a form submission. Returns -1 if the user does not have permission to access a form. Returns an associative array of headers.
See also
File
- src/
Plugin/ YamlFormElement/ YamlFormManagedFileBase.php, line 612
Class
- YamlFormManagedFileBase
- Provides a base class form 'managed_file' elements.
Namespace
Drupal\yamlform\Plugin\YamlFormElementCode
public static function accessFileDownload($uri) {
$files = \Drupal::entityTypeManager()
->getStorage('file')
->loadByProperties([
'uri' => $uri,
]);
if (empty($files)) {
return NULL;
}
$file = reset($files);
if (empty($file)) {
return NULL;
}
/** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
$file_usage = \Drupal::service('file.usage');
$usage = $file_usage
->listUsage($file);
foreach ($usage as $module => $entity_types) {
// Check for YAML Form module.
if ($module != 'yamlform') {
continue;
}
foreach ($entity_types as $entity_type => $counts) {
$entity_ids = array_keys($counts);
// Check for form submission entity type.
if ($entity_type != 'yamlform_submission' || empty($entity_ids)) {
continue;
}
// Get form submission.
$yamlform_submission = YamlFormSubmission::load(reset($entity_ids));
if (!$yamlform_submission) {
continue;
}
// Check form submission view access.
if (!$yamlform_submission
->access('view')) {
return -1;
}
// Return file content headers.
return file_get_content_headers($file);
}
}
return NULL;
}