1

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?

2
  • In the drupal.org example it says: $form['actions']['submit']['#submit'][] ='_onion_form_system_theme_settings_submit'; Does that make any difference? Commented Jan 20, 2017 at 11:07
  • I'm on Drupal 8.2.x and I think that on this version it's changed to $form['#submit'][], but I already tried as you wrote and I got the same warning...
    – mixerowsky
    Commented Jan 20, 2017 at 11:30

1 Answer 1

3

I think you just need to adjust your submission handler; don't include the fieldset "options_settings". I use the getValue() method and pass it an array of values, the form key and the index of its value.

  if ($file_id = $form_state->getValue(['header_background', '0'])) {
    $file = \Drupal::entityTypeManager()->getStorage('file')->load($file_id);
    $file->setPermanent();
    $file->save();
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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