Skip to content

Commit

Permalink
Merge pull request #1806 from magento-engcom/2.2-develop-prs
Browse files Browse the repository at this point in the history
Public Pull Requests

#12530 Added correction for og:type content value by @atishgoswami
#12359 Add a --no-update option to sampledata:deploy and sampledata:remove commands by @schmengler
#11617 Re saving product attribute by @raumatbel
#11608 Fix for issue 9633 500 error on setup wizard with memcache by @sylink

Fixed Public Issues

#6770 M2.1.1 : Re-saving a product attribute with a different name than it's code results in an error
#9633 Web Setup Wizard 500 error when session storage is configured to use memcache
  • Loading branch information
Oleksii Korshenko authored Dec 4, 2017
2 parents 9eb883d + 2c4b5b0 commit 276c690
Show file tree
Hide file tree
Showing 11 changed files with 495 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,95 @@
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;

use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Catalog\Controller\Adminhtml\Product\Attribute;
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
use Magento\Catalog\Helper\Product;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator;
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
use Magento\Framework\Cache\FrontendInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filter\FilterManager;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutFactory;
use Magento\Framework\View\Result\PageFactory;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Save extends \Magento\Catalog\Controller\Adminhtml\Product\Attribute
class Save extends Attribute
{
/**
* @var \Magento\Catalog\Model\Product\AttributeSet\BuildFactory
* @var BuildFactory
*/
protected $buildFactory;

/**
* @var \Magento\Framework\Filter\FilterManager
* @var FilterManager
*/
protected $filterManager;

/**
* @var \Magento\Catalog\Helper\Product
* @var Product
*/
protected $productHelper;

/**
* @var \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory
* @var AttributeFactory
*/
protected $attributeFactory;

/**
* @var \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory
* @var ValidatorFactory
*/
protected $validatorFactory;

/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
* @var CollectionFactory
*/
protected $groupCollectionFactory;

/**
* @var \Magento\Framework\View\LayoutFactory
* @var LayoutFactory
*/
private $layoutFactory;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Cache\FrontendInterface $attributeLabelCache
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Catalog\Model\Product\AttributeSet\BuildFactory $buildFactory
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
* @param \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory
* @param \Magento\Framework\Filter\FilterManager $filterManager
* @param \Magento\Catalog\Helper\Product $productHelper
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param Context $context
* @param FrontendInterface $attributeLabelCache
* @param Registry $coreRegistry
* @param BuildFactory $buildFactory
* @param PageFactory $resultPageFactory
* @param AttributeFactory $attributeFactory
* @param ValidatorFactory $validatorFactory
* @param CollectionFactory $groupCollectionFactory
* @param FilterManager $filterManager
* @param Product $productHelper
* @param LayoutFactory $layoutFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Cache\FrontendInterface $attributeLabelCache,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Catalog\Model\Product\AttributeSet\BuildFactory $buildFactory,
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory,
\Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory $validatorFactory,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
\Magento\Framework\Filter\FilterManager $filterManager,
\Magento\Catalog\Helper\Product $productHelper,
\Magento\Framework\View\LayoutFactory $layoutFactory
Context $context,
FrontendInterface $attributeLabelCache,
Registry $coreRegistry,
PageFactory $resultPageFactory,
BuildFactory $buildFactory,
AttributeFactory $attributeFactory,
ValidatorFactory $validatorFactory,
CollectionFactory $groupCollectionFactory,
FilterManager $filterManager,
Product $productHelper,
LayoutFactory $layoutFactory
) {
parent::__construct($context, $attributeLabelCache, $coreRegistry, $resultPageFactory);
$this->buildFactory = $buildFactory;
Expand All @@ -90,7 +106,7 @@ public function __construct(
}

/**
* @return \Magento\Backend\Model\View\Result\Redirect
* @return Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
Expand All @@ -107,36 +123,51 @@ public function execute()
$name = trim($name);

try {
/** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
/** @var $attributeSet Set */
$attributeSet = $this->buildFactory->create()
->setEntityTypeId($this->_entityTypeId)
->setSkeletonId($setId)
->setName($name)
->getAttributeSet();
} catch (AlreadyExistsException $alreadyExists) {
$this->messageManager->addError(__('An attribute set named \'%1\' already exists.', $name));
$this->messageManager->addErrorMessage(__('An attribute set named \'%1\' already exists.', $name));
$this->_session->setAttributeData($data);

return $this->returnResult('catalog/*/edit', ['_current' => true], ['error' => true]);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while saving the attribute.'));
$this->messageManager->addExceptionMessage(
$e,
__('Something went wrong while saving the attribute.')
);
}
}

