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?

  • Check watchdog, you'll see what the replacements mean. – user21641 Feb 12 '16 at 8:25
  • That was the first thing I checked, but there was nothing regarding this, then only I looked at apache log. – Suresh R Feb 12 '16 at 8:59
  • Check the DB directly. – user21641 Feb 12 '16 at 9:29
  • Possible duplicate of How do I save a file with managed_file in a theme? – mradcliffe Feb 12 '16 at 14:07

From Drupal 8: Use Custom Submit Handler for Theme Settings Form:

Use this method to add custom submit handler for theme settings form in drupal 8.

/**
 * Implements hook_form_system_theme_settings_alter().
 */
function MY_THEME_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $theme_file = drupal_get_path('theme', 'MY_THEME') . '/MY_THEME.theme';
  $build_info = $form_state->getBuildInfo();
  if (!in_array($theme_file, $build_info['files'])) {
    $build_info['files'][] = $theme_file;
  }
  $form_state->setBuildInfo($build_info);

  $form['#submit'][] = 'MY_THEME_form_system_theme_settings_submit';
}

function MY_THEME_form_system_theme_settings_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  // TODO: Extra submssio logic.
  // This submit handler will be called before default submit handler for this form.
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.