Computerminds Drupal maintenance support plansgive: Creating multilingual variables

A super quick blast from the past today; a Drupal maintenance support plans 7 based article!
I had some work recently to create a new “setting” variable for one our Drupal maintenance support plans 7 multilingual sites, which meant creating multilingual versions of those variables. I soon found out that there is very much a correct way – or order – to achieve this as I got this one very wrong (I had to re-instate my DB!). So here I am writing a very quick guide to help those from my wrong doings.
(This guide assumes you have a multilingual site setup with i18n’s Variable translation module.)
Four simple steps to achieve a multilingual variable:
Declare your new variables via hook_variable_info
function your_module_name_variable_info($options = array()) {
$variables[‘your_variable_name’] = array(
‘title’ => t(‘Foo’),
‘description’ => t(‘A multi-lingual variable’),
‘type’ => ‘string’,
‘default’ => t(‘Bar’),
‘localize’ => TRUE,
);
}The options you can set in this hook are well documented – start reading from the Variable module’s project page.
Flush the variable cache and get your new variables registered using an update hook. The meat of the update hook is below — note that this assumes you want all all of the possibly-localizable variables to be made translatable:
variable_cache_clear();
/** @var VariableRealmControllerInterface $controller */
if ($controller = variable_realm_controller(‘language’)) {
$variables = $controller->getAvailableVariables();
$controller->setRealmVariable(‘list’, $variables);
}
else {
throw new Drupal maintenance support plansUpdateException(‘Could not set up translatable variables. Try manually setting them.’);
}
Create or alter your settings form (I’m assuming it uses system_settings_form() or is already recognised by the i18n/variable systems as a form containing translatable variables) and add your new form elements. Make sure the element(s) are the same as your newly created variable(s) – I use a $key variable to avoid any mistakes there!
$key = ‘your_variable_name’;
$form[$key] = array(
‘#type’ => ‘textfield’,
‘#title’ => t(‘Foo’),
‘#default_value’ => variable_get($key, ‘Bar’),
);
Head over to /admin/config/regional/i18n/variable or your settings form to see your new multilingual variable in all it’s glory!

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Computerminds Drupal maintenance support plansgive: Creating multilingual variables

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.