Skip to content

Commit

Permalink
🔃 [Magento Community Engineering] Community Contributions - 2.4-develop
Browse files Browse the repository at this point in the history
Accepted Community Pull Requests:
 - #26075: Fix #6310 - Changing products 'this item has weight' using 'Update Attributes' is not possible (by @Bartlomiejsz)


Fixed GitHub Issues:
 - #6310: Changing products 'this item has weight' using 'Update Attributes' is not possible (reported by @hostep) has been fixed in #26075 by @Bartlomiejsz in 2.4-develop branch
   Related commits:
     1. 2506c08
     2. f6f414b
     3. 36738ec
  • Loading branch information
magento-engcom-team authored Mar 28, 2020
2 parents 45a3970 + b2a2476 commit a6332d6
Show file tree
Hide file tree
Showing 10 changed files with 755 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@

namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Action\Attribute\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Magento\Catalog\Block\Adminhtml\Form;
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Boolean;
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Image;
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Price;
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Weight;
use Magento\Catalog\Helper\Product\Edit\Action\Attribute;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Registry;

/**
* Attributes tab block
Expand All @@ -23,37 +34,38 @@
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
class Attributes extends \Magento\Catalog\Block\Adminhtml\Form implements
\Magento\Backend\Block\Widget\Tab\TabInterface
class Attributes extends Form implements TabInterface
{
/**
* @var \Magento\Catalog\Model\ProductFactory
* @var ProductFactory
*/
protected $_productFactory;

/**
* @var \Magento\Catalog\Helper\Product\Edit\Action\Attribute
* @var Attribute
*/
protected $_attributeAction;

