How do I dynamically change ubercart's default messages from my module - drupal

There are some products for which I would like to have a special checkout complete message that would differ from the default.
I know that I could probably do this by changing the default redirect page to something else. [And I'm not sure if that would introduce another problem]
However, I would like to know if there is a standard/better way of approaching this problem.
Thanks!,
D

Consider the String Overrides module. Here is a quote about it (from the module's project page):
Provides a quick and easy way to replace any text on the site.
Features:
Easily replace anything that's passed through t()
Locale support, allowing you to override strings in any language
Ability to import/export *.po files, for easy migration from the Locale module
Note that this is not a replacement to Locale as having thousands of overrides can cause more pain then benefit. Use this only if you need a few easy text changes.

I guess the only other possible way of doing what I am thinking of is to override the theme function that ubercart uses to display the message. And this probably seems like it would make the most sense.
In this case I would override theme_uc_cart_complete_sale
I could set a
$_SESSION['is_special_product'] == TRUE;
and then set $message to my $special_message if it's been set.
if ($_SESSION['special_product']) {
$special_message = t('This is my special message');
$message = variable_get('special_product_message', $special_message;
}
Finally, to override from my module I will need to hook into the pre-process hook:
function $modulename_prepocess_$hook(&$message) {
if ($_SESSION['special_product']) {
$special_message = t('This is my special message');
$message = variable_get('special_product_message', $special_message;
}
}
It is important to note that it is not enough to have this function in your module. The preprocess functions are only invoked when the template file that overrides the theme function is called.
More details can be found at http://drupal.org/node/223430

Related

Set PHP Variables for Drupal 7 Theme Files

I want to set custom php variables that can be used throughout my drupal theme (html.tpl.php, page.tpl.php etc.) I need the variables set based on the $title of the node. I'm not an expert on how Drupal works, the moduling and hooks, and I just need to know the simplest/easiest way to accomplish this. I keep reading about implementing some sort of hook in the template.php file where you can set variables, but I've been unsuccesful with everything I've tried.
So, basically, how would you accomplish this:
Get $title of Node
Set variables that will be passed along into theme files (for example, to do basic things like: if($title == 'news_page') { $siteSection = "news"; } )
Have $siteSection be available to use in theme files
Any help would be great.. thanks!
Before Drupal builds the HTML for a page from a theme's template (.tpl.php file), it runs preprocess "hooks". Hooks are basically a naming convention for functions that let modules and themes override or "hook" onto Drupal core processes.
E.g., if you want to display a message to a user when they log in, you can use hook_user_login.
function MODULENAME_user_login(&$edit, $account) {
drupal_set_message("Welcome, ". $account->name);
}
When a user logs in, Drupal looks for all loaded functions that end in "_user_login" and it runs them. If this function is in an enabled module, it has been loaded, so it will get run as well.
If you want to make a variable named $site_section available in your page.tpl.php file, you can hook into template_preprocess_page. This is a theme hook, so the name is a little different, but it functions pretty much the same way. To call this hook from your theme, you need to create a file called template.php in your theme's directory. Inside template.php, we'll add:
<?php
function THEMENAME_preprocess_page(&$vars){
switch (drupal_strtolower($vars['node']->title)) {
case "about page":
$site_section = "about";
break;
case "news page":
case "news page1":
case "news page2":
$site_section = "news";
break;
default:
$site_section = "none";
break;
}
$vars['site_section'] = $site_section;
}
The <?php is used to tell the server the treat all of the proceeding code as PHP. We then declare our hook function with the intention of loading Drupal's array of page variables into a local variable called $vars. By adding the & before $vars, we'll be allowed to modify the values for use outside of this function.
The switch statement will let us efficiently test the page title for multiple values. The value of the node's title may contain uppercase letters, lowercase letters, and symbols, so to avoid a case-sensitive mismatch, we're going to convert the title to lowercase and only test that (symbols will still be in the title, though). After the switch statement, we set the value of our $site_section local value into the referenced $vars array for use in page.tpl.php.
However, if it's just your intention to break the site up into sections for theming purposes, there are other ways of accomplishing that. My answer to a similar situation a few months ago might be helpful.

Drupal Panel Pages Pathauto

I have a Panel Page set up with the path node/%node/foo and all works fine when I visit a link such as node/6/foo. However, when I visit nodealias/foo it doesn't work at all. Is it possible to get panels to work with pathauto in this way?
I am thinking I may have to implement the hook hook_url_inbound_alter and change the url myself.
I also posted a support request in the panels module here: http://drupal.org/node/1219796
As Alexey answers panels doesn't care about aliases, it only sees node/%nid
Here's a good explanation that is valid still for D7:
http://drupal.org/node/211338
To summarize and bring it up to date for D7:
Export your variant for the panel you've created and import it into the panel that overrides the default node display in Drupal.
Add Criterias to the variant so the Panel/variant is only used for the type(s) of content you want to display with this variant.
Voila :) (read the discussion at the link, else the summary will be difficult to understand)
Hope this helps - I myself have spend some time googling and trying to understand this, and I was also confused by the fact that Views DOES care about aliases...
I fixed this using the following code, you would need to alter the pattern to match the pattern of your url aliases and alter the function name to match your module's name.
function brooklands_url_inbound_alter(&$path, $original_path, $path_language) {
$pattern = '#^works\/[A-Za-z0-9]+(-[A-Za-z0-9]+)*\/images(\/\d+)?$#';
if(preg_match($pattern, $original_path)) {
$snip = substr($original_path, 0, strrpos($original_path, '/images'));
$system_path = drupal_lookup_path('source', $snip);
if($system_path) {
$tail = substr($original_path, strrpos($original_path, '/images'));
$path = $system_path . $tail;
}
}
}
You can use this module Subpathauto
it automatically makes the alias to work with subpaths, such as: nodealias/foo
The nodealias is the full alias of your node with nid=6. The third argument (foo) is added through hook_menu() by panels module to the exact alias (node/%nid/%anythingelse) and it is NOT applied to your aliased URL, so you can not use nodealias/foo url to access your panel just because it is not 'hooked' by panels module.
Changing url manually is a good idea, I think.

How to change the label of the default value (-Any-) of an exposed filter in Drupal Views?

I created a view which has three exposed filters. Everything works fine except the fact that I can neither translate or change the default string (-Any-) for the dropdowns. Is there a way to change this string to something more meaningful like "Please Select" and make it translatable so the German version displays "Bitte wählen"? I have two screen captures that may be helpful:
and
A further improvement would be the ability to change the text "any" to something like "please select a (field name here)" but I am losing hope for that =)
UPDATE
IMPORTANT: On further testing, I found that if you choose to display "-Any-" from "admin/build/views/tools", then THAT IS translatable.
For anyone who wants to just change the value of "- Any -" to something in particular then use a custom module to override that looks like this:
function yourmodulename_form_alter(&$form, $form_state, $form_id) {
if($form_state['view']->name == 'your_view_name_here') {
$form['your_dropdown_name']['#options']['All'] = t('- Type -'); // overrides <All> on the dropdown
}
}
The reason you might want to do this is if you have 3 (for example) dropdowns for 3 separate fields. Then having on them wouldn't be very useful for a user (especially if you are not using labels).
In the code above just remember to change "yourmodulename" to the name of your module.
your_view_name_here should be the name of your view (replace dashes with underscores - for example "property-search-bar" would become "property_search_bar")
And change "your_dropdown_name" to the field name - I found this by using dsm($form) with the devel module installed and enabled. This is usually the field name of your drop down so it might be something like "field_my_custom_value".
Hope this helps anyone who needs it!
Three options:
You could change it with localisation, if you have that enabled already. Introducing localisation only for this string is far too much overhead.
You can change it with a form_alter, if you already alter the form anyway. Introducing a module with a hook_form alter for just one string is way too much (maintainance and performance) overhead.
You coud change it with a simple string override in your settings.php
In Drupal 7 (Drupal6 differs in details only)
/**
* String overrides:
*
* To override specific strings on your site with or without enabling locale
* module, add an entry to this list. This functionality allows you to change
* a small number of your site's default English language interface strings.
*
* Remove the leading hash signs to enable.
*/
$conf['locale_custom_strings_en'][''] = array(
'<Any>' => 'Whatever!',
);
Note though, that this will change every occurrance of the full string <Any> (case sensitive) to Whatever, not just the ones in that single form.
Views exposed filter label is not translatable in D6.
Go to Administer > Site building > Views and select tab tools.
Replace 'Label for "Any" value on optional single-select exposed filters: ' by the translatable '- Any -'.
Important: visit the views with exposed filters in at least one language which isn't your default language.
Then you can translate "- Any -" through Aminister > Site building > Translate interface (case sensitive).
Or you can simply use a line of jQuery code like this:
$(document).ready(function(){
$("#views-exposed-form-url-name-display-name #edit-tid-all a").text("All");
});
The Better Exposed Filter module allows you to change the "-any-" label in a Views exposed filter.
I'd rather go with the simple solution: String Overrides.
With this you simply add a string you want to change on your site, and replace it with anything you want (Strings of course).
May be module https://www.drupal.org/project/views_advanced_labels helps?
I found it, but have not tried it yet.
if ($form['#id'] == 'views-exposed-form-project-search-block-project-search') {
foreach ($form['#info'] as $filter_info) {
$filter = $filter_info['value'];
if ($form[$filter]['#type'] == 'select') {
$form[$filter]['#options']['All'] = $filter_info['label'];
}
}
}
If you use Better Exposed Filters module, go into Exposed Form > Exposed form style: Better Exposed Filters | Settings > look for your field > Advanced Filter Options > put "- Any -|All" in the "Rewrite the text displayed" field.

How do you use a view with arguments as the site front page in Drupal?

I have a Drupal site and I have setup a view to power the front page.
My goal is to be able to pass 0-2 arguments to the home page, that get passed into the view. However, I still need the normal Drupal pages to work. The list of arguments is known.
For example:
mysite.com/berlin/birds would pass in "berlin" as the first argument and "birds" as the second argument to the view that powers the front page.
mysite.com/berlin would just pass in one argument, "berlin"
mysite.com/admin would load the normal admin pages in Drupal
I'm not clear on how to achieve this. Is there a hook I can use? I can't find one or think of one. Is there a way to specify this in the argument for the view itself? Perhaps I can write a hook that interjects when the URL is being loaded, and rewrite in the background?
The solution I currently have is to add these paths (since my arguments are known) to the menu system. This works, except that when I the pages they aren't the front page, so the pages don't use the node themes I want (they use the node details theme).
I don't think you can really do this without custom code. The View needs a path before it starts taking arguments, and your URLs start with the arguments. What you can do is fake it with custom code. I've used something like this:
/**
* Implements hook_init().
*/
function mymodule_custom_init() {
$item = menu_get_item(); // Matching Drupal path
if (!$item) { // There is no matching Drupal path.
$_GET['q'] = 'view_path/' . $_GET['q']; // Fake path path.
} // if
} // mymodule_custom_init
Then you give the view a path of "view_path" and it responds to everything that doesn't match anything else in Drupal's paths.
There is a spot in the views UI for "Argument handling code" that takes a small snippet of php - http://drupal.org/node/70145
You could run some code there that checks to see if you are on the front page (or arguments are not present or whatever)and insert the arguments you want.
Another way is to set arguments via a hook_views_pre_view or hook_views_pre_build. Is better because you are sure you don't break other stuff (like another view block).
function MYMODULE_views_pre_view(&$view){
if ($view->name == 'your_view_name' && drupal_is_front_page()) {
$view->set_arguments(array('you_argument','you_second_argument'));
}
}

Drupal best way to change text

I'm using the module "simplenews" and during the registration process it reads "Select the newsletter(s) to which you wish to subscribe.".
I'd like to change this line and wanted to ask what the best way is to do so without hard coding it?
Thanks in advance!
I've also done this with the custom english version just so I could override a few strings. It works and is easier than hook_form_alter, but it also creates a bit of unnecessary overhead, especially if your site is otherwise not localized/multilingual.
There is nowadays a light and easy solution, that uses the same locale system, but you don't need to setup a custom english language or code anything: the "String Overrides"-module.
http://drupalmodules.com/module/string-overrides
Surprisingly, it's one of the top rated modules, out of over 5000 module.
Since it's located in a form, an easy and quick solution would be to change it with hook_form_alter.
Another solution would be to create your own custom version of the language english in Drupal and then "translate" the string into something different.
You could also skip the translation part, and use your settings.php file. You can create some custom string override in it without enableling the locale module. From the default.settings.php file:
/**
* String overrides:
*
* To override specific strings on your site with or without enabling locale
* module, add an entry to this list. This functionality allows you to change
* a small number of your site's default English language interface strings.
*
* Remove the leading hash signs to enable.
*/
# $conf['locale_custom_strings_en'] = array(
# 'forum' => 'Discussion board',
# '#count min' => '#count minutes',
# );
Or you can use String Overrides. Some more details about this module (from its project page):
Provides a quick and easy way to replace any text on the site.
Features
Easily replace anything that's passed through t()
Locale support, allowing you to override strings in any language
Ability to import/export *.po files, for easy migration from the Locale module
Note that this is not a replacement to Locale as having thousands of overrides can cause more pain then benefit. Use this only if you need a few easy text changes.

Resources