Drupal 6: Show Flags for Node Translations - drupal

For each node preview, I want to have little flag icons at the top representing the available translations. I have seen the language switcher code, but it outputs all the languages all the time. That is annoying because people will click their language and then find that the page is only available in English anyway (I have a site with many articles in a great variety of languages). I have seen this done though. I'm relatively new to Drupal programming. Can anyone give me a pointer?
Thanks!

Figured it out by myself, and noting it here because I know I'm not the only one with this issue.
The template I'm working off is called scaccarium, so I went to /themes/scaccarium/template.php and added the following function:
function scaccarium_preprocess_node(&$vars) {
$node = $vars['node'];
$translationlinks = array();
// Move translation links into separate variable
foreach ($node->links as $key => $value) {
if ($value['attributes']['class'] == 'translation-link') {
$translationlinks[$key] = $value;
// unset($vars['node']->links[$key]);
}
}
$vars['translationlinks'] = theme('links', $translationlinks, array('class' => 'links translationlinks inline'));
}
If your template is called something else, you should obviously go to a different folder and also change the first word of the function name. And if your theme comes with an existing _preprocess_node function, carefully modify it.
Then, I went to my template's node.tpl.php and I added
<?php if ($translationlinks) {
print $translationlinks;
} ?>
next to the title.
Then I had to clear the Drupal cache (even though caching was disabled!) in Performance > Caching in order to get this to work.
Done!
...
To add language links at the top of full nodes, I had to add another "print $translationlinks" at a different place in node.tpl.php as well, as the h3 title thing was just for node previews. Then, to remove the redundant language links at the bottom of full nodes, I tried that unset line that you see commented out in template.php - I found that it had no effect even though another website had recommended it. So what I ended up doing was using CSS for this, adding the following to my template's CSS file:
.node-links .translation-link {
display: none;
}
I hope that my experience will help someone else with the same problem.

Related

Displaying a Drupal content type field outside of a node

I have created a custom field on a content type in Drupal 7 which I need to display outside of the node, in a separate area on the page.
Similar to how the $title variable works (in which you can place this where you like in the page.tpl.php file) I would like to be able to create another variable called $subtitle which would call the data from the current node and allow me to print out the variable in an area on the page.tpl.php file.
I've seen a view examples seeming to use views and blocks to accomplish this task, but that seems a bit excessive and wondered if there was an easier way.
There is an easier way, you do need to bear in mind though that not every page is a node page, and not every node page will be of the right content type so you should be selective. Just add this to your theme's template.php file:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type == 'my_type') {
$vars['subtitle'] = $node->field_my_field[LANGUAGE_NONE][0]['value'];
}
}
Then in page.tpl.php you should do something like this:
if (isset($subtitle)) :
echo $subtitle;
endif;
Make sure you clear your caches (at admin/config/development/performance) once you've implemented the hook in template.php or Drupal won't pick it up.

Drupal7: Trying to theme a specific page using a preprocess function, but...I get a blank screen instead

I've just discovered that if you want to alter a specific page (or group of pages) all you need is to add templates file to the core templates. For instance, I need to theme my /helloword page using a page--helloworld.tpl.php and node--helloworld.tpl.php template files.
Now all I get is a blank screen so I tried to write a preprocess function that adds support for custom theme files like:
<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use doublehyphens in Drupal7.
$vars['template_files'][] = 'page--'. str_replace('_', '-', $vars['node']->type);
}
}
?>
I see no syntax error but I still get a blank screen. Still no luck
Is someone able to figure out what's wrong in the code/routine?
Drupal7 + Omega Sub-Theme
Kind Regards
I think there's a tiny bit of confusion here: a template file named node--type.tpl.php will automatically be called for any node which has the type type...you don't need to add the template suggestions in yourself.
There is one caveat to this, you have to copy the original node.tpl.php to your theme folder and clear your caches otherwise Drupal won't pick it up.
Also you don't want to use the phptemplate_ prefix...rather you want your function to be called MYTHEMENAME_preprocess_page.
Your code to add the page template based on the node type looks spot on, see if you still have the problem after you change your function name and clear the caches.
Hope that helps :)

Drupal - different images for different languages

