View source
<?php
namespace Drupal\filefield_sources\Plugin\FilefieldSource;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\filefield_sources\FilefieldSourceInterface;
use Symfony\Component\Routing\Route;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
class Imce implements FilefieldSourceInterface, TrustedCallbackInterface {
public static function value(array &$element, &$input, FormStateInterface $form_state) {
if (isset($input['filefield_imce']['imce_paths']) && $input['filefield_imce']['imce_paths'] != '') {
$instance = \Drupal::entityTypeManager()
->getStorage('field_config')
->load($element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
$field_settings = $instance
->getSettings();
$scheme = $field_settings['uri_scheme'];
$imce_paths = explode(':', $input['filefield_imce']['imce_paths']);
$uris = [];
foreach ($imce_paths as $imce_path) {
$uri = rawurldecode($scheme . '://' . $imce_path);
$uris[] = $uri;
}
$connection = \Drupal::service('database');
$fids = $connection
->select('file_managed', 'f')
->condition('uri', $uris, 'IN')
->fields('f', [
'fid',
])
->execute()
->fetchCol();
if ($fids) {
$files = File::loadMultiple($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.'));
}
$input['filefield_imce']['imce_paths'] = '';
}
}
public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
$instance = \Drupal::entityTypeManager()
->getStorage('field_config')
->load($element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
$element['filefield_imce'] = [
'#weight' => 100.5,
'#theme' => 'filefield_sources_element',
'#source_id' => 'imce',
'#filefield_source' => TRUE,
'#description' => filefield_sources_element_validation_help($element['#upload_validators']),
];
$imce_url = Url::fromRoute('filefield_sources.imce', [
'entity_type' => $element['#entity_type'],
'bundle_name' => $element['#bundle'],
'field_name' => $element['#field_name'],
], [
'query' => [
'sendto' => 'imceFileField.sendto',
'fieldId' => $element['#attributes']['data-drupal-selector'] . '-filefield-imce',
],
])
->toString();
$element['filefield_imce']['browse'] = [
'#type' => 'markup',
'#markup' => '<span>' . t('No file selected') . '</span> (<a class="filefield-sources-imce-browse" href="' . $imce_url . '">' . t('browse') . '</a>)',
];
$element['#attached']['library'][] = 'imce/drupal.imce.filefield';
$element['#pre_render'][] = [
get_called_class(),
'preRenderWidget',
];
$element['filefield_imce']['imce_paths'] = [
'#type' => 'hidden',
'#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'] = [
'#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;
}
public static function element($variables) {
$element = $variables['element'];
$output = '';
foreach (Element::children($element) as $key) {
if (!empty($element[$key])) {
$output .= \Drupal::service('renderer')
->render($element[$key]);
}
}
return '<div class="filefield-source filefield-source-imce clear-block">' . $output . '</div>';
}
public static function routes() {
$routes = [];
$routes['filefield_sources.imce'] = new Route('/file/imce/{entity_type}/{bundle_name}/{field_name}', [
'_controller' => '\\Drupal\\filefield_sources\\Controller\\ImceController::page',
'_title' => 'File Manager',
], [
'_access_filefield_sources_field' => 'TRUE',
]);
return $routes;
}
public static function settings(WidgetInterface $plugin) {
$settings = $plugin
->getThirdPartySetting('filefield_sources', 'filefield_sources', [
'source_imce' => [
'imce_mode' => 0,
],
]);
$return['source_imce'] = [
'#title' => t('IMCE file browser settings'),
'#type' => 'details',
'#access' => \Drupal::moduleHandler()
->moduleExists('imce'),
];
$imce_admin_url = 'admin/config/media/imce';
$return['source_imce']['imce_mode'] = [
'#type' => 'radios',
'#title' => t('File browser mode'),
'#options' => [
0 => t('Restricted: Users can only browse the field directory. No file operations are allowed.'),
1 => t('Full: Browsable directories are defined by <a href=":imce-admin-url">IMCE configuration profiles</a>. File operations are allowed.', [
':imce-admin-url' => $imce_admin_url,
]),
],
'#default_value' => isset($settings['source_imce']['imce_mode']) ? $settings['source_imce']['imce_mode'] : 0,
];
return $return;
}
public static function preRenderWidget($element) {
if (!empty($element['#value']['fids'])) {
$element['filefield_imce']['imce_paths']['#access'] = FALSE;
$element['filefield_imce']['imce_button']['#access'] = FALSE;
}
return $element;
}
public static function trustedCallbacks() {
return [
'preRenderWidget',
];
}
}