/** * {@inheritdoc} */ public static function value(array &$element, &$input, FormStateInterface $form_state) { if (isset($input['filefield_reference']['autocomplete']) && strlen($input['filefield_reference']['autocomplete']) > 0 && $input['filefield_reference']['autocomplete'] != FILEFIELD_SOURCE_REFERENCE_HINT_TEXT) { $matches = array(); if (preg_match('/\\[fid:(\\d+)\\]/', $input['filefield_reference']['autocomplete'], $matches)) { $fid = $matches[1]; if ($file = file_load($fid)) { // Remove file size restrictions, since the file already exists on // disk. if (isset($element['#upload_validators']['file_validate_size'])) { unset($element['#upload_validators']['file_validate_size']); } // Check that the user has access to this file through // hook_download(). if (!$file->access('download')) { $form_state->setError($element, t('You do not have permission to use the selected file.')); } elseif (filefield_sources_element_validate($element, (object) $file, $form_state)) { if (!in_array($file->id(), $input['fids'])) { $input['fids'][] = $file->id(); } } } else { $form_state->setError($element, t('The referenced file could not be used because the file does not exist in the database.')); } } // No matter what happens, clear the value from the autocomplete. $input['filefield_reference']['autocomplete'] = ''; } }
/** * {@inheritdoc} */ public static function value(array &$element, &$input, FormStateInterface $form_state) { if (isset($input['filefield_imce']['file_path']) && $input['filefield_imce']['file_path'] != '') { $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']); $field_settings = $instance->getSettings(); $scheme = $field_settings['uri_scheme']; $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme); $file_directory_prefix = $scheme == 'private' ? 'system/files' : $wrapper->getDirectoryPath(); $uri = preg_replace('/^' . preg_quote(base_path() . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $input['filefield_imce']['file_path']); // Resolve the file path to an FID. $fid = db_select('file_managed', 'f')->condition('uri', rawurldecode($uri))->fields('f', array('fid'))->execute()->fetchField(); if ($fid) { $file = file_load($fid); if (filefield_sources_element_validate($element, $file)) { if (!in_array($file->id(), $input['fids'])) { $input['fids'][] = $file->id(); } } } else { $form_state->setError($element, t('The selected file could not be used because the file does not exist in the database.')); } // No matter what happens, clear the value from the file path field. $input['filefield_imce']['file_path'] = ''; } }