Skip to content

Commit

Permalink
Merge pull request #687 from akeneo/release/104.3.7
Browse files Browse the repository at this point in the history
Release/104.3.7
  • Loading branch information
magentix authored Apr 24, 2024
2 parents 6dd5ab3 + cfc0863 commit 1815946
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
52 changes: 39 additions & 13 deletions Block/Adminhtml/System/Config/Form/Field/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,53 @@

namespace Akeneo\Connector\Block\Adminhtml\System\Config\Form\Field;

use Akeneo\Connector\Helper\Config;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\Data\Form\Element\Factory as ElementFactory;

/**
* @author Agence Dn'D <[email protected]>
* @copyright 2004-present Agence Dn'D
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.dnd.fr/
*/
class Gallery extends AbstractFieldArray
{
/**
* Initialise form fields
*
* @return void
*/
protected function _construct()
public function __construct(
private ElementFactory $elementFactory,
Context $context,
array $data = []
) {
parent::__construct($context, $data);
}

protected function _construct(): void
{
$this->addColumn('attribute', ['label' => __('Akeneo Attribute')]);
$this->_addAfter = false;
$this->addColumn('type', ['label' => __('Assign to')]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');

parent::_construct();
}

public function renderCellTemplate($columnName): string
{
if ($columnName !== 'type') {
return parent::renderCellTemplate($columnName);
}

$options = [
Config::PRODUCT_IMAGE_TYPE_ALL => __('All'),
Config::PRODUCT_IMAGE_TYPE_PARENT => __('Parent'),
Config::PRODUCT_IMAGE_TYPE_CHILD => __('Child'),
];
$element = $this->elementFactory->create('select');
$element->setForm(
$this->getForm()
)->setName(
$this->_getCellInputElementName($columnName)
)->setHtmlId(
$this->_getCellInputElementId('<%- _id %>', $columnName)
)->setValues(
$options
);

return str_replace("\n", '', $element->getElementHtml());
}
}
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,6 @@

### Version 104.3.6 :
* PGTO-427: Custom Resource Connection
*

### Version 104.3.7 :
* PGTO-430: Allow to assign an image to the parent or child only
27 changes: 23 additions & 4 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@ class Config
*/
protected $scopeConfig;

public const PRODUCT_IMAGE_TYPE_CHILD = 'child';

public const PRODUCT_IMAGE_TYPE_PARENT = 'parent';

public const PRODUCT_IMAGE_TYPE_ALL = 'all';

/**
* Config constructor
*
Expand Down Expand Up @@ -1441,9 +1447,17 @@ public function getFileImportColumns()
*
* @return array
*/
public function getMediaImportGalleryColumns()
public function getMediaImportGalleryColumns(?array $types = null): array
{
/** @var mixed[] $images */
if ($types === null) {
$types = [
self::PRODUCT_IMAGE_TYPE_CHILD,
self::PRODUCT_IMAGE_TYPE_PARENT,
self::PRODUCT_IMAGE_TYPE_ALL,
];
}

/** @var array $images */
$images = [];
/** @var string $config */
$config = $this->scopeConfig->getValue(self::PRODUCT_MEDIA_GALLERY);
Expand All @@ -1461,10 +1475,15 @@ public function getMediaImportGalleryColumns()
if (!isset($image['attribute']) || $image['attribute'] === '') {
continue;
}
$images[] = $image['attribute'];

$imageType = $image['type'] ?? self::PRODUCT_IMAGE_TYPE_ALL;

if (in_array($imageType, $types)) {
$images[] = $image['attribute'];
}
}

return $images;
return array_unique($images);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions Job/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3992,6 +3992,7 @@ public function importMedia(): void
$data = [
$columnIdentifier => '_entity_id',
'sku' => 'identifier',
'_type_id' => '_type_id',
];

/** @var mixed[] $stores */
Expand Down Expand Up @@ -4067,6 +4068,13 @@ public function importMedia(): void
/** @var string[] $medias */
$medias = [];

$childOnlyImages = $this->configHelper->getMediaImportGalleryColumns(
[ConfigHelper::PRODUCT_IMAGE_TYPE_CHILD]
);
$parentOnlyImages = $this->configHelper->getMediaImportGalleryColumns(
[ConfigHelper::PRODUCT_IMAGE_TYPE_PARENT]
);

/** @var array $row */
while (($row = $query->fetch())) {
/** @var int $positionCounter */
Expand All @@ -4086,6 +4094,14 @@ public function importMedia(): void
continue;
}

if (in_array($row['_type_id'], ['simple', 'virtual']) && in_array($image, $parentOnlyImages)) {
continue;
}

if (in_array($row['_type_id'], ['configurable', 'grouped', 'bundle']) && in_array($image, $childOnlyImages)) {
continue;
}

if (!isset($medias[$row[$image]])) {
$medias[$row[$image]] = $this->akeneoClient->getProductMediaFileApi()->get(
$row[$image]
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"nyholm/psr7": "^1.5"
},
"type": "magento2-module",
"version": "104.3.6",
"version": "104.3.7",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down

0 comments on commit 1815946

Please sign in to comment.