Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU Boulder Site Configuration v2.6.4 #45

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 73 additions & 20 deletions src/Form/AppearanceForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
namespace Drupal\ucb_site_configuration\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\system\Form\ThemeSettingsForm;
use Drupal\ucb_site_configuration\SiteConfiguration;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\MimeTypeGuesserInterface;

/**
* The form for the "Appearance" tab in CU Boulder site settings.
*/
class AppearanceForm extends ThemeSettingsForm {
class AppearanceForm extends ConfigFormBase {
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;

/**
* The current user.
Expand All @@ -40,23 +43,16 @@ class AppearanceForm extends ThemeSettingsForm {
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler instance to use.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
* @param \Symfony\Component\Mime\MimeTypeGuesserInterface $mime_type_guesser
* The MIME type guesser instance to use.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system.
* @param \Drupal\Core\Session\AccountInterface $user
* The current user.
* @param \Drupal\ucb_site_configuration\SiteConfiguration $service
* The site configuration service defined in this module.
*/
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, MimeTypeGuesserInterface $mime_type_guesser, ThemeManagerInterface $theme_manager, FileSystemInterface $file_system, AccountInterface $user, SiteConfiguration $service) {
parent::__construct($config_factory, $module_handler, $theme_handler, $mime_type_guesser, $theme_manager, $file_system);
public function __construct(ConfigFactoryInterface $config_factory, FileSystemInterface $file_system, AccountInterface $user, SiteConfiguration $service) {
parent::__construct($config_factory);
$this->fileSystem = $file_system;
$this->user = $user;
$this->service = $service;
}
Expand All @@ -72,10 +68,6 @@ public function __construct(ConfigFactoryInterface $config_factory, ModuleHandle
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('module_handler'),
$container->get('theme_handler'),
$container->get('file.mime_type.guesser'),
$container->get('theme.manager'),
$container->get('file_system'),
$container->get('current_user'),
$container->get('ucb_site_configuration')
Expand All @@ -89,14 +81,32 @@ public function getFormId() {
return 'ucb_site_configuration_appearance_form';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [$this->service->getThemeName() . '.settings'];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $theme = '') {
$theme = $this->service->getThemeName();
$form = parent::buildForm($form, $form_state, $theme);
$this->service->buildThemeSettingsForm($form, $form_state);
if ($this->user->hasPermission('edit ucb site advanced')) {
$advanced = [];
$advanced['ucb_sidebar_position'] = [
'#type' => 'select',
'#title' => $this->t('Sidebar position'),
'#default_value' => theme_get_setting('ucb_sidebar_position', $theme),
'#options' => [
'right' => $this->t('Right (default)'),
'left' => $this->t('Left'),
],
'#description' => $this->t('Select if sidebar content should appear on the left or right side of a page.'),
];
$advanced['custom_logo'] = [
'#type' => 'container',
];
Expand Down Expand Up @@ -216,8 +226,15 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
if ($fidsLight && isset($fidsLight[0]) && ($file = File::load($fidsLight[0]))) {
$this->makePermanent($file);
}

// Excludes unnecessary elements before saving.
$form_state->cleanValues();
$form_state->unsetValue('ucb_custom_logo_dark_upload');
$form_state->unsetValue('ucb_custom_logo_light_upload');

$values = $form_state->getValues();
$config = $this->config($this->service->getThemeName() . '.settings');
theme_settings_convert_to_config($values, $config)->save();

parent::submitForm($form, $form_state);
}
Expand All @@ -233,4 +250,40 @@ protected function makePermanent(FileInterface $file) {
$file->save();
}

/**
* Helper function for the system_theme_settings form.
*
* Attempts to validate normal system paths, paths relative to the public files
* directory, or stream wrapper URIs. If the given path is any of the above,
* returns a valid path or URI that the theme system can display.
*
* @param string $path
* A path relative to the Drupal root or to the public files directory, or
* a stream wrapper URI.
*
* @return mixed
* A valid path that can be displayed through the theme system, or FALSE if
* the path could not be validated.
*
* @see \Drupal\system\Form\ThemeSettingsForm->validatePath
*/
protected function validatePath($path) {
// Absolute local file paths are invalid.
if ($this->fileSystem->realpath($path) == $path) {
return FALSE;
}
// A path relative to the Drupal root or a fully qualified URI is valid.
if (is_file($path)) {
return $path;
}
// Prepend 'public://' for relative file paths within public filesystem.
if (StreamWrapperManager::getScheme($path) === FALSE) {
$path = 'public://' . $path;
}
if (is_file($path)) {
return $path;
}
return FALSE;
}

}
11 changes: 0 additions & 11 deletions src/SiteConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,6 @@ public function buildThemeSettingsForm(array &$form, FormStateInterface &$form_s
'#open' => TRUE,
];

$form['misc']['ucb_sidebar_position'] = [
'#type' => 'select',
'#title' => $this->t('Where to show sidebar content on a page'),
'#default_value' => theme_get_setting('ucb_sidebar_position', $themeName),
'#options' => [
$this->t('Left'),
$this->t('Right'),
],
'#description' => $this->t('Select if sidebar content should appear on the left or right side of a page.'),
];

// Choose where social share buttons are positioned on each page.
$form['misc']['ucb_social_share_position'] = [
'#type' => 'select',
Expand Down
2 changes: 1 addition & 1 deletion ucb_site_configuration.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CU Boulder Site Configuration
description: 'Allows CU Boulder site administrators to configure site-specific settings.'
core_version_requirement: ^9 || ^10
type: module
version: '2.6.3'
version: '2.6.4'
package: CU Boulder
dependencies:
- block
Expand Down
Loading