Magento2 grid checkbox override old selected values - magento2

In Magento2, I have created a module for admin panel. There at Grid i select 2 rows and saved. It works fine.
But when i selected new row(It means 2 old and 1 new row) then it saved only new row rather than all 3 rows id.
My PrepareColumns for checkbox is
protected function _prepareColumns()
{
$this->addColumn(
'in_products[]',
[
'type' => 'checkbox',
'html_name' => 'in_products',
'required' => true,
'values' => $this->_getSelectedProducts(),
'align' => 'center',
'checked' => 'checked',
'index' => 'entity_id',
'header_css_class' => 'col-select',
'column_css_class' => 'col-select'
]
);
Please help me where i made mistake.

Related

How can we add new fields in a new tab in user setting in TYPO3

How can we add new fields in a new tab in user setting in TYPO3 version 8.7?
Our problem is the creation of a new tab.
I can add new fields to personal data tab in user setting, but I need to create new fields into a new tab.
For backend admin user setting I added a new tab, but for user setting I want to try this. I have an extension and since the installation of the extension it will add-on.
I tried to create new tab in fe_user.php.
Previously I tried to change ext_tables.php, but that is not working at all.
// Add some fields to FE Users table to show TCA fields definitions
// USAGE: TCA Reference > $GLOBALS['TCA'] array reference >
// ['columns'][fieldname]['config'] / TYPE: "select"
$temporaryColumns = array (
'tx_examples_options' => array (
'exclude' => 1,
'label' => 'tx_examples_options',
'config' => array (
'type' => 'select',
'showitem' => array (
array('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:fe_users.tx_examples_options.I.0', '1'),
array('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:fe_users.tx_examples_options.I.1', '2'),
array('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:fe_users.tx_examples_options.I.2', '--div--'),
array('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:fe_users.tx_examples_options.I.3', '3'),
),
'size' => 1,
'maxitems' => 1,
)
),
'tx_examples_special' => array (
'exclude' => 1,
'label' => 'tx_examples_special',
'config' => array (
'type' => 'user',
'size' => '30',
'userFunc' => 'Documentation\\Examples\\Userfuncs\\Tca->specialField',
'parameters' => array(
'color' => 'blue'
)
)
),
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
'fe_users',
$temporaryColumns
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
'fe_users',
'--div--;newtab,tx_examples_options, tx_examples_special'
);
When I changed into file ext_tables.php
$GLOBALS['TYPO3_USER_SETTINGS']['columns']['copy_directory'] = array(
'label' => 'Alternative directory for saving copies',
'type' => 'text',
'table' => 'be_users',
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addFieldsToUserSettings('copy_directory','after:lang');
then it's showing in "personal data" tab but I want new tab in user setting for non-admin user.
In general your code looks fine.
But you are in the wrong file!
Avoid ext_tables.php if possible.
If you change anything to the TCA you should do it in Configuration/TCA/ for new tables and Configuration/TCA/Overrides for enhancing existing tables.
Use filenames according to the table you are modifying.
In your case all the code should be located in Configuration/TCA/Overrides/fe_users.php.
And be sure you clear all caches if you develop anything with TCA!

Magento 2 shipping address custom dropdown field value not posting

We have added custom field under shipping address in checkout page.
It's select field and we are showing custom option from database. It's working fine but when we click next and move to payment option to place an order at that time value not posting. selected dropdown value not showing in console.
If we add text field instead then it shows value posted in console. What could be the issue?
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['custom-field'] = [
'component' => 'Magento_Ui/js/form/element/abstract',
'config' => [
'customScope' => 'shippingAddress.custom_attributes',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/select',
'id' => 'custom-field',
'options' => $optionarray
],
'dataScope' => 'shippingAddress.custom_attributes.custom_field',
'label' => 'Custom Field',
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => [],
'sortOrder' => 250,
'id' => 'custom-field'
];
return $jsLayout;
Any help would be appreciated.

Symfony Choice type with disabled options

Is there any way with Symfony to render a <select> form type with disabled options, based on the truthyness of the given choices options ?
I saw this thread (thanks to DonCallisto) about disabling choice expanded options ;
However I do not want to have an expanded choice.
I would like to keep a select element, with disabled options.
$builder->add('list', 'choice', array(
'choices' => array(
array(
'value' => 1,
'label' => '1',
'disabled' => false
),
array(
'value' => 2,
'label' => '2',
'disabled' => false
),
array(
'value' => 3,
'label' => '3',
'disabled' => true
)
),
// Instead of
// 'choices' => array(
// 1 => 'Option 1',
// 2 => 'Option 2',
// 3 => 'Option 3'
// )
);
# Which would render to the following element
<select [...]>
<option value='1'>1</value>
<option value='2'>2</value>
<option value='3' disabled='disabled'>3</value>
</select>
I just can't find the way...
Is it necessary to build its own field type ?
Since version 2.7, Symfony has introduced a way to set choice attributes using a callable, this is just what you need.
this code is taken from official Symfony documentation
$builder->add('attending', ChoiceType::class, array(
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
),
'choices_as_values' => true,
'choice_attr' => function($val, $key, $index) {
// adds a class like attending_yes, attending_no, etc
return ['class' => 'attending_'.strtolower($key)];
},
));
you can use the 'choice_attr' and pass a function that will decide wether to add a disabled attribute or not depending on the value, key or index of the choice.
...
'choice_attr' => function($key, $val, $index) {
$disabled = false;
// set disabled to true based on the value, key or index of the choice...
return $disabled ? ['disabled' => 'disabled'] : [];
},
...
According to the forms layout :
https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
and the choice_widget_collapsed and choice_widget_options,i don't think it's possible directly with the default Choice form of Symfony.
You can try :
to build your own Choice form (by extending the existing one and adding parameters to option list, i think it's the best way)
to use javascript/jquery to modify the options parameters at load time with the existing Symfony's Choice from
This is another way to load options from Entity.
You can create a new Entity Attending and use it with EntityType.
->add('attending', EntityType::class, [
'class' => Attending::class,
'choice_attr' => function($key, $val, $index) {
if ($key->getId() == StaticVal) {
return ['disabled' => 'disabled'];
}
return [];
}]
Here StaticVal can be any value that you want to disable it.
$key will give you an object value
$val will give you a value starting from 0 if you are using the placeholder option.

Zend Framework 2 Radio Button null value default

I am using Zend Framework 2, Doctrine Module and SQLServer to build a number of products.
I have a question in relation to Zend\Form\Radio.
I have the following defined in a form:
// boolean $disabled_access
$this->add(array(
'name' => 'disabled_access',
'type' => 'radio',
'options' => array(
'label' => 'Disabled Access',
'value_options' => array('1'=>"Yes", '0' => 'No'),
'allow_empty' => true,
'nullable' => true,
),
'attributes' => array('value' => null),
));
It is bound to a $building entity.
If the value of 'disabled_access' is set to true in the DB, the radio button renders correctly. Similarly if it's is set to false.
However, if the column has a NULL value, the radio button defaults to 'No'. How do I set it up to show all three potential results?
This should be enough -
$this->add(array(
'name' => 'disabled_access',
'type' => 'radio',
'options' => array(
'label' => 'Disabled Access',
'value_options' => array('1'=>'Yes', '0' => 'No'),
),
)
);
If value in database is -
1 -> 'Yes' radio button will be selected,
0 -> 'No' radio button will be selected,
NULL -> none of them will be selected.
Its working for me.
I hope it helps.

Magento, add and set a checkbox on grid and form backend

I've a fully working backend page with a grid and a corresponding form to edit the changes on the corresponding model. I added a new field on the table, bit type, as it will answer to a yes/no configuration option from the user. I added the checkbox on both grid and form.
My problem is that after a couple of hours of searching and trying different approaches I can not set the checkbox checked value both on the grid and the form reading the corresponding field from the database. Also when I click on save on the form the value corresponding to the checkbox is always saved with 1. Everything else on the grid and the form works as it should. I have read here, here, here, here and some more sites and SO questions/answers but still no clue on what I'm doing wrong. Some solutions recommend using a combo box with YES/NO options, but I want a checkbox, can't be so difficult.
Grid code inside the function _prepareColumns():
protected function _prepareColumns() {
...
$this->addColumn('banner_gral', array(
'header' => Mage::helper('banners')->__('General'),
'align' => 'center',
'index' => 'banner_gral',
'type' => 'checkbox',
'values' => $this->getBannerGral()==1 ? 'true' : 'false',
));
...
}
public function __construct()
{
parent::__construct();
$this->setId('bannersgrid');
$this->setDefaultSort('bannerid');
$this->setDefaultDir('asc');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
}
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
protected function _prepareCollection()
{
$collection = Mage::getModel('banners/bannersadmin')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
Form code to add the checkbox inside the function _prepareForm():
protected function _prepareForm()
{
$id = $this->getRequest()->getParam('id');
$params = array('id' => $this->getRequest()->getParam('id'));
if (Mage::registry('banners_data')->getdata()) {
$data = Mage::registry('banners_data')->getdata();
}
elseif (Mage::getSingleton('adminhtml/session')) {
$data = Mage::getSingleton('adminhtml/session')->getdata();
Mage::getSingleton('adminhtml/session')->getdata(null);
}
else {
$data = array();
}
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', $params),
'method' => 'post',
'enctype' => 'multipart/form-data',
));
...
$fieldset->addField('banner_gral', 'checkbox', array(
'label' => Mage::helper('banners')->__('Is general'),
'name' => 'banner_gral',
'class' => 'banner_gral',
'checked' => $this->getBannerGral()==1 ? 'true' : 'false',
'onclick' => 'this.value == this.checked ? 1 : 0',
'note' => Mage::helper('banners')->__('blablablabla'),
'tabindex' => 2
));
...
}
On the saveAction() of my form I have:
$campaign->setbanner_gral(!empty($data['banner_gral']));
In your controller saveAction() when saving the checkbox data do
$banner_gral = isset($your_form_Data['banner_gral']) ? 1 : 0;
For Grid and Form Page
In your controller you should have Mage::register(...)->getData() or Mage::register(...)
public function editAction()
....
Mage::register('example_data', $model);
On your form _prepareForm()
$model = Mage::registry('example_data'); // NOTE registry('example_data'); NOT registry('example_data')->getData();
$fieldset->addField('entire_range', 'checkbox', array(
....
'checked' => $model->getBannerGral()==1 ? 'true' : 'false',
......
))
see http://www.magentocommerce.com/boards/viewthread/20536/
On your grid _prepareColumns()
$this->addColumn('banner_gral', array(
....
'type' => 'checkbox',
'index' => 'banner_gral',
'values' => array(1,2),
'field_name' => 'checkbox_name',
....
));
#R.S answered one issue, how to save the checkbox value on the corresponding model/database field. But the issue on how to correctly display the checkbox on both the grid and the form was not solved. After doing some more searches I finally got to these two links that helped me solve my problem.
To correct the grid issue: Understanding the Grid Serializer Block
Now the part of code where the checkbox column is added, see that I added array(1,2) on the values element.
$this->addColumn('banner_gral', array(
'header' => Mage::helper('banners')->__('General'),
'width' => '20px',
'type' => 'checkbox',
'align' => 'center',
'index' => 'banner_gral',
'values' => array(1,2),
'editable' => 'false',
));
Also if you take a look into the core code of Magento, the class Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Checkbox returns an array of values. Taking a look here finally got me into the right path.
/**
* Returns values of the column
*
* #return array
*/
public function getValues()
{
if (is_null($this->_values)) {
$this->_values = $this->getColumn()->getData('values') ? $this->getColumn()->getData('values') : array();
}
return $this->_values;
}
To correct the form issue: Mage_Adminhtml_Block_System_Store_Edit_Form Class Reference
The issue on this case was that I was trying to use the $this but what I needed to use was the $data that is loaded at the beginning of the _prepareForm function. #R.S pointed the right direction, but it is not possible to use $model->getBannerGral() as the $data on the registry is an array, not a model. So, using $data["banner_gral"] I could get the needed value for the checkbox. Tested and it is working.
$fieldset->addField('banner_gral', 'checkbox', array(
'label' => Mage::helper('banners')->__('Is general'),
'name' => 'banner_gral',
'checked' => $data["banner_gral"],
'onclick' => 'this.value = this.checked ? 1 : 0;',
'note' => Mage::helper('banners')->__('blablablabla'),
'tabindex' => 2
));