Skip to content

Fix AbstractFieldArray with column renderers, Call setId() and setName(), remove workarounds #22009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2011 Adobe
* All Rights Reserved.
*/

namespace Magento\CatalogInventory\Block\Adminhtml\Form\Field;
Expand Down Expand Up @@ -90,8 +90,12 @@ protected function _getCustomerGroups($groupId = null)
}

/**
* Set input name
*
* @param string $value
* @return $this
* @deprecated
* @see Nothing
*/
public function setInputName($value)
{
Expand All @@ -113,6 +117,7 @@ public function _toHtml()
);
}
foreach ($this->_getCustomerGroups() as $groupId => $groupLabel) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$this->addOption($groupId, addslashes($groupLabel));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2011 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

Expand All @@ -16,6 +16,7 @@
* @deprecated 100.3.0 Replaced with Multi Source Inventory
* @link https://developer.adobe.com/commerce/webapi/rest/inventory/index.html
* @link https://developer.adobe.com/commerce/webapi/rest/inventory/inventory-api-reference.html
* @see Nothing
*/
class Minsaleqty extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
{
Expand All @@ -33,9 +34,7 @@ protected function _getGroupRenderer()
{
if (!$this->_groupRenderer) {
$this->_groupRenderer = $this->getLayout()->createBlock(
\Magento\CatalogInventory\Block\Adminhtml\Form\Field\Customergroup::class,
'',
['data' => ['is_render_to_js_template' => true]]
\Magento\CatalogInventory\Block\Adminhtml\Form\Field\Customergroup::class
);
$this->_groupRenderer->setClass('customer_group_select admin__control-select');
}
Expand Down Expand Up @@ -69,6 +68,8 @@ protected function _prepareToRender()
*
* @param \Magento\Framework\DataObject $row
* @return void
* @deprecated
* @see Nothing
*/
protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2011 Adobe
* All Rights Reserved.
*/

namespace Magento\Config\Block\System\Config\Form\Field\FieldArray;
Expand All @@ -10,7 +10,6 @@
* Backend system config array field renderer
*
* @author Magento Core Team <core@magentocommerce.com>
* @api
* @since 100.0.2
*/
abstract class AbstractFieldArray extends \Magento\Config\Block\System\Config\Form\Field
Expand Down Expand Up @@ -126,10 +125,12 @@ protected function _getElementHtml(\Magento\Framework\Data\Form\Element\Abstract
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
// @codingStandardsIgnoreStart
protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
{
// override in descendants
}
// @codingStandardsIgnoreEnd

/**
* Obtain existing data from form element
Expand Down Expand Up @@ -196,27 +197,31 @@ protected function _getCellInputElementName($columnName)
public function renderCellTemplate($columnName)
{
if (empty($this->_columns[$columnName])) {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception('Wrong column name specified.');
}
$column = $this->_columns[$columnName];
$inputName = $this->_getCellInputElementName($columnName);
$inputId = $this->_getCellInputElementId('<%- _id %>', $columnName);

if ($column['renderer']) {
return $column['renderer']->setInputName(
return $column['renderer']->setName(
$inputName
)->setId(
$inputId
// setInputName() and setInputId() are called for BC.
)->setInputName(
$inputName
)->setInputId(
$this->_getCellInputElementId('<%- _id %>', $columnName)
$inputId
)->setColumnName(
$columnName
)->setColumn(
$column
)->toHtml();
}

return '<input type="text" id="' . $this->_getCellInputElementId(
'<%- _id %>',
$columnName
) .
return '<input type="text" id="' . $inputId .
'"' .
' name="' .
$inputName .
Expand All @@ -237,10 +242,12 @@ public function renderCellTemplate($columnName)
*
* @return void
*/
// @codingStandardsIgnoreStart
protected function _prepareToRender()
{
// Override in descendants to add columns, change add button label etc
}
// @codingStandardsIgnoreEnd

/**
* Render block HTML
Expand All @@ -255,6 +262,7 @@ protected function _toHtml()
$this->_isPreparedToRender = true;
}
if (empty($this->_columns)) {
// phpcs:ignore Magento2.Exceptions.DirectThrow
throw new \Exception('At least one column must be defined.');
}
return parent::_toHtml();
Expand All @@ -281,6 +289,8 @@ public function getColumns()
}

/**
* Add Button Label
*
* @return string
* @since 101.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/

namespace Magento\CatalogInventory\Block\Adminhtml\Form\Field;

class CustomergroupTest extends \PHPUnit\Framework\TestCase
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class CustomergroupTest extends TestCase
{
/**
* @var \Magento\CatalogInventory\Block\Adminhtml\Form\Field\Customergroup
* @var Customergroup
*/
protected $_block;

protected function setUp(): void
{
$this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\CatalogInventory\Block\Adminhtml\Form\Field\Customergroup::class
);
$this->_block = Bootstrap::getObjectManager()->create(Customergroup::class);
}

public function testToHtml()
public function testToHtml(): void
{
$this->_block->setClass('customer_group_select admin__control-select');
$this->_block->setId('123');
$this->_block->setTitle('Customer Group');
$this->_block->setInputName('groups[item_options]');
$this->_block->setName('groups[item_options]');
$expectedResult = '<select name="groups[item_options]" id="123" '
. 'class="customer_group_select admin__control-select" '
. 'title="Customer Group" ><option value="32000" >ALL GROUPS</option><option value="0" >NOT LOGGED IN'
. '</option><option value="1" >General</option><option value="2" >Wholesale</option><option value="3" >'
. 'Retailer</option></select>';

$this->assertEquals($expectedResult, $this->_block->_toHtml());
}
}