Magento 2 Get Category list in admin tab/main.php - magento2

I have created a custom module.Now I want to get categories in drop down in admin.
The file is on the following path,
app/code/vendor/theme/block/adminhtml/catbanner/edit/tab/Main.php
The html is for dropdown is,
$fieldset->addField(
'banner_category',
'select',
[
'label' => __('Select Category'),
'title' => __('Select Category'),
'name' => 'banner_category',
'required' => true,
'options' => \vendor\module\Block\Adminhtml\Catbanner\Grid::getOptionArray1(),
'disabled' => $isElementDisabled
]
);
I want the options to be populated by the categories.Kindly help how can i do that?

Use below code for fieldset
$fieldset->addField(
'category',
'select',
[
'name' => 'category',
'label' => __('Category'),
'id' => 'category',
'title' => __('Category'),
'values' => \vendor\module\Block\Adminhtml\Catbanner\Grid::getOptionArray1(),
'class' => 'category',
'required' => true,
]);
In your grid block use below code:
public function getOptionArray1()
{
$categoryCollection = $this->_categoryCollectionFactory->create()
->addAttributeToSelect(array('id','name'))
->addAttributeToFilter('is_active','1');
$options = array();
foreach($categoryCollection as $category){
$options[] = array(
'label' => $category->getName(),
'value' => $category->getId()
);
}
return $options;
}
Hope this will work for you...

Related

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

I have following code in my Form.php
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
));
I want to restrict the number of rows to display it like a single row, the code: 'rows' => 1 doesn't work here.
You can use styling to do this
$fieldset->addField('name', 'textarea', array(
'label' => Mage::helper('module')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
'style' => "height: 1em;",
));
Just had to change 'textarea' to 'text'

How to build a single 'flag' checkbox in prestashop with form helpers?

I can't figure out from official docs how to build a single checkbox element from the standard helpers. I already have the relevant boolean entity in database and I can build radios or selects as well for it, and they work.
But what I'd really like is to have a single checkbox to use as a boolean flag.
Anyone knows how?
Ok, the answer is to just use the 'switch' type: that will build a 'slider' switch on backoffice page. For future reference, I'm gonna report 3 different ways to accomplish the same task: radio, select and switch.
They have all been tested on AdminAddressesController and are bound to a custom DB boolean field called 'expo'.
//SELECT
$s_options = array(
array( 'expo' => 1, 'name' => 'Yes' ),
array( 'expo' => 0, 'name' => 'No' )
);
$temp_fields[] = array(
'type' => 'select',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'options' => array(
'query' => $s_options,
'id' => 'expo',
'name' => 'name'
)
);
//RADIO
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'radio',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => $s_options
);
//SWITCH
$s_options = array(
array( 'id' => 'expo_on', 'value' => 1, 'label' => $this->l('Yes')),
array( 'id' => 'expo_off', 'value' => 0, 'label' => $this->l('No')),
);
$temp_fields[] = array(
'type' => 'switch',
'label' => $this->l('Is Expo'),
'name' => 'expo',
'required' => false,
'is_bool' => true,
'values' => $s_options
);

Prestashop Module : Add multi select dropdown