$attributeId = $this->getRequest()->getParam('attribute_id');
$attributeCode = $this->getRequest()->getParam('attribute_code')
?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);

/** @var $model ProductAttributeInterface */
$model = $this->attributeFactory->create();
if ($attributeId) {
$model->load($attributeId);
}
$attributeCode = $model && $model->getId()
? $model->getAttributeCode()
: $this->getRequest()->getParam('attribute_code');
$attributeCode = $attributeCode ?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
if (strlen($attributeCode) > 0) {
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u']);
$validatorAttrCode = new \Zend_Validate_Regex(
['pattern' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u']
);
if (!$validatorAttrCode->isValid($attributeCode)) {
$this->messageManager->addError(
$this->messageManager->addErrorMessage(
__(
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
'numbers (0-9) or underscore(_) in this field, first character should be a letter.',
$attributeCode
)
);

return $this->returnResult(
'catalog/*/edit',
['attribute_id' => $attributeId, '_current' => true],
Expand All @@ -148,12 +179,13 @@ public function execute()

//validate frontend_input
if (isset($data['frontend_input'])) {
/** @var $inputType \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator */
/** @var $inputType Validator */
$inputType = $this->validatorFactory->create();
if (!$inputType->isValid($data['frontend_input'])) {
foreach ($inputType->getMessages() as $message) {
$this->messageManager->addError($message);
$this->messageManager->addErrorMessage($message);
}

return $this->returnResult(
'catalog/*/edit',
['attribute_id' => $attributeId, '_current' => true],
Expand All @@ -162,19 +194,17 @@ public function execute()
}
}

/* @var $model \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$model = $this->attributeFactory->create();

if ($attributeId) {
$model->load($attributeId);
if (!$model->getId()) {
$this->messageManager->addError(__('This attribute no longer exists.'));
$this->messageManager->addErrorMessage(__('This attribute no longer exists.'));

return $this->returnResult('catalog/*/', [], ['error' => true]);
}
// entity type check
if ($model->getEntityTypeId() != $this->_entityTypeId) {
$this->messageManager->addError(__('We can\'t update the attribute.'));
$this->messageManager->addErrorMessage(__('We can\'t update the attribute.'));
$this->_session->setAttributeData($data);

return $this->returnResult('catalog/*/', [], ['error' => true]);
}

Expand All @@ -195,7 +225,7 @@ public function execute()

$data += ['is_filterable' => 0, 'is_filterable_in_search' => 0];

if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
if ($model->getIsUserDefined() === null || $model->getIsUserDefined() != 0) {
$data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
}

Expand Down Expand Up @@ -241,7 +271,7 @@ public function execute()

try {
$model->save();
$this->messageManager->addSuccess(__('You saved the product attribute.'));
$this->messageManager->addSuccessMessage(__('You saved the product attribute.'));

$this->_attributeLabelCache->clean();
$this->_session->setAttributeData(false);
Expand All @@ -252,9 +282,10 @@ public function execute()
'_current' => true,
'product_tab' => $this->getRequest()->getParam('product_tab'),
];
if (!is_null($attributeSet)) {
if ($attributeSet !== null) {
$requestParams['new_attribute_set_id'] = $attributeSet->getId();
}

return $this->returnResult('catalog/product/addAttribute', $requestParams, ['error' => false]);
} elseif ($this->getRequest()->getParam('back', false)) {
return $this->returnResult(
Expand All @@ -263,25 +294,28 @@ public function execute()
['error' => false]
);
}

return $this->returnResult('catalog/*/', [], ['error' => false]);
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
$this->messageManager->addErrorMessage($e->getMessage());
$this->_session->setAttributeData($data);

return $this->returnResult(
'catalog/*/edit',
['attribute_id' => $attributeId, '_current' => true],
['error' => true]
);
}
}

return $this->returnResult('catalog/*/', [], ['error' => true]);
}

/**
* @param string $path
* @param array $params
* @param array $response
* @return \Magento\Framework\Controller\Result\Json|\Magento\Backend\Model\View\Result\Redirect
* @return Json|Redirect
*/
private function returnResult($path = '', array $params = [], array $response = [])
{
Expand All @@ -291,8 +325,10 @@ private function returnResult($path = '', array $params = [], array $response =

$response['messages'] = [$layout->getMessagesBlock()->getGroupedHtml()];
$response['params'] = $params;

return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($response);
}

return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath($path, $params);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/** @var $block \Magento\Catalog\Block\Product\View */
?>

<meta property="og:type" content="og:product" />
<meta property="og:type" content="product" />
<meta property="og:title" content="<?= $block->escapeHtmlAttr($block->stripTags($block->getProduct()->getName())) ?>" />
<meta property="og:image" content="<?= $block->escapeUrl($block->getImage($block->getProduct(), 'product_base_image')->getImageUrl()) ?>" />
<meta property="og:description" content="<?= $block->escapeHtmlAttr($block->stripTags($block->getProduct()->getShortDescription())) ?>" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command for deployment of Sample Data
*/
class SampleDataDeployCommand extends Command
{
const OPTION_NO_UPDATE = 'no-update';

/**
* @var \Magento\Framework\Filesystem
*/
Expand Down Expand Up @@ -66,6 +69,12 @@ protected function configure()
{
$this->setName('sampledata:deploy')
->setDescription('Deploy sample data modules');
$this->addOption(
self::OPTION_NO_UPDATE,
null,
InputOption::VALUE_NONE,
'Update composer.json without executing composer update'
);
parent::configure();
}

Expand All @@ -80,6 +89,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (!empty($sampleDataPackages)) {
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
$commonArgs = ['--working-dir' => $baseDir, '--no-progress' => 1];
if ($input->getOption(self::OPTION_NO_UPDATE)) {
$commonArgs['--no-update'] = 1;
}
$packages = [];
foreach ($sampleDataPackages as $name => $version) {
$packages[] = "$name:$version";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\SampleData\Model\Dependency;
use Symfony\Component\Console\Input\ArrayInput;
Expand All @@ -22,6 +23,8 @@
*/
class SampleDataRemoveCommand extends Command
{
const OPTION_NO_UPDATE = 'no-update';

/**
* @var Filesystem
*/
Expand Down Expand Up @@ -69,6 +72,12 @@ protected function configure()
{
$this->setName('sampledata:remove')
->setDescription('Remove all sample data packages from composer.json');
$this->addOption(
self::OPTION_NO_UPDATE,
null,
InputOption::VALUE_NONE,
'Update composer.json without executing composer update'
);
parent::configure();
}

Expand All @@ -81,6 +90,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (!empty($sampleDataPackages)) {
$baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
$commonArgs = ['--working-dir' => $baseDir, '--no-interaction' => 1, '--no-progress' => 1];
if ($input->getOption(self::OPTION_NO_UPDATE)) {
$commonArgs['--no-update'] = 1;
}
$packages = array_keys($sampleDataPackages);
$arguments = array_merge(['command' => 'remove', 'packages' => $packages], $commonArgs);
$commandInput = new ArrayInput($arguments);
Expand Down
Loading

0 comments on commit 276c690

Please sign in to comment.