I have a multi-language drupal setup (2 languages, english default). I want users to receive always content in the other language (lets say spanish) on initial page request, but keep english as default language for future language switch. So users will be redirected on initial load to site.com/es, but through the language switch will be able to go to site.com/ (which is english).
Any suggestions? Thank you.
(Apache, PHP)
Redirect users using preprocess in template.php file of your theme:
Approximate code:
/**
* Override or insert variables into the page templates.
*
* #param $vars
* An array of variables to pass to the theme template.
* #param $hook
* The name of the template being rendered ("page" in this case.)
*/
function THEMENAME_preprocess_page(&$vars, $hook) {
global $language;
if ($language->language == 'en') { // Add here some checking for page, see print_r($vars)
drupal_goto(url().'/es/'.$GET['q']); //goto es version
}
}
Related
I was asked to help a friend with virtual product download issues with their WooCommerce site. I solved the problem in the short term by moving them off their Hostgator shared hosting to a dedicated VPS through Digital Ocean (DO). However, I need to take this one step further as storing their files on droplets is going to get expensive really fast.
What I am wanting to do is use DOs Spaces, which are effectively S3 buckets. The tests we have done suggest this will be a really good setup as the storage space is very cheap compared to droplets, and we can use a CDN feature. Unfortunately, I am having issues working out how to best integrate WooCommerce digital downloads with Spaces. WooCommerce provides three download options:
The first two seem to proxy the file through your webserver to hide the origin URL
The third option 'Redirect (insecure)' updates the database to show you've downloaded the product, and then redirects you to a static link.
I want to use the redirect option and override the plugin function woocommerce_download_file_redirect with my own code that would use DOs API to generate a pre-signed download URL for a protected resource that is valid for no more than 60 seconds, thus protecting the download from unauthorized users.
After looking through the documentation, I came across the function which handles the redirect, and on line 25 of the same class the line of code that registered that action. Knowing about these functions I wrote a simple plugin to test overriding that code
<?php
/**
* plugin meta here (name, uri, description etc)
*/
function download_redirect_override() {
var_dump('test to see if my code works');
// Once I get this to work, this method will take the product URL
// explode it on '/' to get the parts required to make an API call to Spaces
// to generate a pre-signed URL with a 60-second life-span
// this URL will then be returned to the user using Header('Location: '.$url); die;
}
add_action( 'init', function() {
global $WC_Download_Handler;
// remove the default action so I can replace it with my one that generates short-life download URLs
remove_action( 'woocommerce_download_file_redirect', array( $WC_Download_Handler, 'download_file_redirect' ), 10, 2 );
add_action( 'woocommerce_download_file_redirect', 'download_redirect_override', 10, 2 );
});
Once my plugin is enabled, I try hitting the download button and WooCommerce ignores my code continuing to use the default function. I know my add_action( 'init', ..) is getting called because I have inserted a var_dump('test'); in the function and that prints to my screen.
I am not a PHP developer so I don't fully understand the nuances of Word Press. I am hoping someone could either point out what I am doing wrong or point me in the right direction. Thanks.
After taking a break for a few days, I spent last night and finally got it working.
While the code below works, I may well be doing it wrong so I am still open to feedback. Here is my code:
The function `spaces_download` is in another file, I have not posted it below as it is not relevant, however, it ends with a Header('Location: '.$Url); Exit();
<?php
/**
* Plugin Name: <redacted>
* Plugin URI: <redacted>
* Description: <redacted>
* Version: 0.0.1
* Author: <redacted>
* Author URI: <redacted>
*/
add_action('plugins_loaded', function() {
// Override the default Woo Commerce function that handles the redirect download
// The third arg, '1', is the priority, we use this to override the default
add_action( 'woocommerce_download_file_redirect', 'spaces_download', 1, 1 );
}
I am developing a project in Symfony 4.3. I am trying to create URLs in different languages. I have tried to use this notation:
/**
* #Route({
* "it": "/{_locale}/rassegna/stampa",
* "bg": "/{_locale}/Статии/в/печата"
* }, name="rassegna_stampa", utf8=true)
*/
The problem is that when I try to create a link in a twig to change language, for example:
GO TO BG LANGUAGE
it is rendered in something like:
http://website.it/it/%D0%A1%D1%82%D0%B0%D1%82%D0%B8%D0%B8/%D0%B2/%D0%BF%D0%B5%D1%87%D0%B0%D1%82%D0%B0
So it seems that the translated final part is ok, but the _locale parameter is not matched, as the {_locale} placeholder is filled with the current locale, and not with the locale that I requested in the link. It is 'it' but it should be 'bg'.
I'm using Loco Translate translation plugin with my Wordpress 4.7 installation.
I have my MU-Plugin correctly registered and properly configured to load its text domain.
Yet Loco Translate only manages to recognize themes and regular plugins, therefore I cannot use Loco Translate UI for translating my plugin.
Help will be appreciated.
Thanks
Welcome to stack overflow Jonathan Darchy!
Here is an example of adding an unregistered MU plugin to Loco Translate
Raw, taken from this gist
I think it answers your question:
<?php
/**
* MU plugins inside directories are not returned in `get_mu_plugins`.
* This filter modifies the array obtained from Wordpress when Loco grabs it.
*
* Note that this filter only runs once per script execution, because the value is cached.
* Define the function *before* Loco Translate plugin is even included by WP.
*/
function add_unregistered_plugins_to_loco( array $plugins ){
// we know the plugin by this handle, even if WordPress doesn't
$handle = 'foo-bar/foo-bar.php';
// fetch the plugin's meta data from the would-be plugin file
$data = get_plugin_data( trailingslashit(WPMU_PLUGIN_DIR).$handle );
// extra requirement of Loco - $handle must be resolvable to full path
$data['basedir'] = WPMU_PLUGIN_DIR;
// add to array and return back to Loco Translate
$plugins[$handle] = $data;
return $plugins;
}
add_filter('loco_plugins_data', 'add_unregistered_plugins_to_loco', 10, 1 );
In the contributed module (ldap_sso.module) a custom drupal_set_message is being declared.
drupal_set_message(theme('ldap_authentication_message_not_authenticated',
array('message' =>
t('You were not authenticated by the server.
You may log in with your credentials below.')
)
), 'error');
How can i override this message or possible unset it via my custom module so it doesn't get called at all when LDAP SSO authentication fails?
If you look at the code of drupal_set_message() at https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_set_message/7, you will notice that a message gets set only if the $message parameter is not null. So if we somehow set the $message parameter to be NULL, then no message will be set.
Fortunately since the LDAP module is using a theme function, this is easy. Define a theme function in the template.php file of the theme that you are using. Suppose you are using a theme called "mytheme". Then open the template.php file in that folder and add the following code:
/**
* Implements theme_ldap_authentication_message_not_authenticated().
*/
function mytheme_ldap_authentication_message_not_authenticated(&$vars) {
return NULL;
}
Clear the cache. Now theme('ldap_authentication_message_not_authenticated', …) will call the theme function that you have defined rather than the one defined by the LDAP module. Since your theme functions returns NULL, message will not be set.
Neerav Mehta
Drupal development in Bay Area
I know this is probably a n00b question but I've searched everywhere for an answer and havent found anything.
I have a CCK multiple value field for "Features" where a product can have a random number of multiple features entered for it. I am editing the view so I can style the output of the features on the product page.
Right now in my view I can output the entire list of features at once using:
<?php print $fields['field_features_value']->content ?>
This will give me a list all the features given for a product. But what I want to do is loop through and pull out each individual feature and format/style it separately. How exactly would I do this?
I ran into this yesterday again, and spent hours trying to Google the syntax, to no avail.
I was able to get this to work, but I must admit it is not the best way. It is duplicating some of the work that Views has already potentially done for us, and should be considered a brute-force approach.
My use case involved theming each filefield file in a node separately, per node-based row:
<?php
// $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $
/**
* This template is used to print a single field in a view. It is not
* actually used in default Views, as this is registered as a theme
* function which has better performance. For single overrides, the
* template is perfectly okay.
*
* Variables available:
* - $view: The view object
* - $field: The field handler object that can process the input
* - $row: The raw SQL result that can be used
* - $output: The processed output that will normally be used.
*
* When fetching output from the $row, this construct should be used:
* $data = $row->{$field->field_alias}
*
* The above will guarantee that you'll always get the correct data,
* regardless of any changes in the aliasing that might happen if
* the view is modified.
*/
?>
<?php
$output = explode('|', $output); // I've rewritten the field output in Views like this: [field_portfolio_image-field_name]|[nid]
$paths = $output[0]; // I've set filefield to show file paths rather than the file
$nid = $output[1]; // The NID is all that's really needed for this approach
$node = node_load($nid);
$slots = $node->field_portfolio_image;
foreach($slots as $prop) {
print ''.$prop[data][description].'';
}
?>
I used the Devel module heavily here (image reference for this example attached), in order to get the nested values I needed.
I know there is a better, proper way of doing this, rather than reloading the node data, since views should already have access to this upon page load.
When theming views is too specific I set conditions, relationships, parameters and all the views stuff except fields. The only field I use is node id.
Then when doing the theming I use...
$node = node_load($nid);
... to get the node object. You can inspect the node object with the dpm function that comes with the devel module.
dpm($node);
This "technique" works well for nodes and when you don't care about optimization or speed, because if you are going to do this with 1000 nodes you should load the nodes on batch.