View source
<?php
namespace Drupal\imce;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Url;
class ImceFileField {
public static function supportedWidgets() {
$widgets =& drupal_static(__FUNCTION__);
if (!isset($widgets)) {
$widgets = [
'file_generic',
'image_image',
];
\Drupal::moduleHandler()
->alter('imce_supported_widgets', $widgets);
$widgets = array_unique($widgets);
}
return $widgets;
}
public static function isWidgetSupported(WidgetInterface $widget) {
return in_array($widget
->getPluginId(), static::supportedWidgets());
}
public static function widgetSettingsForm(WidgetInterface $widget) {
$form = [];
if (static::isWidgetSupported($widget)) {
$form['enabled'] = [
'#type' => 'checkbox',
'#title' => t('Allow users to select files from <a href=":url">Imce File Manager</a> for this field.', [
':url' => Url::fromRoute('imce.admin')
->toString(),
]),
'#default_value' => $widget
->getThirdPartySetting('imce', 'enabled'),
];
}
return $form;
}
public static function alterWidgetSettingsSummary(&$summary, $context) {
$widget = $context['widget'];
if (static::isWidgetSupported($widget)) {
$status = $widget
->getThirdPartySetting('imce', 'enabled') ? t('Yes') : t('No');
$summary[] = t('Imce enabled: @status', [
'@status' => $status,
]);
}
}
public static function processWidget($element, FormStateInterface $form_state, $form) {
$element['imce_paths'] = [
'#type' => 'hidden',
'#attributes' => [
'class' => [
'imce-filefield-paths',
],
'data-imce-url' => Url::fromRoute('imce.page', [
'scheme' => $element['#scheme'],
])
->toString(),
],
'#value' => '',
];
$element['#attached']['library'][] = 'imce/drupal.imce.filefield';
$element['#pre_render'][] = [
get_called_class(),
'preRenderWidget',
];
return $element;
}
public static function preRenderWidget($element) {
if (!empty($element['#value']['fids'])) {
$element['imce_paths']['#access'] = FALSE;
}
return $element;
}
public static function setWidgetValue($element, &$input, FormStateInterface $form_state) {
if (empty($input['imce_paths'])) {
return;
}
$paths = $input['imce_paths'];
$input['imce_paths'] = '';
$paths = array_unique(array_filter(explode(':', $paths)));
if (isset($element['#cardinality']) && $element['#cardinality'] > -1) {
$paths = array_slice($paths, 0, $element['#cardinality']);
}
if (!($paths = Imce::accessFilePaths($paths, \Drupal::currentUser(), $element['#scheme']))) {
return;
}
$file_usage = \Drupal::service('file.usage');
$errors = [];
foreach ($paths as $path) {
$file = Imce::getFileEntity($element['#scheme'] . '://' . $path, TRUE);
if ($new_errors = file_validate($file, $element['#upload_validators'])) {
$errors = array_merge($errors, $new_errors);
}
else {
if ($file
->isNew()) {
$file
->save();
}
if ($fid = $file
->id()) {
if (!$file_usage
->listUsage($file)) {
$file_usage
->add($file, 'imce', 'file', $fid);
}
$input['fids'][] = $fid;
}
}
}
if ($errors) {
$errors = array_unique($errors);
if (count($errors) > 1) {
$errors = [
'#theme' => 'item_list',
'#items' => $errors,
];
$message = \Drupal::service('renderer')
->render($errors);
}
else {
$message = array_pop($errors);
}
\Drupal::messenger()
->addMessage($message, 'error');
}
}
}