I have to change the template in one of Drupal 6 project.
On every "page" node type is stored an image in the CCK field. I have made new design template to change the the design through CSS. But now the customer demand is to change the images that are in CCK field named "field_obr" (one per node) only in my new design template. That he can switch the themes and the images will correspond with themes (current design = original images from CCK, new design = images from altered path from original CCK field path). Images names can be the same, only the directory changed.
Original images are stored in folder like:
sites/default/files/image1.jpg
sites/default/files/image2.jpg
...
I want to change in my new theme the path to an image to:
sites/default/files/new_img/image1.jpg
sites/default/files/new_img/image2.jpg
...
= to add the "/new_img" folder in the path before the image. (Or later path to my template file - thac I know how to, but I'm looking here for the principle.)
Template outputs the image as:
<img class="imagefield imagefield-field_obr" width="229" height="346" alt="" src="/sites/default/files/radek_home_0.jpg?1354577633" />
I have fond this solutions:
First solution - change $content output through preg_replace()
In the "node-page.tpl.php" to look in the $content throug preg_replace() function, find the path and change/add the string as I wish. - This is doable, but i feel that is not optimal solution to Drupal-flow.
Second solution - using hook_page_alter() or hook_page_build() functions
I have fount that the content of variables can be altered through using hook_page_alter() and hook_page_build() functions.
I put these functions to my "template.php" before phptemplate_preprocess_page(&$vars) function, bud it seems that they do nothing:
function hook_page_build(&$page) {
// print_r($page);
$page['node']->field_obr['0']['filepath'] = "sites/default/files/new_img/radek_home_0.jpg";
echo "Test function hook_page_build() has run";
}
Than i found in the docu, that these functions are for Drupal 7+ :-(
Question: Can anyone explain me the best solution tho solve the situation described in the beginning? How to achieve it through the profi "Drupal way?" Thank you. Frantisek
I have made some research and for Drupal 6 found single easy working solution for the described problem. It is the mentioned PHP preg_replace() function located in the template page.tpl.php
:
print preg_replace('#/sites/default/files/#', '/sites/default/files/_new/', $content);