View source
<?php
namespace Drupal\geshifilter\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\geshifilter\GeshiFilterCss;
use Drupal\Core\Cache\Cache;
use Drupal\geshifilter\GeshiFilter;
class GeshiFilterLanguagesForm extends ConfigFormBase {
public static $modules = [
'libraries',
'geshifilter',
];
protected $config;
public function getFormId() {
return 'geshifilter_languages';
}
protected function getEditableConfigNames() {
return [
'geshifilter.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, $view = NULL) {
$config = $this
->config('geshifilter.settings');
$geshi_library = GeshiFilter::loadGeshi();
if (!$geshi_library['loaded']) {
$this
->messenger()
->addError($geshi_library['error message']);
return [];
}
$add_checkbox = TRUE;
$add_tag_option = !$config
->get('use_format_specific_options');
$form['language_settings'] = $this
->perLanguageSettings($view, $add_checkbox, $add_tag_option);
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('geshifilter.settings');
$languages = GeshiFilter::getAvailableLanguages();
$values = $form_state
->getValue('language');
$config = $this
->config('geshifilter.settings');
$values = array_merge($config
->get('language'), $values);
foreach ($languages as $language1 => $language_data1) {
if ($values[$language1]['enabled'] == FALSE) {
continue;
}
$tags1 = GeshiFilter::tagSplit($values[$language1]['tags']);
foreach ($languages as $language2 => $language_data2) {
if ($language1 == $language2) {
continue;
}
$tags2 = GeshiFilter::tagSplit($values[$language2]['tags']);
$generics = GeshiFilter::tagSplit($config
->get('tags'));
$tags2 = array_merge($tags2, $generics);
foreach ($tags1 as $tag1) {
foreach ($tags2 as $tag2) {
if ($tag1 == $tag2) {
$name = "language[{$language2}][tags]";
$form_state
->setErrorByName($name, $this
->t('The language tags should differ between languages and from the generic tags.'));
}
}
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('geshifilter.settings');
$languages = $form_state
->getValue('language');
foreach ($languages as $key => $value) {
if ($value["enabled"] == FALSE) {
$config
->clear("language.{$key}.enabled");
}
else {
$config
->set("language.{$key}.enabled", TRUE);
}
if ($value["tags"] == '') {
$config
->clear("language.{$key}.tags");
}
else {
$config
->set("language.{$key}.tags", $value["tags"]);
}
}
$config
->save();
if ($config
->get('css_mode', GeshiFilter::CSS_INLINE) == GeshiFilter::CSS_CLASSES_AUTOMATIC) {
GeshiFilterCss::generateLanguagesCssFile();
}
Cache::invalidateTags([
'geshifilter',
]);
}
protected function perLanguageSettings($view, $add_checkbox, $add_tag_option) {
$config = $this
->config('geshifilter.settings');
$form = [];
$header = [
$this
->t('Language'),
$this
->t('GeSHi language code'),
];
if ($add_tag_option) {
$header[] = $this
->t('Tag/language attribute value');
}
$form['language'] = [
'#type' => 'table',
'#header' => $header,
'#empty' => $this
->t('Nome language is available.'),
];
$languages = GeshiFilter::getAvailableLanguages();
foreach ($languages as $language => $language_data) {
$enabled = $config
->get("language.{$language}.enabled", FALSE);
if ($view == 'enabled' && !$enabled || $view == 'disabled' && $enabled) {
continue;
}
$form['language'][$language] = [];
if ($add_checkbox) {
$form['language'][$language]['enabled'] = [
'#type' => 'checkbox',
'#default_value' => $enabled,
'#title' => $language_data['fullname'],
];
}
else {
$form['language'][$language]['fullname'] = [
'#type' => 'markup',
'#markup' => $language_data['fullname'],
];
}
$form['language'][$language]['name'] = [
'#type' => 'markup',
'#markup' => $language,
];
if ($add_tag_option) {
$form['language'][$language]['tags'] = [
'#type' => 'textfield',
'#default_value' => $config
->get("language.{$language}.tags", ''),
'#size' => 20,
];
}
}
return $form;
}
}