getSettings(); $scheme = $field_settings['uri_scheme']; $imce_paths = explode(':', $input['filefield_imce']['imce_paths']); $uris = []; foreach ($imce_paths as $imce_path) { //$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 . '://', $imce_path); $uri = rawurldecode($scheme . '://' . $imce_path); $uris[] = $uri; } // Resolve the file path to an FID. $fids = db_select('file_managed', 'f') ->condition('uri', $uris, 'IN') ->fields('f', array('fid')) ->execute() ->fetchCol(); if ($fids) { $files = file_load_multiple($fids); foreach ($files as $file) { if (filefield_sources_element_validate($element, $file, $form_state)) { 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']['imce_paths'] = ''; } } /** * {@inheritdoc} */ public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) { $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']); $element['filefield_imce'] = array( '#weight' => 100.5, '#theme' => 'filefield_sources_element', '#source_id' => 'imce', // Required for proper theming. '#filefield_source' => TRUE, '#description' => filefield_sources_element_validation_help($element['#upload_validators']), ); $imce_url = \Drupal::url('filefield_sources.imce', array( 'entity_type' => $element['#entity_type'], 'bundle_name' => $element['#bundle'], 'field_name' => $element['#field_name'], ), array( 'query' => array( 'sendto' => 'imceFileField.sendto', 'fieldId' => $element['#attributes']['data-drupal-selector'] . '-filefield-imce', ), )); $element['filefield_imce']['browse'] = array( '#type' => 'markup', '#markup' => '' . t('No file selected') . ' (' . t('browse') . ')', ); $element['#attached']['library'][] = 'imce/drupal.imce.filefield'; // Set the pre-renderer to conditionally disable the elements. $element['#pre_render'][] = array(get_called_class(), 'preRenderWidget'); // Path input $element['filefield_imce']['imce_paths'] = array( '#type' => 'hidden', // Reset value to prevent consistent errors '#value' => '', ); $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_imce']['imce_button'] = array( '#name' => implode('_', $element['#parents']) . '_imce_select', '#type' => 'submit', '#value' => t('Select'), '#attributes' => ['class' => ['js-hide']], '#validate' => [], '#submit' => ['filefield_sources_field_submit'], '#limit_validation_errors' => [$element['#parents']], '#ajax' => $ajax_settings, ); return $element; } /** * Theme the output of the imce element. */ public static function element($variables) { $element = $variables['element']; $output = drupal_render_children($element); return '