0

I found this solution here:

https://www.drupal.org/forum/support/module-development-and-code-questions/2010-12-25/add-custom-variable-to-pagetplphp

I created a module for give possibility to some users to change the background file from the UI.

In my module:

function office_file_upload_preprocess_page(&$vars)
{
    $vars['background'] = "BK2.jpg";
}

In my

page.tpl.php

Now how can I modify permanently after this function

$upload = file_save_upload('upload_item', $validators, $location, $replace = FILE_EXISTS_REPLACE);

the above variable to change the background?

I tried

variable_set($vars['background'], $upload->filename)

but no luck!

This is the form

enter image description here

and this is the module:

https://kopy.io/kOW3s

  • Hey hey, your request is a little bit too broad actually. Basically you might consider 1) to create a custom config (settings) page 2) where you offer radios to choose an image and 3) then store that value into a variable, 4) to later retrieve the value inside a preprocess function 5) to pass it over to a template. There are quite a bunch of tutorials out there. And just to clarify you are using D7, are you? – leymannx May 30 '18 at 13:13
  • Hi, thanks for your comment! I'm on D7 and I uploaded the form and the code of my module. You're right I'm a bit broad but I'm actually confused: if I put my preprocess in template.php, anytime the file is called the variable will be reset to the original value, isn't it? So I put the preprocess function in the module but I'm stuck on variable_get that doesn't work :( – Ponzio Pilato May 30 '18 at 14:16
  • 1
    In your submit function you miss to actually set the variable, p.e. variable_set('background_file', $file_id). By that it get's stored in the variables table where you can retrieve it with p.e. $file_id = variable_get('background_file', NULL). Plus you have to ensure this happens on a per-user basis. – leymannx May 30 '18 at 14:23
  • Thanks a lot! I remove my preproces function and add a variable_set() in my callback and...magic!! – Ponzio Pilato May 31 '18 at 12:44

Your Answer

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

Browse other questions tagged or ask your own question.