View source
<?php
namespace Drupal\sophron\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Schema\SchemaCheckTrait;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\sophron\MimeMapManagerInterface;
use Drupal\sophron\CoreExtensionMimeTypeGuesserExtended;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\Yaml;
class SettingsForm extends ConfigFormBase {
use SchemaCheckTrait;
protected $mimeMapManager;
protected $typedConfig;
public function getFormId() {
return 'sophron_settings';
}
protected function getEditableConfigNames() {
return [
'sophron.settings',
];
}
public function __construct(ConfigFactoryInterface $config_factory, MimeMapManagerInterface $mime_map_manager, TypedConfigManagerInterface $typed_config) {
parent::__construct($config_factory);
$this->mimeMapManager = $mime_map_manager;
$this->typedConfig = $typed_config;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('sophron.mime_map.manager'), $container
->get('config.typed'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('sophron.settings');
$form['tabs'] = [
'#type' => 'vertical_tabs',
'#tree' => FALSE,
];
$guessing_status = $this->mimeMapManager
->requirements('runtime')['mime_type_guessing_sophron'];
$form['guessing'] = [
'#type' => 'details',
'#title' => $guessing_status['title'],
'#group' => 'tabs',
];
$form['guessing']['info'] = [
'#type' => 'item',
'#title' => $guessing_status['value'],
'#description' => $guessing_status['description'],
];
$form['mapping'] = [
'#type' => 'details',
'#title' => $this
->t('Mapping'),
'#description' => $this
->t("Manage additional MIME types and mapping issues."),
'#group' => 'tabs',
];
$options = [
MimeMapManagerInterface::DRUPAL_MAP => $this
->t("<strong>Sophron map.</strong> Use this map for maximum compatibility with Drupal, still increasing the number of MIME types and file extensions identified."),
MimeMapManagerInterface::DEFAULT_MAP => $this
->t("<strong>MimeMap default map.</strong> Use this map for maximum compatibility with Apache and Freedesktop projects, accepting differences versus current Drupal mappings."),
MimeMapManagerInterface::CUSTOM_MAP => $this
->t("<strong>Custom map.</strong> Use this option to select a custom built mapping class."),
];
$form['mapping']['map_option'] = [
'#type' => 'radios',
'#title' => $this
->t('Map'),
'#default_value' => $config
->get('map_option'),
'#options' => $options,
'#required' => TRUE,
'#description' => $this
->t("Select the map to use."),
];
$form['mapping']['map_class'] = [
'#type' => 'textfield',
'#title' => $this
->t('Class name'),
'#description' => $this
->t('A fully qualified PHP class name. The map class must extend from \\FileEye\\MimeMap\\Map\\AbstractMap.'),
'#default_value' => $config
->get('map_class'),
'#states' => [
'visible' => [
':radio[name="map_option"]' => [
'value' => MimeMapManagerInterface::CUSTOM_MAP,
],
],
],
];
$commands = $config
->get('map_commands');
$form['mapping']['map_commands'] = [
'#type' => 'textarea',
'#title' => $this
->t('Mapping commands'),
'#description' => $this
->t("The commands below alter the default MIME type mapping. More information in the module's README.md file."),
'#description_display' => 'before',
'#rows' => 5,
'#default_value' => empty($commands) ? '' : Yaml::dump($commands, 1),
];
if ($errors = $this->mimeMapManager
->getMappingErrors($this->mimeMapManager
->getMapClass())) {
$form['mapping']['mapping_errors'] = [
'#type' => 'details',
'#collapsible' => TRUE,
'#open' => TRUE,
'#title' => $this
->t("Mapping errors"),
'#description' => $this
->t("The list below shows the errors occurring in applying mapping commands to the map. Correct them to clean up the list."),
];
$rows = [];
foreach ($errors as $error) {
$rows[] = [
$error['method'],
"'" . implode("', '", $error['args']) . "'",
$error['type'],
$error['message'],
];
}
$form['mapping']['mapping_errors']['table'] = [
'#type' => 'table',
'#id' => 'sophron-mapping-errors-table',
'#header' => [
[
'data' => $this
->t('Method'),
],
[
'data' => $this
->t('Arguments'),
],
[
'data' => $this
->t('Error'),
],
[
'data' => $this
->t('Description'),
],
],
'#rows' => $rows,
];
}
if ($gaps = $this
->determineMapGaps($this->mimeMapManager
->getMapClass())) {
$form['mapping']['gaps'] = [
'#type' => 'details',
'#collapsible' => TRUE,
'#open' => FALSE,
'#title' => $this
->t("Mapping gaps"),
'#description' => $this
->t("The list below shows the gaps of the current map vs. Drupal's core MIME type mapping. Overcome the gaps by adding additional mapping commands."),
];
$form['mapping']['gaps']['table'] = [
'#type' => 'table',
'#id' => 'sophron-mapping-gaps-table',
'#header' => [
[
'data' => $this
->t('File extension'),
],
[
'data' => $this
->t('Drupal core MIME type'),
],
[
'data' => $this
->t('Gap'),
],
],
'#rows' => $gaps,
];
}
$form['types'] = [
'#type' => 'details',
'#title' => $this
->t('MIME types'),
'#description' => $this
->t("List of MIME types and their file extensions."),
'#group' => 'tabs',
];
$rows = [];
$i = 1;
foreach ($this->mimeMapManager
->listTypes() as $type_string) {
if ($type = $this->mimeMapManager
->getType($type_string)) {
$rows[] = [
$i++,
$type_string,
implode(', ', $type
->getExtensions()),
$type
->getDescription(),
implode(', ', $type
->getAliases()),
];
}
}
$form['types']['table'] = [
'#type' => 'table',
'#id' => 'sophron-mime-types-table',
'#header' => [
[
'data' => $this
->t('#'),
],
[
'data' => $this
->t('MIME Type'),
],
[
'data' => $this
->t('File extensions'),
],
[
'data' => $this
->t('Description'),
],
[
'data' => $this
->t('Aliases'),
],
],
'#rows' => $rows,
];
$form['extensions'] = [
'#type' => 'details',
'#title' => $this
->t('File extensions'),
'#description' => $this
->t("List of file extensions and their MIME types."),
'#group' => 'tabs',
];
$rows = [];
$i = 1;
foreach ($this->mimeMapManager
->listExtensions() as $extension_string) {
if ($extension = $this->mimeMapManager
->getExtension($extension_string)) {
$rows[] = [
$i++,
$extension_string,
implode(', ', $extension
->getTypes()),
$this->mimeMapManager
->getType($extension
->getDefaultType())
->getDescription(),
];
}
}
$form['extensions']['table'] = [
'#type' => 'table',
'#id' => 'sophron-extensions-table',
'#header' => [
[
'data' => $this
->t('#'),
],
[
'data' => $this
->t('File extension'),
],
[
'data' => $this
->t('MIME types'),
],
[
'data' => $this
->t('Description'),
],
],
'#rows' => $rows,
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('map_option') == MimeMapManagerInterface::CUSTOM_MAP && !$this->mimeMapManager
->isMapClassValid($form_state
->getValue('map_class'))) {
$form_state
->setErrorByName('map_class', $this
->t("The map class is invalid. Make sure the selected class is an extension of \\FileEye\\MimeMap\\Map\\AbstractMap."));
}
if ($form_state
->getValue('map_commands') !== '') {
try {
$map_commands = Yaml::parse($form_state
->getValue('map_commands'));
$data = $this->configFactory
->get('sophron.settings')
->get();
$data['map_commands'] = $map_commands;
$schema_errors = $this
->checkConfigSchema($this->typedConfig, 'sophron.settings', $data);
if (is_array($schema_errors)) {
$fail_items = [];
foreach ($schema_errors as $key => $value) {
$matches = [];
if (preg_match('/sophron\\.settings\\:map\\_commands\\.(\\d+)/', $key, $matches)) {
$item = (int) $matches[1] + 1;
$fail_items[$item] = $item;
}
}
$form_state
->setErrorByName('map_commands', $this
->t("The items at line(s) @lines are wrongly typed. Make sure they follow the pattern '- [method, [arg1, ..., argN]]'.", [
'@lines' => implode(', ', $fail_items),
]));
}
} catch (\Exception $e) {
$form_state
->setErrorByName('map_commands', $this
->t("YAML syntax error: @error", [
'@error' => $e
->getMessage(),
]));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable('sophron.settings');
try {
$config
->set('map_option', $form_state
->getValue('map_option'));
$config
->set('map_class', $form_state
->getValue('map_class'));
$commands = Yaml::parse($form_state
->getValue('map_commands'));
$config
->set('map_commands', $commands ?: []);
$config
->save();
} catch (\Exception $e) {
}
parent::submitForm($form, $form_state);
}
protected function determineMapGaps() {
$core_extended_guesser = new CoreExtensionMimeTypeGuesserExtended();
$extensions = $core_extended_guesser
->listExtensions();
sort($extensions);
$rows = [];
foreach ($extensions as $extension_string) {
$drupal_mime_type = $core_extended_guesser
->guess('a.' . $extension_string);
$extension = $this->mimeMapManager
->getExtension($extension_string);
if ($extension) {
try {
$mimemap_mime_type = $extension
->getDefaultType();
} catch (\Exception $e) {
$mimemap_mime_type = '';
}
}
$gap = '';
if ($mimemap_mime_type === '') {
$gap = $this
->t('No MIME type mapped to this file extension.');
}
elseif (mb_strtolower($drupal_mime_type) != mb_strtolower($mimemap_mime_type)) {
$gap = $this
->t("File extension mapped to '@type' instead.", [
'@type' => $mimemap_mime_type,
]);
}
if ($gap !== '') {
$rows[] = [
$extension_string,
$drupal_mime_type,
$gap,
];
}
}
return $rows;
}
}