View source
<?php
namespace Drupal\imce\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\Url;
use Drupal\imce\ImceSettersTrait;
use Drupal\user\RoleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ImceSettingsForm extends ConfigFormBase {
use ImceSettersTrait;
protected $entityTypeManager;
protected $configSystemFile;
protected $streamWrapperManager;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance
->setConfigSystemFile($container
->get('config.factory')
->get('system.file'));
$instance
->setEntityTypeManager($container
->get('entity_type.manager'));
$instance
->setStreamWrapperManager($container
->get('stream_wrapper_manager'));
return $instance;
}
public function getFormId() {
return 'imce_settings_form';
}
protected function getEditableConfigNames() {
return [
'imce.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('imce.settings');
$form['roles_profiles'] = $this
->buildRolesProfilesTable($config
->get('roles_profiles') ?: []);
$form['common'] = [
'#type' => 'details',
'#title' => $this
->t('Common settings'),
];
$form['common']['abs_urls'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable absolute URLs'),
'#description' => $this
->t('Make the file manager return absolute file URLs to other applications.'),
'#default_value' => $config
->get('abs_urls'),
];
$form['common']['admin_theme'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use admin theme for IMCE paths'),
'#default_value' => $config
->get('admin_theme'),
'#description' => $this
->t('If you have user interface issues with the active theme you may consider switching to admin theme.'),
];
$form['#attached']['library'][] = 'imce/drupal.imce.admin';
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('imce.settings');
$config
->set('abs_urls', $form_state
->getValue('abs_urls'));
$config
->set('admin_theme', $form_state
->getValue('admin_theme'));
$roles_profiles = $form_state
->getValue('roles_profiles');
foreach ($roles_profiles as $rid => &$profiles) {
if (!($profiles = array_filter($profiles))) {
unset($roles_profiles[$rid]);
}
}
$config
->set('roles_profiles', $roles_profiles);
$config
->save();
if (!empty($roles_profiles[RoleInterface::ANONYMOUS_ID])) {
$this
->messenger()
->addMessage($this
->t('You have enabled anonymous access to the file manager. Please make sure this is not a misconfiguration.'), 'warning');
}
parent::submitForm($form, $form_state);
}
public function getProfileOptions() {
$options = [
'' => '-' . $this
->t('None') . '-',
];
foreach ($this->entityTypeManager
->getStorage('imce_profile')
->loadMultiple() as $pid => $profile) {
$options[$pid] = $profile
->label();
}
return $options;
}
public function buildHeaderProfilesTable() : array {
$wrappers = $this->streamWrapperManager
->getNames(StreamWrapperInterface::WRITE_VISIBLE);
$imce_url = Url::fromRoute('imce.page')
->toString();
$rp_table['#header'] = [
$this
->t('Role'),
];
$default = $this->configSystemFile
->get('default_scheme');
foreach ($wrappers as $scheme => $name) {
$url = $scheme === $default ? $imce_url : $imce_url . '/' . $scheme;
$rp_table['#header'][]['data'] = [
'#markup' => '<a href="' . $url . '">' . Html::escape($name) . '</a>',
];
}
return $rp_table;
}
public function buildRowsProfilesTables($roles, $roles_profiles, $wrappers) {
$rp_table = [];
foreach ($roles as $rid => $role) {
$rp_table[$rid]['role_name'] = [
'#plain_text' => $role
->label(),
];
foreach ($wrappers as $scheme => $name) {
$rp_table[$rid][$scheme] = [
'#type' => 'select',
'#options' => $this
->getProfileOptions(),
'#default_value' => isset($roles_profiles[$rid][$scheme]) ? $roles_profiles[$rid][$scheme] : '',
];
}
}
return $rp_table;
}
public function buildRolesProfilesTable(array $roles_profiles) {
$rp_table = [
'#type' => 'table',
];
$roles = user_roles();
$wrappers = $this->streamWrapperManager
->getNames(StreamWrapperInterface::WRITE_VISIBLE);
$imce_url = Url::fromRoute('imce.page')
->toString();
$rp_table += $this
->buildHeaderProfilesTable($wrappers);
$rp_table += $this
->buildRowsProfilesTables($roles, $roles_profiles, $wrappers);
$rp_table['#prefix'] = '<h3>' . $this
->t('Role-profile assignments') . '</h3>';
$rp_table['#suffix'] = '<div class="description">' . $this
->t('Assign configuration profiles to user roles for available file systems. Users with multiple roles get the bottom most profile.') . ' ' . $this
->t('The default file system %name is accessible at :url path.', [
'%name' => $wrappers[$this->configSystemFile
->get('default_scheme')],
':url' => $imce_url,
]) . '</div>';
return $rp_table;
}
}