I am creating a new D8 theme and having slideshow functionality in it. For slideshow, we have title, description and image field in the theme settings form. I am able to alter the theme setting form using THEME_NAME_form_system_theme_settings_alter
, but when I add a custom submit handler the form seems to break and get internal server error.
My objective for adding custom submit is to make the uploaded files status as permanent.
Here is my code,
function THEME_NAME_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
// $form['#submit'][] = 'custom_settings_form_submit'; //tried this as well
$form['actions']['submit']['#submit'][] = '_custom_settings_form_submit';
}
function _custom_settings_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$account = \Drupal::currentUser();
$values = $form_state->getValues();
for ($i = 0; $i <= SLIDESHOW_COUNT; $i++) {
if (isset($values['slide_image_path' . $i]) && !empty($values['slide_image_path' . $i])) {
// Load the file via file.fid.
$file = file_load($values['slide_image_path' . $i][0]);
// Change status to permanent.
$file->setPermanent();
$file->save();
// $file_usage = \Drupal::service('file.usage');
// $file_usage->add($file, 'user', 'user', $account->id());
}
}
}
NOTE:
This is the error present in apache error log:
PHP Fatal error: Invalid placeholder (!message) in string: %type: !message in %function (line %line of %file). in /core/lib/Drupal/Component/Render/FormattableMarkup.php on line 240
Does anyone have any idea where I'm doing it wrong? or is there any workaround for making the file status permanent?