I'm working on a module and I would like to know how to add multiple dropdown with fields_options.
$this->fields_options = array(
'Test' => array(
'title' => $this->l('Test'),
'icon' => 'delivery',
'fields' => array(
'IXY_GALLERY_CREATION_OCCASION' => array(
'title' => $this->l('DropdownList'),
'type' => 'select',
'multiple' => true , // not working
'identifier' => 'value',
'list' => array(
1 => array('value' => 1, 'name' => $this->l('Test 1 ')),
2 => array('value' => 2, 'name' => $this->l('Test 2)'))
)
),
),
'description' =>'',
'submit' => array('title' => $this->l('Save'))
)
);
This is how I'm doin if you're meaning that :
$combo = $this->getAddFieldsValues();
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Title'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'nom_matiere',
'options' => array(
'query' => $combo[0],
'id' => 'id_option',
'name' => 'name'
)
),
array(
'type' => 'select',
'lang' => true,
'label' => $this->l('Nom'),
'name' => 'name',
'options' => array(
'query' => $combo[1],
'id' => 'id_option',
'name' => 'name'
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => $this->l('updateData'),
)
),
);
the answer are not correctly .. due its not only dfined the field in the database, also must capture and stored in special way the values, in this example i demostrate to store as "1,2,3,6,8" using a single field
THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk
here i put only the most important parts..
as mentioned int he previous link, added a new fiel in the model definition, class and the table sql
this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field
then in the helper form list array of inputs define a select as:
at the begining dont foget to added that line:
// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['id_employee[]'] = explode(',',$obj->id_employee);
this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"
and the in the field form helper list of inputs define the select multiple as:
array(
'type' => 'select',
'label' => $this->l('Select and employee'),
'name' => 'id_employee_tech',
'required' => false,
'col' => '6',
'default_value' => (int)Tools::getValue('id_employee_tech'),
'options' => array(
'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
'id' => 'id_employee',
'name' => 'firstname',
'default' => array(
'value' => '',
'label' => $this->l('ninguno')
)
)
),
an then override the post process too
public function postProcess()
{
if (Tools::isSubmit('submitTallerOrden'))
{
$_POST['id_employee'] = implode(',', Tools::getValue('id_employee'));
}
parent::postProcess();
}
this make stored in the db as "1,2,3"

Add custom form in magento admin side

I want to make custom form in magento with two section like (two green box)
Also I wand to make three fields instead of one like
I have make code like
protected function _prepareForm() {
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('slider_form', array('legend' => Mage::helper('slider')->__('Slider information')));
$fieldset->addField('link_title', 'text', array(
'label' => Mage::helper('slider')->__('Link Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'link_title',
));
$fieldset->addField('link', 'text', array(
'label' => Mage::helper('slider')->__('Link'),
'class' => 'required-entry',
'required' => true,
'name' => 'link',
));
$fieldset->addField('content', 'text', array(
'label' => Mage::helper('slider')->__('Text'),
'class' => 'required-entry',
'required' => true,
'name' => 'content',
));
....
....
}
Can anybody help!!!

Symfony2: How to translate custom error messages in form types?

I need to translate the error messages from my form type. Here is my form Type code:
class ReferFriendType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$defaultSubject = "This is a default referral subject.";
$defaultMessage = "This is a default referral message.";
$builder->add('email1', 'email',array(
'required' => true,
'label' => 'Email 1* :',
'attr' => array('class' => 'large_text'),
));
$builder->add('email2', 'email',array(
'label' => 'Email 2 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('email3', 'email',array(
'label' => 'Email 3 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('email4', 'email',array(
'label' => 'Email 4 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('email5', 'email',array(
'label' => 'Email 5 :',
'required' => false,
'attr' => array('class' => 'large_text'),
));
$builder->add('subject', 'text', array(
'data' => $defaultSubject,
'required' => true,
'label' => 'Subject* :',
'attr' => array('class' => 'large_text'),
));
$builder->add('message', 'textarea', array(
'data' => $defaultMessage,
'required' => true,
'label' => 'Message* :',
'attr' => array('rows' => '5', 'cols' => '40'),
));
}
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection( array(
'fields' => array(
'email1' => array(
new Email(),
new NotBlank(array(
'message' => 'You must enter atleast one email address for a valid submission',
)),
),
'subject' => new NotBlank(),
'message' => new NotBlank(),
),
'allowExtraFields' => true,
'allowMissingFields' => true,
));
return array(
'validation_constraint' => $collectionConstraint,
'csrf_protection' => false,
);
}
public function getName()
{
return 'referFriend';
}
}
I want to translate 'You must enter atleast one email address for a valid submission' in getDefaultOptions() method into french. I have added the translation in the messages.fr.yml. But it is not getting translated. Any ideas how this can be done?
Validation translations go to the validators.LANG.yml files — not messages.LANG.yml ones.
The replacements are not set in the validation.yml file but by the Validator.
validators.en.yml
noFirstnameMinLimit: Please provide at least {{ limit }} characters
validation.yml
Acm\AddressBundle\Entity\Address:
properties:
firstname:
- Length:
min: 3
minMessage: "noFirstnameMinLimit"
This works for me with Symfony 2.4
There is an example in the docs.
It`s easy, see http://symfony.com/doc/current/book/translation.html#translating-constraint-messages
And set default_locale in /app/config/config.yml or play with $this->get('request')->setLocale('ru');