I have to "i18n" an existing drupal installation and add a second language.
I have an image on the homepage that is defined as a 'background-image' in a CSS file.
The image contains text, thus I need to show different images depending if the URL is:
http://example.com/en/
or
http://example.com/es/
How can I show a different image on the homepage depending on the user language (url based)?
I am pretty new to Drupal, so please don't assume I know anything!!!
update: I was hoping that I could define a particular image as "EN" and add an "ES" alternative, have one URL to the image (that can be used in templates or css etc), but depending on the user language, Drupal would serve up the language specific version. This would be ideal.
update2: Or maybe another approach is possible - to create a new 'content type' that simply contains an image so that: http://example.com/node/23 returns the pure image binary (image/jpeg) - then this node could be internationalized/translated like other nodes. I'm really struggling to find the 'Drupal' way to i18n images...
update3: Does Drupal store the user language in the session? If so, I could just write my own script, read the session language and serve a language specific image right? and completely avoid Drupal (eg: http://example.com/i18n-image.php?img=logo - ugly, but you get the idea), if so, how does Drupal store the session user language?
You can use the internationalization module: http://drupal.org/project/i18n
and use the function "i18n_get_lang()" to get the current language.
Name your files depending on the language:
image_LANGUAGE.jpg
image_fr.jpg
image_es.jpg
To embed the images:
<img src="<?php echo base_path().path_to_theme().'/images/'; ?>image_<?php echo i18n_get_lang(); ?>.jpg" />
EDIT:
To implement something similar for the CSS files:
You can create various CSS files depending on the language:
styles.css
styles_fr.css
styles_es.css
Into the main file (styles.css) you can put all CSS that is not related to a specific language.
Into the other files you can put the CSS related to a specific language.
styles_fr.css:
div.item1 { background: url('../images/item1_fr.jpg'); }
styles_es.css:
div.item1 { background: url('../images/item1_es.jpg'); }
And then you create a preprocess function for the page in your template.php file that will include the specific CSS style sheet based on the current language:
function YOURTHEME_preprocess_page(&$vars, $hook){
drupal_add_css(path_to_theme().'/css/styles.css');
drupal_add_css(path_to_theme().'/css/styles_'.i18n_get_lang().'.css');
}
Or you can directly use drupal_add_css() into a template file.
I would go with hook_preprocess_page. For example, it can be a good idea to switch style sheets based on the language:
function foobar_hook_preprocess_page(&$variables, $hook) {
global $language;
if($language != 'en') {
$sheets = array();
foreach($variables['css']['all']['theme'] as $stylesheet => $value) {
if (strstr($stylesheet, path_to_theme())) {
$stylesheet = str_replace('.css', '-'. $language->language .'.css', $stylesheet);
}
$sheets[$stylesheet] = $value;
}
$vars['css']['all']['theme'] = $sheets;
$vars['styles'] = drupal_get_css($vars['css']);
}
}
For further explanations, see this blogpost: http://becircle.com/language_based_stylesheet_switching_your_theme
Just to extend your answers for Drupal 7: There's a global variable $languagethat provides a standard class containing many information about the current language, including $language->language with the language short code.
It's defined in modules/locale/locale.admin.inc.
So call
<?php
global $language;
print 'images/'.$language->language.'/image.png';
?>
in your theme and catch all the "$language->language is undefined" errors.

Select a template through the admin area?

Up to now, I've always hard coded what page template a certain page should use, either based on the URL, or what type of node it is.
What would be really useful is if there was a way to select which tpl file to use, right there in the node edit form. This would allow the user to flick between different page layouts at will.
Does anyone know a good way to approach this problem, or a flat out solution to it?
ThemeKey will let you load a theme based on a path or other criteria. You can use other methods like utilize preprocesser functions of template.php, and hook it in with hook_form_alter and come up with a way to switch files.
I ended up adding a new vocabulary for template files (the VID for this is 2 in my case) , and then rolled this into the page preprocessor in my template.php:
function phptemplate_preprocess_page(&$vars) {
if (count($vars[node]->taxonomy)>0)
foreach ($vars[node]->taxonomy as $term)
$template = $term->vid == 2 ? $term->name : NULL;
if ($template) $vars['template_files'][] = "template-".preg_replace("/[^a-zA-Z0-9s]/", "", strtolower($template));
}
Now if I have a node in a taxonomy term called: A Green Page! it will look for template-agreenpage.tpl.php as a template file.
I was wanting this functionality as well, so I made a module to do this very thing for node templates. You can find it here: http://drupal.org/project/template-picker

Drupal 6: Theme Developer gives too common candidate name, nothing specific

I'm working on a restaurant directory site. I have restaurant details page that I need to implement gmap, slideshow, etc. So I need a specific page.tpl and Theme Developer gives the info as below
alt text http://img.skitch.com/20100708-b9af98fb8mpckai5wfpmktxwgf.jpg
but site's about us, contact us, faq, etc pages has same candidate name as page-node.tpl.php . So I can't use that :/
Why isnt there anything like page-restaurant.tpl.php
how can I solve this? Appreciate helps so much! thanks a lot!
[SORTED]
<script type="text/javascript">
window.onload = function() {
load();
}
window.onunload = function() {
GUnload();
}
</script>'
If you are just wanting to add Javascript code, you can use the CSS class provided with most themes. For example, Zen will give you
<body class="not-front logged-in node-type-restaurant two-sidebars page-restaurant">...</body>
which you can detect with jQuery as
$(document).ready() { $('.page-restaurant').Myfunction(); }
Regarding slideshow, I've had good luck with Views Slideshow, which provides a block you can just place in a region on your page.
We had a similar question yesterday.
The logic of drupal is that the page should be for the common elements of a page accross all types of page and content types. Page is the top level template, if you are trying to change that per content type you may not have seperated your theme correctly. You have node theming for differentiating between the different types of node. The format is node-[NODE TYPE].tpl.php
Have you created a content type "restaurant"? If you do this, you may find modules like gmap will help you out.
Add the following function to your template.php file. Replace "themename" with the actual name of your theme. This assumes you've created a separate content type for "restaurant" - I gather you have from the previous answers and comments. You'll get an option to use page-restaurant.tpl.php once you upload the edited template.php file and refresh the theme registry.
function themename_preprocess(&$vars, $hook) {
switch ($hook){
case 'page':
// Add a content-type page template in second to last.
if ('node' == arg(0)) {
$node_template = array_pop($vars['template_files']);
$vars['template_files'][] = 'page-' . $vars['node']->type;
$vars['template_files'][] = $node_template;
}
break;
}
return $vars;
}

Resources