/** @var array */
/**
* @var array
*/
private $excludeFields;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param \Magento\Catalog\Helper\Product\Edit\Action\Attribute $attributeAction
* @param Context $context
* @param Registry $registry
* @param FormFactory $formFactory
* @param ProductFactory $productFactory
* @param Attribute $attributeAction
* @param array $data
* @param array|null $excludeFields
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Catalog\Helper\Product\Edit\Action\Attribute $attributeAction,
Context $context,
Registry $registry,
FormFactory $formFactory,
ProductFactory $productFactory,
Attribute $attributeAction,
array $data = [],
array $excludeFields = null
) {
Expand All @@ -72,7 +84,7 @@ public function __construct(
*/
protected function _prepareForm(): void
{
$this->setFormExcludedFieldList($this->getExcludedFields());
$this->setFormExcludedFieldList($this->excludeFields);
$this->_eventManager->dispatch(
'adminhtml_catalog_product_form_prepare_excluded_field_list',
['object' => $this]
Expand Down Expand Up @@ -110,10 +122,10 @@ public function getAttributes()
protected function _getAdditionalElementTypes()
{
return [
'price' => \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Price::class,
'weight' => \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Weight::class,
'image' => \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Image::class,
'boolean' => \Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Boolean::class
'price' => Price::class,
'weight' => Weight::class,
'image' => Image::class,
'boolean' => Boolean::class
];
}

Expand All @@ -129,7 +141,7 @@ protected function _getAdditionalElementHtml($element)
$nameAttributeHtml = $element->getExtType() === 'multiple' ? 'name="' . $element->getId() . '_checkbox"' : '';
$elementId = $element->getId();
$dataAttribute = "data-disable='{$elementId}'";
$dataCheckboxName = "toggle_" . "{$elementId}";
$dataCheckboxName = "toggle_{$elementId}";
$checkboxLabel = __('Change');
// @codingStandardsIgnoreStart
$html = <<<HTML
Expand All @@ -140,14 +152,8 @@ protected function _getAdditionalElementHtml($element)
</label>
</span>
HTML;
if ($elementId === 'weight') {
$html .= <<<HTML
<script>require(['Magento_Catalog/js/product/weight-handler'], function (weightHandle) {
weightHandle.hideWeightSwitcher();
});</script>
HTML;
// @codingStandardsIgnoreEnd
}

// @codingStandardsIgnoreEnd
return $html;
}

Expand Down Expand Up @@ -190,14 +196,4 @@ public function isHidden()
{
return false;
}

/**
* Returns excluded fields
*
* @return array
*/
private function getExcludedFields(): array
{
return $this->excludeFields;
}
}
153 changes: 115 additions & 38 deletions app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,64 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

/**
* Product form weight field helper
*/
namespace Magento\Catalog\Block\Adminhtml\Product\Helper\Form;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Directory\Helper\Data;
use Magento\Framework\Data\Form;
use Magento\Catalog\Model\Product\Edit\WeightResolver;
use Magento\Framework\Data\Form\Element\CollectionFactory;
use Magento\Framework\Data\Form\Element\Factory;
use Magento\Framework\Data\Form\Element\Radios;
use Magento\Framework\Data\Form\Element\Text;
use Magento\Framework\Escaper;
use Magento\Framework\Locale\Format;

class Weight extends \Magento\Framework\Data\Form\Element\Text
/**
* Product form weight field helper
*/
class Weight extends Text
{
/**
* Weight switcher radio-button element
*
* @var \Magento\Framework\Data\Form\Element\Checkbox
* @var Radios
*/
protected $weightSwitcher;

/**
* @var \Magento\Framework\Locale\Format
* @var Format
*/
protected $localeFormat;

/**
* @var \Magento\Directory\Helper\Data
* @var Data
*/
protected $directoryHelper;

/**
* @param \Magento\Framework\Data\Form\Element\Factory $factoryElement
* @param \Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Framework\Locale\Format $localeFormat
* @param \Magento\Directory\Helper\Data $directoryHelper
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param Format $localeFormat
* @param Data $directoryHelper
* @param array $data
*/
public function __construct(
\Magento\Framework\Data\Form\Element\Factory $factoryElement,
\Magento\Framework\Data\Form\Element\CollectionFactory $factoryCollection,
\Magento\Framework\Escaper $escaper,
\Magento\Framework\Locale\Format $localeFormat,
\Magento\Directory\Helper\Data $directoryHelper,
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
Format $localeFormat,
Data $directoryHelper,
array $data = []
) {
$this->directoryHelper = $directoryHelper;
$this->localeFormat = $localeFormat;
$this->weightSwitcher = $factoryElement->create('radios');
$this->weightSwitcher->setValue(
WeightResolver::HAS_WEIGHT
WeightResolver::HAS_NO_WEIGHT
)->setValues(
[
['value' => WeightResolver::HAS_WEIGHT, 'label' => __('Yes')],
Expand All @@ -75,28 +84,48 @@ public function __construct(
*/
public function getElementHtml()
{
if (!$this->getForm()->getDataObject()->getTypeInstance()->hasWeight()) {
$this->weightSwitcher->setValue(WeightResolver::HAS_NO_WEIGHT);
if ($this->getForm()->getDataObject()->getTypeInstance()->hasWeight()) {
$this->weightSwitcher->setValue(WeightResolver::HAS_WEIGHT);
}

if ($this->getDisabled()) {
$this->weightSwitcher->setDisabled($this->getDisabled());
}
return '<div class="admin__field-control weight-switcher">' .
'<div class="admin__control-switcher" data-role="weight-switcher">' .
$this->weightSwitcher->getLabelHtml() .
'<div class="admin__field-control-group">' .
$this->weightSwitcher->getElementHtml() .
'</div>' .
'</div>' .
'<div class="admin__control-addon">' .
parent::getElementHtml() .
'<label class="admin__addon-suffix" for="' .
$this->getHtmlId() .
'"><span>' .
$this->directoryHelper->getWeightUnit() .
'</span></label>' .
'</div>' .
'</div>';

$htmlId = $this->getHtmlId();
$html = '';

if ($beforeElementHtml = $this->getBeforeElementHtml()) {
$html .= '<label class="addbefore" for="' . $htmlId . '">' . $beforeElementHtml . '</label>';
}

$html .= '<div class="admin__control-addon">';

if (is_array($this->getValue())) {
foreach ($this->getValue() as $value) {
$html .= $this->getHtmlForInputByValue($this->_escape($value));
}
} else {
$html .= $this->getHtmlForInputByValue($this->getEscapedValue());
}

$html .= '<label class="admin__addon-suffix" for="' .
$this->getHtmlId() .
'"><span>' .
$this->directoryHelper->getWeightUnit() .
'</span></label></div>';

if ($afterElementJs = $this->getAfterElementJs()) {
$html .= $afterElementJs;
}

if ($afterElementHtml = $this->getAfterElementHtml()) {
$html .= '<label class="addafter" for="' . $htmlId . '">' . $afterElementHtml . '</label>';
}

$html .= $this->getHtmlForWeightSwitcher();

return $html;
}

/**
Expand All @@ -112,8 +141,7 @@ public function setForm($form)
}

/**
* @param null|int|string $index
* @return null|string
* @inheritDoc
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getEscapedValue($index = null)
Expand All @@ -134,4 +162,53 @@ public function getEscapedValue($index = null)

return $value;
}

/**
* Get input html by sting value.
*
* @param string|null $value
*
* @return string
*/
private function getHtmlForInputByValue($value)
{
return '<input id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->_getUiId()
. ' value="' . $value . '" ' . $this->serialize($this->getHtmlAttributes()) . '/>';
}

/**
* Get weight switcher html.
*
* @return string
*/
private function getHtmlForWeightSwitcher()
{
$html = '<div class="admin__control-addon">';
$html .= '<div class="admin__field-control weight-switcher">' .
'<div class="admin__control-switcher" data-role="weight-switcher">' .
$this->weightSwitcher->getLabelHtml() .
'<div class="admin__field-control-group">' .
$this->weightSwitcher->getElementHtml() .
'</div>' .
'</div>';

$html .= '<label class="addafter">';
$elementId = ProductAttributeInterface::CODE_HAS_WEIGHT;
$nameAttributeHtml = 'name="' . $elementId . '_checkbox"';
$dataCheckboxName = "toggle_{$elementId}";
$checkboxLabel = __('Change');
$html .= <<<HTML
<span class="attribute-change-checkbox">
<input type="checkbox" id="$dataCheckboxName" name="$dataCheckboxName" class="checkbox" $nameAttributeHtml
onclick="toogleFieldEditMode(this, 'weight-switcher1'); toogleFieldEditMode(this, 'weight-switcher0');" />
<label class="label" for="$dataCheckboxName">
{$checkboxLabel}
</label>
</span>
HTML;

$html .= '</label></div></div>';

return $html;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
namespace Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute;

use Magento\AsynchronousOperations\Api\Data\OperationInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Model\Config;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Backend\App\Action;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

/**
* Class Save
* Class used for saving mass updated products attributes.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Save extends \Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute implements HttpPostActionInterface
Expand Down Expand Up @@ -146,6 +147,10 @@ private function sanitizeProductAttributes($attributesData)
$dateFormat = $this->timezone->getDateFormat(\IntlDateFormatter::SHORT);

foreach ($attributesData as $attributeCode => $value) {
if ($attributeCode === ProductAttributeInterface::CODE_HAS_WEIGHT) {
continue;
}

$attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
if (!$attribute->getAttributeId()) {
unset($attributesData[$attributeCode]);
Expand Down
Loading

0 comments on commit a6332d6

Please sign in to comment.