I'm creating some Drupal 8 theme and I need some additional settings for it. So, I have to add some managed_file field in theme settings.
I have theme-settings.php file where I define this field and I want to make this file permanent.
There is what I have in theme-settings.php so far:
<?php
/**
* @file
* Provides an additional config form for theme settings.
*/
use Drupal\Core\Form\FormStateInterface;
// Set theme name to use in the key values.
$theme_name = \Drupal::theme()->getActiveTheme()->getName();
/**
* Implements hook_form_system_theme_settings_alter().
*
* Form override for theme settings.
*/
function onion_form_system_theme_settings_alter(array &$form, FormStateInterface $form_state) {
$form['options_settings'] = [
'#type' => 'fieldset',
'#title' => t('Theme Specific Settings'),
];
// Main navigation logo
$form['options_settings']['menu_logo'] = [
'#title' => t('Menu Logo'),
'#type' => 'managed_file',
'#default_value' => theme_get_setting('menu_logo'),
'#upload_location' => 'public://',
'#description' => t('Logo below main navigation. Available extensions (.gif .png .jpg .jpeg)'),
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg')
),
];
$form['#submit'][] = '_onion_form_system_theme_settings_submit';
}
function _onion_form_system_theme_settings_submit(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
$menu_logo_id = $form_state['values']['options_settings']['menu_logo'];
$menu_logo_file = \Drupal\file\Entity\File::load($menu_logo_id);
// set status to permanent so that its not deleted
$menu_logo_file->setPermanent();
$menu_logo_file->save();
}
But I'm getting this warning:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function '_onion_form_system_theme_settings_submit' not found or invalid function name in Drupal\Core\Form\FormSubmitter->executeSubmitHandlers() (line 111 of core/lib/Drupal/Core/Form/FormSubmitter.php).
What I'm doing wrong there and what is the best and right way to set file permanent?
$form['#submit'][]
, but I already tried as you wrote and I got the same warning...