Set front end label for customer attribute - magento - magento

I have a customer attribute which I setup using this script (I've only pasted part of it, the part related to the attribute)
$setup->addAttribute('customer', 'age', array(
'label' => 'Age',
'type' => 'int',
'input' => 'select',
'user_defined' => true,
'source' => 'eav/entity_attribute_source_table',
'visible' => true,
'required' => false,
'visible_on_front' => true
));
I would like to set the front end label of the attribute to "How old are you?" but keep the Admin label to "Age". How could I do this?
Thanks in advance,

Ok I managed after looking into magento
$labels = array();
$labels[0] = 'Age';//default store label
$labels[1] = 'Label for store with id 1';
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'age');
$oAttribute->setData('store_labels', $labels);
$oAttribute->save();
This did the trick.
Hope it helps someone else.

Related

How to add the alt and title fields in an image field of a custom entity in drupal 8

I created a custom entity with an image field. But I can not display the alt and title fields.
Here is my code:
$fields['main_img'] = BaseFieldDefinition::create('image')
->setLabel(t('Main image of the hardware'))
->setSettings([
'file_directory' => 'hardware',
'file_extensions' => 'png jpg jpeg',
])
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'image',
'weight' => -30,
))
->setDisplayOptions('form', array(
'label' => 'above',
'type' => 'image_image',
'weight' => -30,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
Could you tell me how to display the alt and title fields of my image and maybe someone knows where the documentation is for doing that because I can not find it?
Thank you all
I loaded one of my node field definitions with $node->getFieldDefinitions():
I believe you can try something like this:
->setDisplayOptions('form', array(
'label' => 'above',
'type' => 'image_image',
'weight' => -30,
'settings' => [
'alt_field' => TRUE,
'alt_field_required' => TRUE, //optional
'title_field' => TRUE,
'title_field_required' => TRUE, //optional
],
))
Thank Dmytro.
I feel a little stupid but it's life.
It was enough to add 'alt_field_required' => FALSE and 'title_field' => TRUE in setSettings.
But as title and alt is displayed that when we download an image I thought it did not work.
A day of lost!

How do I limit number of characters in the admin form in magento

I have following code in my Form.php
$fieldset->addField('desc', 'textarea', array(
'label' => Mage::helper('module')->__('Description'),
'required' => true,
'name' => 'desc',
));
How to restrict the number of characters in this text area?
In theory you should be able to do that by adding to the textarea a maxlength attribute.
So you should end up with something like this:
<textarea maxlength="50"></textarea>
But Magento does not allow the maxlength attribute.
If you take a look at the Varien_Data_Form_Element_Textarea class (the one responsable for rendering textareas) you will see this method.
public function getHtmlAttributes()
{
return array('title', 'class', 'style', 'onclick', 'onchange', 'rows', 'cols', 'readonly', 'disabled', 'onkeyup', 'tabindex');
}
Those are the only ones that you can specify when you create the element.
First option would be to extend this class and add the maxlength among the allowed attributes, then your column could look like this:
$fieldset->addField('desc', 'textarea', array(
'label' => Mage::helper('module')->__('Description'),
'required' => true,
'name' => 'desc',
'maxlength' => 50
));
The second option is to add it via some javascript.
$fieldset->addField('desc', 'textarea', array(
'label' => Mage::helper('module')->__('Description'),
'required' => true,
'name' => 'desc',
'after_element_html' => '<script type="text/javascript">Event.observe(window, "load", function() {$("id_of_textarea_here").setAttribute("maxlength", 50)})</script>'
));
A third option would be to insert instead of the javascript above some code that limits the length of the text.
You can find an example here.
Final Note:
the content from after_element_html will be displayed in the form right after the element. So you can basically put anything there.

Magento - Install EAV Attribute via Installer

Hey guys im using the installer in my module to add a new EAV product attribute to the Default attribute set. So far its working great, but there are 2 little things that bother me.
public function getDefaultEntities(){
return array(
'catalog_product' => array(
'entity_model' => 'catalog/product',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/product',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/product_attribute_collection',
'attributes' => array(
'disable_sale' => array(
'group' => 'General',
'label' => 'Disable Sale',
'type' => 'int',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'is_visible_on_front' => true,
'used_in_product_listing' => true,
)
)
)
);
}
I want my attribute set to be visible on product details page and catalog listing per default.
'is_visible_on_front' => true,
'used_in_product_listing' => true,
The problem is that both values are not set to be visible.
What am i doing wrong?
I haven't done this with attributes per-se, but try both 'integers' 1 AND 'string' "1" in your code and see if it works.
is_visible_on_front change to visible_on_front and then check.
Found the answer here at stackoverflow:
Magento module setup/installer script
Setup class should extend from
Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
Now the installer is aware of the additional attributes and its working like a charme.

Magento - Add global product attribute

I can not really find a satisfying answer to the following question:
What would be the best practice for a module to add a new product attribute to all attribute_sets (and possibly at a certain location).
All approaches I can find seem to refer to adding a new attribute to just one attribute set.
There is a magento way for it
Example:
$installer->addAttribute('catalog_product', 'test_me', array(
'label' => 'test Me',
'input' => 'textarea',
'type' => 'text',
'class' => '',
'global' => true,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'apply_to' => 'simple,configurable',
'visible_on_front' => true,
'is_configurable' => false,
'wysiwyg_enabled' => true,
'used_in_product_listing' => true,
'is_html_allowed_on_front' => true,
'group' => 'Config',
'sort_order' => 25
));
To add attribute to every attribute_set you need to set 'user_defined' = true and set 'group' = 'your_group_name'. If needed magento will add your group to every attribute set, and attach attribute to it.
Once you add an attribute you may assign it to one or all attribute sets, it isn't unique to the first set you assign it to. Does this answer your question?

magento: add attibute to product, but not show up when editing the product

As per bens comment and answer I updated my script, comments indicate changes
{Magento 1.4.0.1} currently i have an installer script:
$installer = $this;
$installer->startSetup();
//commented out to use factory method
//$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup = Mage::getResourceModel('catalog/setup','core_setup');
if(!$setup->getAttribute('catalog_product','attribute_code')){
$newFields = array(
'attribute_code' => array(
'type' => 'text',
'label' => 'Attribute Label',
//added visible option
'visible' => false,
),
);
$entities = array(
'catalog_product',
);
foreach($newFields as $attributeName => $attributeDefs) {
foreach ($entities as $entity) {
$setup->addAttribute($entity, $attributeName, array(
'type' => $attributeDefs['type'],
'label' => $attributeDefs['label'],
//added visible option
'visible' => $attributeDefs['visible'],
'class' => '',
'required' => false,
));
}
}
}
$installer->endSetup();
It works wonderfully! Except the attribute shows up in the General attribute group when editing the product and I don't want it to show up at all (its a secret ninja attribute) is there something I'm doing wrong? or perhaps something I should be doing to let Magento know not its not supposed to show up?
Using addAttribute() you can set the 'visibleindex tofalse. UsingupdateAttribute()` you should do the following:
$setup->updateAttribute('catalog_product','attr_code','is_visible',false);
Let me know if I'm wrong.

Resources