public function YamlFormManagedFileBase::getTestValue in YAML Form 8
Get test value for an element.
Parameters
array $element: An element.
\Drupal\yamlform\YamlFormInterface $yamlform: A form.
Return value
mixed A test value for an element.
Overrides YamlFormElementBase::getTestValue
File
- src/
Plugin/ YamlFormElement/ YamlFormManagedFileBase.php, line 374
Class
- YamlFormManagedFileBase
- Provides a base class form 'managed_file' elements.
Namespace
Drupal\yamlform\Plugin\YamlFormElementCode
public function getTestValue(array $element, YamlFormInterface $yamlform) {
if ($this
->isDisabled()) {
return NULL;
}
$file_extensions = explode(' ', $this
->getFileExtensions($element));
$file_extension = $file_extensions[array_rand($file_extensions)];
$upload_location = $this
->getUploadLocation($element, $yamlform);
$file_destination = $upload_location . '/' . $element['#yamlform_key'] . '.' . $file_extension;
// Look for an existing temp files that have not been uploaded.
$fids = \Drupal::entityQuery('file')
->condition('status', 0)
->condition('uid', \Drupal::currentUser()
->id())
->condition('uri', $upload_location . '/' . $element['#yamlform_key'] . '.%', 'LIKE')
->execute();
if ($fids) {
return reset($fids);
}
// Copy sample file or generate a new temp file that can be uploaded.
$sample_file = drupal_get_path('module', 'yamlform') . '/tests/files/sample.' . $file_extension;
if (file_exists($sample_file)) {
$file_uri = file_unmanaged_copy($sample_file, $file_destination);
}
else {
$file_uri = file_unmanaged_save_data('{empty}', $file_destination);
}
$file = File::create([
'uri' => $file_uri,
'uid' => \Drupal::currentUser()
->id(),
]);
$file
->save();
$fid = $file
->id();
return $this
->hasMultipleValues($element) ? [
$fid,
] : $fid;
}