SilverStripe change field label from parent class - php

Within SiteConfig there is a TextField Site title. I'm trying to change the label of this textfield $titleField through a SiteConfig extension in class SiteConfigExtension extends DataExtension.
Here is the code from siteconfig/code/model/ where it's created:
$fields = new FieldList(
new TabSet("Root",
$tabMain = new Tab('Main',
$titleField = new TextField("Title", _t('SiteConfig.SITETITLE', "Site title")),
....
Q: What's the easiest way to replace the SiteTitle label without having to remove the field in the SiteConfig extension and re-add it again with the desired label?

You could make use of the implemented _t() function. Put following in your mysite/lang/{LANG_CODE}.yml file:
{LANG_CODE}:
SiteConfig:
SITETITLE: 'My new title'
Replace {LANG_CODE} with the admin language(s) being used (eg sv for Swedish, or en for English). Keeping your strings separated from the code comes with many benefits. Remember to run ?flush after updating YAML files.
https://docs.silverstripe.org/en/3.4/developer_guides/i18n/

Updating the title from SiteConfigExtension uses updateCMSFields...
class SiteConfigExtension extends DataExtension {
public function updateCMSFields(FieldList $fields) {
if ($titleField = $fields->dataFieldByName('Title'))
$titleField->setTitle('my title');
}
}

Related

Magento How to use translation on attribute label?

I use Magento and I need help to use in frontend the translation of an attribute label !
I use this on my marketplace.phtml to load the attribute label
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'etat_verres');
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) { if($instance['label']) {
echo $instance['value'] // to show the value
echo $instance["label"] // to show the label
} }
?>
The problem is that Magento use Admin Value and not french value or english value.
Thanks in advance !
Sincerely yours,
John
Using the $this->__ is the core Magento helper which also contains the translation functionality, now assuming your module has extends the core Magento helper this will work. Now you can use a theme translate.csv file to translate your text.
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'etat_verres');
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach($allOptions as $instance)
{
if ($instance['label'])
{
echo $this->__($instance['value']) // to show the value
echo $this->__($instance["label"]) // to show the label
}
}
Also keep in mind, translating the "labels" should not be necessary as you can use the Magento admin store view translations within Catalog->Manage Attributes Select your attribute and manage attributes per store view, however, I'm not entirely sure of the context. While what I provided should work, having context will provide a better solution.

$this->l("Descripcion del modulo") return empty string

I am creating a new module for prestashop.
I am using the prestashop guide and when I try to set this:
class MiModulo extends Module{
public function __construct(){
$this->name = "mimodulo";
$this->tab = "front_office_features";
$this->version = "1.0.0";
$this->author = "Ivan Javier Barranco Gavilan";
$this->need_instance = 0;
$this->ps_versions_compliancy = array("min" => "1.6", "max" => _PS_VERSION_ );
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l("Mi Modulo");
$this->description = $this->l("Descripcion del modulo");
$this->confirmUninstall = $this->l("¿Esta seguro de que desea desinstalar el modulo?");
if(!Configuration::get("mimodulo"))
$this->warning = $this->l("Sin nombre proporcionado");
}
}
::l(), according to prestashop it is a function that translates text strings, but not where it should introduce these translations.
$this->l() always returns an empty string.
I have an installation of prestashop in spanish.
How I should make the translations correctly? Thanks!
Go to the "Translations" page under the "Localization" menu, In the "Modify translations" drop-down menu, choose "Installed modules translations", Choose the language you want to translate the module into.
The destination language must already be installed to enable translation in it. Click the "Modify" button.
The page that loads displays all the strings for all the currently-installed modules. Modules that have all their strings already translated have their fieldset closed, whereas if at least one string is missing in a module's translation, its fieldset is expanded.
In order to translate your module's strings (the ones that were "marked" using the l() method), simply find your module in the list (use the browser's in-page search), and fill the empty fields.
Once all strings for your module are correctly translated, click on either the "Save and stay" button or the "Save" button at the bottom of your list of strings.
PrestaShop then saves the translations in a new file, named using the languageCode.php format (for instance, /mymodule/fr.php).
http://doc.prestashop.com/display/PS16/Module+translation

joomla 3.x tags in custom component fails

