5 * Contains \Drupal\filefield_sources\Plugin\FilefieldSource\Imce.
8 namespace Drupal\filefield_sources\Plugin\FilefieldSource;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\filefield_sources\FilefieldSourceInterface;
12 use Symfony\Component\Routing\Route;
13 use Drupal\Core\Field\WidgetInterface;
16 * A FileField source plugin to allow referencing of files from IMCE.
20 * name = @Translation("IMCE file browser"),
21 * label = @Translation("File browser"),
22 * description = @Translation("Select a file to use from a file browser."),
26 class Imce implements FilefieldSourceInterface {
31 public static function value(array &$element, &$input, FormStateInterface $form_state) {
32 if (isset($input['filefield_imce']['imce_paths']) && $input['filefield_imce']['imce_paths'] != '') {
33 $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
34 $field_settings = $instance->getSettings();
35 $scheme = $field_settings['uri_scheme'];
36 $imce_paths = explode(':', $input['filefield_imce']['imce_paths']);
39 foreach ($imce_paths as $imce_path) {
40 //$wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
41 //$file_directory_prefix = $scheme == 'private' ? 'system/files' : $wrapper->getDirectoryPath();
42 //$uri = preg_replace('/^' . preg_quote(base_path() . $file_directory_prefix . '/', '/') . '/', $scheme . '://', $imce_path);
43 $uri = rawurldecode($scheme . '://' . $imce_path);
47 // Resolve the file path to an FID.
48 $fids = db_select('file_managed', 'f')
49 ->condition('uri', $uris, 'IN')
50 ->fields('f', array('fid'))
54 $files = file_load_multiple($fids);
55 foreach ($files as $file) {
56 if (filefield_sources_element_validate($element, $file, $form_state)) {
57 if (!in_array($file->id(), $input['fids'])) {
58 $input['fids'][] = $file->id();
64 $form_state->setError($element, t('The selected file could not be used because the file does not exist in the database.'));
66 // No matter what happens, clear the value from the file path field.
67 $input['filefield_imce']['imce_paths'] = '';
74 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
75 $instance = entity_load('field_config', $element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
77 $element['filefield_imce'] = array(
79 '#theme' => 'filefield_sources_element',
80 '#source_id' => 'imce',
81 // Required for proper theming.
82 '#filefield_source' => TRUE,
83 '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
86 $imce_url = \Drupal::url('filefield_sources.imce', array(
87 'entity_type' => $element['#entity_type'],
88 'bundle_name' => $element['#bundle'],
89 'field_name' => $element['#field_name'],
93 'sendto' => 'imceFileField.sendto',
94 'fieldId' => $element['#attributes']['data-drupal-selector'] . '-filefield-imce',
97 $element['filefield_imce']['browse'] = array(
99 '#markup' => '<span>' . t('No file selected') . '</span> (<a class="filefield-sources-imce-browse" href="' . $imce_url . '">' . t('browse') . '</a>)',
102 $element['#attached']['library'][] = 'imce/drupal.imce.filefield';
103 // Set the pre-renderer to conditionally disable the elements.
104 $element['#pre_render'][] = array(get_called_class(), 'preRenderWidget');
107 $element['filefield_imce']['imce_paths'] = array(
109 // Reset value to prevent consistent errors
113 $class = '\Drupal\file\Element\ManagedFile';
115 'callback' => [$class, 'uploadAjaxCallback'],
118 'element_parents' => implode('/', $element['#array_parents']),
121 'wrapper' => $element['upload_button']['#ajax']['wrapper'],
125 $element['filefield_imce']['imce_button'] = array(
126 '#name' => implode('_', $element['#parents']) . '_imce_select',
128 '#value' => t('Select'),
129 '#attributes' => ['class' => ['js-hide']],
131 '#submit' => ['filefield_sources_field_submit'],
132 '#limit_validation_errors' => [$element['#parents']],
133 '#ajax' => $ajax_settings,
140 * Theme the output of the imce element.
142 public static function element($variables) {
143 $element = $variables['element'];
145 $output = drupal_render_children($element);
146 return '<div class="filefield-source filefield-source-imce clear-block">' . $output . '</div>';
150 * Define routes for Imce source.
155 public static function routes() {
158 $routes['filefield_sources.imce'] = new Route(
159 '/file/imce/{entity_type}/{bundle_name}/{field_name}',
161 '_controller' => '\Drupal\filefield_sources\Controller\ImceController::page',
162 '_title' => 'File Manager',
165 '_access_filefield_sources_field' => 'TRUE',
173 * Implements hook_filefield_source_settings().
175 public static function settings(WidgetInterface $plugin) {
176 $settings = $plugin->getThirdPartySetting('filefield_sources', 'filefield_sources', array(
177 'source_imce' => array(
182 $return['source_imce'] = array(
183 '#title' => t('IMCE file browser settings'),
184 '#type' => 'details',
185 '#access' => \Drupal::moduleHandler()->moduleExists('imce'),
188 // $imce_admin_url = \Drupal::url('imce.admin');
189 $imce_admin_url = 'admin/config/media/imce';
190 $return['source_imce']['imce_mode'] = array(
192 '#title' => t('File browser mode'),
194 0 => t('Restricted: Users can only browse the field directory. No file operations are allowed.'),
195 1 => t('Full: Browsable directories are defined by <a href=":imce-admin-url">IMCE configuration profiles</a>. File operations are allowed.', array(':imce-admin-url' => $imce_admin_url)),
197 '#default_value' => isset($settings['source_imce']['imce_mode']) ? $settings['source_imce']['imce_mode'] : 0,
205 * Pre-renders widget form.
207 public static function preRenderWidget($element) {
208 // Hide elements if there is already an uploaded file.
209 if (!empty($element['#value']['fids'])) {
210 $element['filefield_imce']['imce_paths']['#access'] = FALSE;
211 $element['filefield_imce']['imce_button']['#access'] = FALSE;