Unable to understand Install script (sql script) in Magento - magento

I am adding attribute by updating sql script, like this,
$installer = $this;
$installer->startSetup();
$installer->addAttribute('customer_address', 'group_id', array(
'label' => 'Address group',
'visible' => true,
'required' => false,
'type' => 'int',
'input' => 'select',
'source' => 'address_group/address_attribute_source_group',
'user_defined' => 1,
'position' => 100
));
.
.
.
$installer->endSetup();
I am unable to understand what is meant by following line, I am unable to find any explanation about it
'source' => 'address_group/address_attribute_source_group',

I am unable to comment on your post. Trying to understand if you have copied this code from somewhere. From your code I understand that you want to add a "Customer Address Attribute" named as "customer_address"
'source' => 'address_group/address_attribute_source_group'
The implication of the above is the path. You should have a folder/file path as below:
/app/code/local/Address/Group/Model/Address/Attribute/Source/Group.php
Group.php:
class Address_Group_Model_Address_Attribute_Source_Group ...
Since, this attribute is of TYPE => "SELECT", you should be having the options array in this file "Group.php"
Options Array should be something very similar to:
public function toOptionsArray() {
return array(
array(
'label' => '',
'value' => ''
),
array(
'label' => Yes,
'value' => 1
),
array(
'label' => No,
'value' => 0
)
);
}
Let me know if you got it!
Happy to Help!
Happy Coding...

It points to the class that provides options for the attribute. As attribute uses select input it requires options to be provided. This class is created by calling Mage::getModel() and passing the value of source to it. To find the class you need to find node models/address_group in config.xml files of the available modules. This will provide class prefix. Next what comes after slash is added to that prefix in order to create class name. So in this case it will resolve to something like Company_AddressGroupModule_Model_Address_Attribute_Source_Group. This class need to implement toOptionsArray method that returns an array in the following format:
array(
array('value' => 'option_value', 'label' => 'option_label'),
...
);

Related

Magento creating custom attribute with custom validations

I need to create an attribute. Also i need to validate that attribute value. So i created a new .js file and add some functions. Then in setup file, i call the function name. But after creating the attribute that validation class wont come with the field.
$installer->addAttribute(MageTest_Module_Model_Name::ENTITY, 'test_value', array(
'input' => 'text',
'type' => 'text',
'label' => 'Test Value',
'backend' => '',
'user_defined' => false,
'visible' => 1,
'required' => 0,
'position' => 60,
'class' => 'validate-testValue',
'note' => 'This should contains 2 digits. Example 00',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
'validate-testValue' is my js function name. Can anyone help me to solve this please.
Thank You.
to use Magento form validator you have to register your method with the Validator object.
Try something like that in your custom js file
Validation.add('validate-testValue','HERE your error message if the field is not valid',function(fieldValue) {
return Validation.get('IsEmpty').test(fieldValue) || fieldValue == "testValue";
});
Ok, this example will only validate if your field value is "testValue", not very usefull.
But you can adapt it I guess :)

ckeditor in Drupal 8 custom module

I am currently developing a custom module for Drupal 8.
While adding a backend form to get some data from users I tried to get the ckeditor configured to replace my textareas... and failed >.<
here is the form definition:
$form['text'] = array(
'#type' => 'textarea',
'#title' => t('Text'),
'#required' => TRUE,
'#attributes' => array(
'id' => 'text',
'style' => 'max-width: 650px'
),
'#default_value' => $data['text']
);
where do I need to load the ckeditor to replace my textarea?
things I already tried:
$build['#attached'] = array(
'js' => array(
drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js'
drupal_render($build);
and
drupal_load_library("ckeditor", "ckeditor");
but I can't load all the dependencies
Refer to the Drupal 8 form API which has a text_format form type. A check on your #default_value is also recommended.
Navigate to Configuration > Content authoring > Text formats and editors giving you an overview of the current available text formats. The ones listed here can be used in the #format proprty of your form field. (Default fallback currently is basic_html)
Try to change your code to:
$form['text'] = array(
'#type' => 'text_format',
'#title' => t('Text'),
'#required' => TRUE,
'#default_value' => isset($data['text']) ? $data['text'] : '',
'#format' => 'full_html',
);
I had a similar issue but had issues saving the data with above solution. In the end it looked like this:
$form['body'] = [
'#type' => 'text_format',
'#title' => $this->t('Body'),
'#description' => $this->t('Message'),
'#default_value' => $config->get('body'),
'#format' => 'full_html',
];
and in submitForm method I save config like so
/**
* {#inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('my_module.default')
->set('body', $form_state->getValue('body')['value'])
->save();
}
Note the reference to the attached ['value'] array.

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 admin grid sending data from Action to Controller

I'm trying to write a custom action to run off of an admin grid that I have built. Is it possible to send a value from a column in the grid to the controller via either get or post?
I've tried googling, but I cannot find a proper explanation for this anywhere. A link to an explanation of the column settings ('getter', 'type' etc.) would also be useful if this is available.
Add this code to your Grid.php:
$this->addColumn('action',
array(
'header' => Mage::helper('yourmodulename')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('yourmodulename')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
That will build an "Edit" URL with the Id of the selected row as part of the URL. It will look something like <frontname>/<controllername>/edit/id/<value> where value is returned by the getter getId().
The getter field will execute any of the standard Magento magic getters, ie any attribute is gettable. So you could have getName or getProductUrl or getIsLeftHanded if you wanted and your controller can parse the attribute.
The controller can then retrieve that passed value using Mage::app()->getRequest()->getParam('attributename');
In terms of documentation/tutorials, have a read of this article on the website of #AlanStorm as it might help.
HTH,
JD

cakePHP optional validation for file upload

How to make file uploading as optional with validation?
The code below validates even if i didn't selected any file.
I want to check the extension only if i selected the the file.
If i am not selecting any file it should not return any validation error.
class Catalog extends AppModel{
var $name = 'Catalog';
var $validate = array(
'name' => array(
'rule' => '/^[a-z0-9 ]{0,}$/i',
'allowEmpty' => false,
'message' => 'Invalid Catalog name'
),
'imageupload' => array(
'rule' => array('extension',array('jpeg','jpg','png','gif')),
'required' => false,
'allowEmpty' => true,
'message' => 'Invalid file'
),
);
}
thanks in advance
"I assign $this->data['Catalog']['image'] = $this->data['Catalog']['imageupload']['name'];"
So by the time you save your data array, it looks something like this I assume:
array(
'image' => 'foobar',
'imageupload' => array(
'name' => 'foobar',
'size' => 1234567,
'error' => 0,
...
)
)
Which means, the imageupload validation rule is trying to work on this data:
array(
'name' => 'foobar',
'size' => 1234567,
'error' => 0,
...
)
I.e. the value it's trying to validate is an array of stuff, not just a string. And that is unlikely to pass the specified validation rule. It's also probably never "empty".
Either you create a custom validation rule that can handle this array, or you need to do some more processing in the controller before you try to validate it.
Concept:
In Controller, before validating, or saving (which does validation automatically by default) check if any file is uploaded. If not uploaded, then unset validator for the file field.
Sample code:
Controller
// is any image uploaded?
$isNoFileUploaded = ($this->request->data['Model']['field_name']['error'] == UPLOAD_ERR_NO_FILE) ? true : false ;
if ($isNoFileUploaded) {
$this->Model->validator()->remove('field_name');
}
Notes:
This solution comes under preprocessing as one of the two alternative approaches (preprocessing in controller, custom validation in model) suggested by #deceze's answer

Resources