Running Joomla 3.3.0-dev
I'm following the info posted here about adding tag support to a third-party component.
I've added the content type to the #__content_types table and modified my table file like this:
class MycomponentTableElement extends JTable
{
public $tagsHelper = null; // failed when protected and public
public function __construct(&$_db)
{
parent::__construct('#__mycomponent', 'id', $_db);
// Add Joomla tags
JObserverMapper::addObserverClassToClass('JTableObserverTags', 'MycomponentTableElement', array('typeAlias' => 'com_mycomponent.element'));
//$this->_observers = new JObserverUpdater($this); JObserverMapper::attachAllObservers($this); // failed with or without this line
}
I added the tag field in the edit template, and it worked fine-- but when I save an object I get the following error:
Save failed with the following error: Unknown column 'tagsHelper' in 'field list'
What am I missing? There's no other steps (besides front-end steps!) that are mentioned. It seems like I need to modify the model but that info is not applicable.
Thanks
"This Page Needs Copy Editing" and it's really true!
I also follow initial steps as described in page
Register a 'Content type' for the extension view(s)
Add 'Observer methods' to the extension table class(es)
Add 'Tag fields' to the extension edit forms
But to make field tag works on custom extensions I need to explicit set form field value in view file of backend:
$tagsHelper = new JHelperTags;
$this->form= $this->get('Form');
$this->form->setValue('tags', null, $tagsHelper->getTagIds( $this->item->id, 'com_custom.viewname') );
in this way on edit page all seems to work correctly.. surely exist better and more clean method, but until doc page will not be updated, this can help someone!
1- Add tag field to your xml form file or edit template file
2- Modify #__content_types table file:
function __construct(&$db)
{
parent::__construct('#__ir_products', 'id', $db);
JTableObserverTags::createObserver($this, array('typeAlias' => 'com_itemreview.product'));
}
3- Modify model file getItem function:
public function getItem($pk = null)
{
$item = parent::getItem($pk);
if (!empty($item->id))
{
$item->tags = new JHelperTags;
$item->tags->getTagIds($item->id, 'com_yourcomponent.yourmodel');
}
return $item;
}

How to customize Silverstripe 3.0 ModelAdmin

I'd like to do a number of small customizations on the ModelAdmin.
I'd like to change the text on the 'Add' button to one that is different from the original DataModel.
I have a has_many relationship. I'd like to hide the ability to 'link to existing' so that one cannot search for other 'skills' as per below.
I have the following in the Model:
public function getCMSFields() {
...
$characterSkillsField = new GridField(
'CharacterSkills',
'Character Skills',
$this->CharacterSkills(),
GridFieldConfig_RelationEditor::create()
);
$fields->addFieldToTab('Root.CharacterSkills', $characterSkillsField);
...
}
ANSWER to #2:
// Add the relation editor.
$config = GridFieldConfig_RelationEditor::create();
// Remove the ability to search and link to other skills.
$config->removeComponentsByType('GridFieldAddExistingAutocompleter');
$characterSkillsField = new GridField(
'CharacterSkills',
'Character Skills',
$this->CharacterSkills(),
$config
);
regarding #1:
add the following to the model class that's managed by the GridField (e.g. 'CharacterSkill') :
private static $singular_name = 'foo';
private static $plural_name = 'bar';
don't forget flushing the cache afterwards (add '?flush=All' to the url).
the preceding will set the button name to 'Add foo', but it's also possible to set your very own button title, using the following:
$config = GridFieldConfig_RelationEditor::create();
$addButton = $config->getComponentByType('GridFieldAddNewButton');
$addButton->setButtonName('my button name');

Advice regarding Zend_Form

I am building an application using Zend_Framework and I would like to ask you for an advice regarding Zend_Form. All forms on the site should be decorated with tables, instead of definition lists. My idea is that I should create a common form (with no elements) and then just instantiate it across the site and add elements as needed. This common form would need to modify the standard decorators, so that tables are used instead of definition lists. How do you suggest to do this? Override Zend_Form's addElement() method so that it alters decorators of new elements?
But there's another caveat. There should be an option of using a different set of decorators for a particular element, if needed. So I'm a bit puzzled how to do this. Do you have any advice?
There is no simple way to override the default decorators. The solution I use is to override all the elements and redefine the loadDefaultDecorators method.
The problem is that each element have a specific set of decorator. For example, the hidden element needs only the ViewHelper decorator while file element needs File, Errors, Description, HtmlTag (td), Label (th), HtmlTag (tr).
You can also use Zend_Form::setElementDecorators at the end of your init method (after calls to addElement). But you need to customize it for each form...
Use an intermediate class for your project wide configuration. You will then extend this class instead of Zend_Form
file My/Form.php
<?php
abstract class My_Form extends Zend_Form {
public function __construct ( $options = null ) {
parent::__construct($options);
$this->setElementDecorators(array(
// the base <input, <select, <textarea markup
'ViewHelper',
// wrap that into a <div class="input-wrap" />
array (
'HtmlTag',
array (
'tag' => 'div',
'class' => 'input-wrap',
)
),
// append errors in <ul/li>
'Errors',
// then prepend <label markup
'Label',
));
}
}
then in file My/Form/Demo.php
<?php
class My_Form_Demo extends My_Form {
public function init () {
// Your elements here
}
}
You can do this for specific element as well
file My/Form/Element/Group.php
<?php
class My_Form_Element_Group extends Zend_Form_Element_Select {
public function init () {
// Specific options
$this->addMultiOptions(array(
'A' => 'group A',
'B' => 'group B',
));
// This element doesn't need the div.input-wrap
$this->removeDecorator('HtmlTag');
}
}

Categories