Commit 7f9287e8 authored by larowlan's avatar larowlan

Issue #1862892 by oriol_e9g, joseph.olstad, vaplas, G.I.Joe, leopathu,...

Issue #1862892 by oriol_e9g, joseph.olstad, vaplas, G.I.Joe, leopathu, peterpoe, cilefen, xjm, larowlan: When a theme has a managed_file field and a submit callback, call to undefined function error is thrown
parent cdfcaea1
......@@ -324,9 +324,21 @@ public function buildForm(array $form, FormStateInterface $form_state, $theme =
// Process the theme and all its base themes.
foreach ($theme_keys as $theme) {
// Include the theme-settings.php file.
$filename = DRUPAL_ROOT . '/' . $themes[$theme]->getPath() . '/theme-settings.php';
if (file_exists($filename)) {
require_once $filename;
$theme_path = drupal_get_path('theme', $theme);
$theme_settings_file = $theme_path . '/theme-settings.php';
$theme_file = $theme_path . '/' . $theme . '.theme';
$filenames = [$theme_settings_file, $theme_file];
foreach ($filenames as $filename) {
if (file_exists($filename)) {
require_once $filename;
// The file must be required for the cached form too.
$files = $form_state->getBuildInfo()['files'];
if (!in_array($filename, $files)) {
$files[] = $filename;
}
$form_state->addBuildInfo('files', $files);
}
}
// Call theme-specific settings.
......
<?php
namespace Drupal\Tests\system\FunctionalJavascript;
use Drupal\file\Entity\File;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\Tests\TestFileCreationTrait;
/**
* Tests that theme form settings works correctly.
*
* @group system
*/
class ThemeFormSettingsTest extends JavascriptTestBase {
use TestFileCreationTrait;
/**
* {@inheritdoc}
*/
public static $modules = ['file'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$admin = $this->drupalCreateUser(['administer themes']);
$this->drupalLogin($admin);
}
/**
* Tests that submission handler works correctly.
*
* @dataProvider providerTestFormSettingsSubmissionHandler
*/
public function testFormSettingsSubmissionHandler($theme) {
\Drupal::service('theme_handler')->install([$theme]);
$page = $this->getSession()->getPage();
$assert_session = $this->assertSession();
$this->drupalGet("admin/appearance/settings/$theme");
// Add a new managed file.
$file = current($this->getTestFiles('image'));
$image_file_path = \Drupal::service('file_system')->realpath($file->uri);
$page->attachFileToField('files[custom_logo]', $image_file_path);
$assert_session->waitForButton('custom_logo_remove_button');
// Assert the new file is uploaded as temporary. This file should not be
// saved as permanent if settings are not submited.
$image_field = $this->xpath('//input[@name="custom_logo[fids]"]')[0];
$file = File::load($image_field->getValue());
$this->assertFalse($file->isPermanent());
$page->pressButton('Save configuration');
\Drupal::entityTypeManager()->getStorage('file')->resetCache();
// Assert the uploaded file is saved as permanent.
$image_field = $this->xpath('//input[@name="custom_logo[fids]"]')[0];
$file = File::load($image_field->getValue());
$this->assertTrue($file->isPermanent());
}
/**
* Provides test data for ::testFormSettingsSubmissionHandler().
*/
public function providerTestFormSettingsSubmissionHandler() {
return [
'test theme.theme' => ['test_theme_theme'],
'test theme-settings.php' => ['test_theme_settings'],
];
}
}
# Schema for the configuration files of the Test theme settings.
test_theme_settings.settings:
type: theme_settings
label: 'Test theme settings'
mapping:
custom_logo:
type: sequence
label: 'Custom logo'
sequence:
type: integer
label: 'fids'
name: 'Theme test theme-settings.php'
type: theme
description: 'Test theme that extends theme settings options via theme-settings.php file'
version: VERSION
core: 8.x
base theme: false
<?php
/**
* @file
* Test to ensure theme compatibility with managed files.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
/**
* Implements hook_form_system_theme_settings_alter().
*/
function test_theme_settings_form_system_theme_settings_alter(&$form, FormStateInterface $form_state) {
$form['custom_logo'] = [
'#type' => 'managed_file',
'#title' => t('Secondary logo.'),
'#default_value' => theme_get_setting('custom_logo'),
'#progress_indicator' => 'bar',
'#progress_message' => t('Please wait...'),
'#upload_location' => 'public://test',
'#upload_validators' => [
'file_validate_extensions' => ['gif png jpg jpeg'],
],
];
$form['#submit'][] = 'test_theme_settings_form_system_theme_settings_submit';
}
/**
* Test theme form settings submission handler.
*/
function test_theme_settings_form_system_theme_settings_submit(&$form, FormStateInterface $form_state) {
if ($file_id = $form_state->getValue(['custom_logo', '0'])) {
$file = File::load($file_id);
$file->setPermanent();
$file->save();
}
}
# Schema for the configuration files of the Test theme theme.
test_theme_theme.settings:
type: theme_settings
label: 'Test theme settings'
mapping:
custom_logo:
type: sequence
label: 'Custom logo'
sequence:
type: integer
label: 'fids'
name: 'Theme test theme.theme file'
type: theme
description: 'Test theme that extends theme settings options via theme.theme file'
version: VERSION
core: 8.x
base theme: false
<?php
/**
* @file
* Test to ensure theme compatibility with managed files.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
/**
* Implements hook_form_system_theme_settings_alter().
*/
function test_theme_theme_form_system_theme_settings_alter(&$form, FormStateInterface $form_state) {
$form['custom_logo'] = [
'#type' => 'managed_file',
'#title' => t('Secondary logo.'),
'#default_value' => theme_get_setting('custom_logo'),
'#progress_indicator' => 'bar',
'#progress_message' => t('Please wait...'),
'#upload_location' => 'public://test',
'#upload_validators' => [
'file_validate_extensions' => ['gif png jpg jpeg'],
],
];
$form['#submit'][] = 'test_theme_theme_form_system_theme_settings_submit';
}
/**
* Test theme form settings submission handler.
*/
function test_theme_theme_form_system_theme_settings_submit(&$form, FormStateInterface $form_state) {
if ($file_id = $form_state->getValue(['custom_logo', '0'])) {
$file = File::load($file_id);
$file->setPermanent();
$file->save();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment