Skip to content

Instantly share code, notes, and snippets.

@somatonic
Created March 13, 2014 18:50
Show Gist options
  • Save somatonic/9534415 to your computer and use it in GitHub Desktop.
Save somatonic/9534415 to your computer and use it in GitHub Desktop.
Adds a method to get lanugage label from a field with $fields->getLangLabel("body")
<?php
/**
* ProcessWire module
*
* Example Fields Language Label Helper
* Gets the language value of a field label for the current user's language
*
* Once Installed it will add a new method to fields
*
* Usage in templates
* $label = $fields->getLangLabel('body');
*
* Usage in modules
* $label = wire("fields")->getLanguageLabel('body');
*
*/
class HelperFieldsLanguageLabel extends WireData implements Module {
/**
* getModuleInfo is a method required by all modules to tell ProcessWire about them
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'Fields Language Label Helper',
'version' => 100,
'summary' => 'Adds a method to get language label from a field. $fields->getLangLabel(\'fieldname\')',
'singular' => true,
'autoload' => true,
);
}
public function init() {}
/**
* when API and page to render is fully ready
*/
public function ready() {
// add a new method to fields to easily get a language value of a field label
$this->fields->addHook("getLangLabel", $this, "getLabelLang");
}
/**
* return language version of a label from a field
*/
public function getLabelLang($event){
$field = $event->arguments(0); // field name
$lang = wire("user")->language->isDefault() ? "" : wire("user")->language->id;
$event->return = wire("fields")->get($field)->get("label$lang");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment