View source
<?php
namespace Drupal\styleswitcher\Form;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class StyleswitcherStyleForm extends FormBase {
protected $themeManager;
public function __construct(ThemeManagerInterface $theme_manager) {
$this->themeManager = $theme_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('theme.manager'));
}
public function getFormId() {
return 'styleswitcher_style_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $style = NULL) {
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Title'),
'#description' => $this
->t('Human-readable name for this style.'),
'#default_value' => $style ? $style['label'] : '',
'#required' => TRUE,
];
if ($style) {
list(, $name_value) = explode('/', $style['name']);
}
$form['name'] = [
'#type' => 'machine_name',
'#description' => $this
->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
'#default_value' => $style ? $name_value : '',
'#field_prefix' => 'custom/',
'#machine_name' => [
'source' => [
'label',
],
'exists' => [
$this,
'exists',
],
],
];
if ($style) {
$form['name']['#description'] .= '<br />' . $this
->t('<strong>WARNING:</strong> if you change style machine name, users who have chosen this style will see the default one instead until they switch again.');
}
$form['old_name'] = [
'#type' => 'value',
'#value' => $style ? $style['name'] : '',
];
$form['path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path'),
'#description' => $this
->t('The path to the stylesheet file relative to the site root or an external CSS file.'),
'#default_value' => $style ? $style['path'] : '',
'#required' => TRUE,
'#access' => !$style || isset($style['path']),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
if ($style) {
$form['actions']['delete'] = [
'#type' => 'link',
'#title' => $this
->t('Delete'),
'#url' => Url::fromRoute('styleswitcher.style_delete', [
'style' => $name_value,
]),
'#attributes' => [
'class' => [
'button',
'button--danger',
],
],
'#access' => isset($style['path']),
];
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$form_state
->setValueForElement($form['label'], trim($form_state
->getValue('label')));
$path = $form_state
->getValue('path');
if ($path === '') {
$form_state
->setValueForElement($form['path'], NULL);
}
else {
$path = trim($path);
$form_state
->setValueForElement($form['path'], $path);
if (!is_file($path) && !UrlHelper::isExternal($path)) {
$form_state
->setErrorByName('path', $this
->t('Stylesheet file %path does not exist.', [
'%path' => $path,
]));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$old_name = $form_state
->getValue('old_name');
$styles = styleswitcher_custom_styles();
$style = [
'label' => $form_state
->getValue('label'),
'name' => 'custom/' . $form_state
->getValue('name'),
'path' => $form_state
->getValue('path'),
];
if ($old_name !== '') {
unset($styles[$old_name]);
if ($style['name'] != $old_name) {
$config = $this
->configFactory()
->getEditable('styleswitcher.styles_settings');
$settings = $config
->get('settings') ?? [];
foreach (array_keys($settings) as $theme) {
if (isset($settings[$theme][$old_name])) {
$settings[$theme][$style['name']] = $settings[$theme][$old_name];
unset($settings[$theme][$old_name]);
}
}
$config
->set('settings', $settings)
->save();
}
}
$styles[$style['name']] = $style;
$this
->configFactory()
->getEditable('styleswitcher.custom_styles')
->set('styles', $styles)
->save();
$this
->messenger()
->addStatus($this
->t('The style %title has been saved.', [
'%title' => $style['label'],
]));
$form_state
->setRedirect('styleswitcher.admin');
}
public function title(array $style) {
return $style['label'];
}
public function exists($input) {
$active_theme_name = $this->themeManager
->getActiveTheme()
->getName();
return styleswitcher_style_load($input, $active_theme_name, 'custom');
}
}