1

Hy, I'm trying to display an image which has been set in the theme settings. This file should be stored permanently. I'm using drupal 8 and based on the information I found online, this is what I got:

First of all I added the setting to the form with the following code (located in theme-settings.php):

function THEME_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id = NULL) {
    if (isset($form_id)) {
        return;
    }

    $form['default_avatar'] = array(
        '#type'          => 'managed_file',
        '#title'         => t('default avatar'),
        '#default_value' => theme_get_setting('default_avatar'),
        '#description'   => t("The default avatar for users."),
    );

    $form['#submit'][] = 'THEME_form_system_theme_settings_submit';

    // Get the current theme
    $active_theme = \Drupal::theme()->getActiveTheme()->getPath();
    $form_state->addBuildInfo('files', $active_theme . '/theme-settings.php');
}

Then I use the following submit hook (located in theme-settings.php):

function dwagulu_form_system_theme_settings_submit(&$form, $form_state) {
    $image_fid = $form_state->getValue('default_avatar');
    if (count($image_fid) > 0) {
        $image = file_load($image_fid[0]);
        if (is_object($image)) {
            // Check to make sure that the file is set to be permanent.
            if (!$image->isPermanent()) {
                $image->setPermanent();
                $image->save();
                // Add a reference to prevent warnings.
                $file_usage = \Drupal::service('file.usage');
                $file_usage->add($image, 'dwagulu', 'theme', 1);
            }
        }
    }
}

This seems correct, however, when I add upload an image and save the settings I get the following warnings:

Warning: Invalid argument supplied for foreach() in Drupal\Core\Form\FormCache->loadCachedFormState() (line 157 of core\lib\Drupal\Core\Form\FormCache.php).
Drupal\Core\Form\FormCache->loadCachedFormState('form-5FkI3Kf8XhoxscPOAf-slbJ7Nn--ubyPoCQoO5UaQhQ', Object) (Line: 122)
Drupal\Core\Form\FormCache->getCache('form-5FkI3Kf8XhoxscPOAf-slbJ7Nn--ubyPoCQoO5UaQhQ', Object) (Line: 432)
Drupal\Core\Form\FormBuilder->getCache('form-5FkI3Kf8XhoxscPOAf-slbJ7Nn--ubyPoCQoO5UaQhQ', Object) (Line: 257)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 139)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'dwagulu_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).
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 583)
Drupal\Core\Form\FormBuilder->processForm('system_theme_settings', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 139)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

This second warning would lead me to believe that the submit function is not correctly loaded, despite the fact that I added it to the build_info of the form_state. Does anyone have any suggestions on what I might be missing.

Kind regards.

// edit:

I continues experimenting with the code. I moved both functions "alter" and "submit" to the THEME.theme file. Then I changed the build_info to include the THEME.theme file. This removed the warnings and I can see that my code is executed. The file's status is set to permanent. This is good, however, when I ask for the file, the uri is "temporary://default-avatar_2.png" which is not permanent I assume?

1

By adding an upload location to the form field:

$form['default_avatar'] = array(
    '#type'            => 'managed_file',
    '#title'           => t('default avatar'),
    '#default_value'   => theme_get_setting('default_avatar'),
    '#description'     => t("The default avatar for users."),
    '#upload_location' => 'public://'
);

the file is no longer uploaded to a temporary url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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