public static function Attach::process in FileField Sources 8
Process callback for file field source plugin.
Parameters
array $element: An associative array containing the properties and children of the generic input element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
Return value
array The processed element.
Overrides FilefieldSourceInterface::process
File
- src/
Plugin/ FilefieldSource/ Attach.php, line 86
Class
- Attach
- A FileField source plugin to allow use of files within a server directory.
Namespace
Drupal\filefield_sources\Plugin\FilefieldSourceCode
public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
$settings = $element['#filefield_sources_settings']['source_attach'];
$field_name = $element['#field_name'];
$instance = \Drupal::entityTypeManager()
->getStorage('field_config')
->load($element['#entity_type'] . '.' . $element['#bundle'] . '.' . $field_name);
$element['filefield_attach'] = [
'#weight' => 100.5,
'#theme' => 'filefield_sources_element',
'#source_id' => 'attach',
// Required for proper theming.
'#filefield_source' => TRUE,
];
$path = static::getDirectory($settings);
$options = static::getAttachOptions($path, $instance
->getSetting('file_extensions'));
// If we have built this element before, append the list of options that we
// had previously. This allows files to be deleted after copying them and
// still be considered a valid option during the validation and submit.
$triggering_element = $form_state
->getTriggeringElement();
$property = [
'filefield_sources',
$field_name,
'attach_options',
];
if (!isset($triggering_element) && $form_state
->has($property)) {
$attach_options = $form_state
->get($property);
$options = $options + $attach_options;
}
else {
$form_state
->set([
'filefield_sources',
$field_name,
'attach_options',
], $options);
}
$description = t('This method may be used to attach files that exceed the file size limit. Files may be attached from the %directory directory on the server, usually uploaded through FTP.', [
'%directory' => realpath($path),
]);
// Error messages.
if ($options === FALSE || empty($settings['path'])) {
$attach_message = t('A file attach directory could not be located.');
$attach_description = t('Please check your settings for the %field field.', [
'%field' => $instance
->getLabel(),
]);
}
elseif (!count($options)) {
$attach_message = t('There currently are no files to attach.');
$attach_description = $description;
}
if (isset($attach_message)) {
$element['filefield_attach']['attach_message'] = [
'#markup' => $attach_message,
];
$element['filefield_attach']['#description'] = $attach_description;
}
else {
$validators = $element['#upload_validators'];
if (isset($validators['file_validate_size'])) {
unset($validators['file_validate_size']);
}
$description .= '<br />' . filefield_sources_element_validation_help($validators);
$element['filefield_attach']['filename'] = [
'#type' => 'select',
'#options' => $options,
];
$element['filefield_attach']['#description'] = $description;
}
$class = '\\Drupal\\file\\Element\\ManagedFile';
$ajax_settings = [
'callback' => [
$class,
'uploadAjaxCallback',
],
'options' => [
'query' => [
'element_parents' => implode('/', $element['#array_parents']),
],
],
'wrapper' => $element['upload_button']['#ajax']['wrapper'],
'effect' => 'fade',
];
$element['filefield_attach']['attach'] = [
'#name' => implode('_', $element['#parents']) . '_attach',
'#type' => 'submit',
'#value' => t('Attach'),
'#validate' => [],
'#submit' => [
'filefield_sources_field_submit',
],
'#limit_validation_errors' => [
$element['#parents'],
],
'#ajax' => $ajax_settings,
];
return $element;
}