Skip to content

Commit

Permalink
BB-22832: Multifile attribute is shown on product visibility page in …
Browse files Browse the repository at this point in the history
…back-office - 5.1 (#36428)
  • Loading branch information
dsyrvachov committed Dec 6, 2023
1 parent fb940cc commit cf94df7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 11 deletions.
Expand Up @@ -4,6 +4,10 @@

use Doctrine\Common\Util\ClassUtils;
use Oro\Bundle\AttachmentBundle\Helper\FieldConfigHelper;
use Oro\Bundle\EntityConfigBundle\Attribute\Entity\AttributeFamilyAwareInterface;
use Oro\Bundle\EntityConfigBundle\Config\Id\FieldConfigId;
use Oro\Bundle\EntityConfigBundle\Entity\FieldConfigModel;
use Oro\Bundle\EntityConfigBundle\Manager\AttributeManager;
use Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider;
use Oro\Bundle\EntityExtendBundle\Event\ValueRenderEvent;
use Oro\Bundle\UIBundle\Event\BeforeFormRenderEvent;
Expand All @@ -20,6 +24,9 @@ class MultiFileBlockListener
// default priority for view pages
const ADDITIONAL_SECTION_PRIORITY = 1200;

/** @var AttributeManager */
private $attributeManager;

/** @var ConfigProvider */
private $entityConfigProvider;

Expand All @@ -32,6 +39,11 @@ public function __construct(ConfigProvider $configProvider, TranslatorInterface
$this->translator = $translator;
}

public function setAttributeManager(AttributeManager $attributeManager): void
{
$this->attributeManager = $attributeManager;
}

public function onBeforeValueRender(ValueRenderEvent $event)
{
if (FieldConfigHelper::isMultiField($event->getFieldConfigId())) {
Expand All @@ -53,8 +65,7 @@ public function onBeforeViewRender(BeforeViewRenderEvent $event)
}

$className = ClassUtils::getClass($event->getEntity());

$fieldConfigs = $this->entityConfigProvider->getIds($className);
$fieldConfigs = $this->getConfigs($event->getEntity());
if (!$fieldConfigs) {
return;
}
Expand All @@ -66,9 +77,6 @@ public function onBeforeViewRender(BeforeViewRenderEvent $event)

foreach ($fieldConfigs as $fieldConfig) {
$fieldName = $fieldConfig->getFieldName();
if (!FieldConfigHelper::isMultiField($fieldConfig)) {
continue;
}
$config = $this->entityConfigProvider->getConfig($className, $fieldName);

$blockKey = $fieldName . '_block_section';
Expand Down Expand Up @@ -103,7 +111,7 @@ public function onBeforeFormRender(BeforeFormRenderEvent $event)

$className = ClassUtils::getClass($event->getEntity());

$fieldConfigs = $this->entityConfigProvider->getIds($className);
$fieldConfigs = $this->getConfigs($event->getEntity());
if (!$fieldConfigs) {
return;
}
Expand All @@ -115,9 +123,6 @@ public function onBeforeFormRender(BeforeFormRenderEvent $event)

foreach ($fieldConfigs as $fieldConfig) {
$fieldName = $fieldConfig->getFieldName();
if (!FieldConfigHelper::isMultiField($fieldConfig)) {
continue;
}
$config = $this->entityConfigProvider->getConfig($className, $fieldName);
$newBlockKey = $fieldName . '_block_section';

Expand All @@ -139,6 +144,35 @@ public function onBeforeFormRender(BeforeFormRenderEvent $event)
$event->setFormData($scrollData->getData());
}

/**
* @param object $entity
*
* @return FieldConfigModel[]
*/
private function getConfigs(object $entity): array
{
$configManager = $this->entityConfigProvider->getConfigManager();
$className = ClassUtils::getClass($entity);

$ids = $this->entityConfigProvider->getIds($className);
$multiIds = array_filter($ids, fn (FieldConfigId $id) => FieldConfigHelper::isMultiField($id));

if ($entity instanceof AttributeFamilyAwareInterface && $entity->getAttributeFamily()) {
$family = $entity->getAttributeFamily();
$familyFilter = function (FieldConfigId $id) use ($configManager, $family, $className) {
$config = $configManager->getFieldConfig('attribute', $className, $id->getFieldName());

return $config->get('is_attribute')
? $this->attributeManager->getAttributeByFamilyAndName($family, $id->getFieldName())
: true;
};

$multiIds = array_filter($multiIds, $familyFilter);
}

return $multiIds;
}

private function isFileOrImageField(string $type): bool
{
return in_array($type, [FieldConfigHelper::FILE_TYPE, FieldConfigHelper::IMAGE_TYPE], true);
Expand Down
2 changes: 2 additions & 0 deletions src/Oro/Bundle/AttachmentBundle/Resources/config/services.yml
Expand Up @@ -171,6 +171,8 @@ services:
arguments:
- '@oro_entity_config.provider.entity'
- '@translator'
calls:
- ['setAttributeManager', ['@oro_entity_config.manager.attribute_manager']]
tags:
- { name: kernel.event_listener, event: entity_form.render.before, method: onBeforeFormRender, priority: -260 }
- { name: kernel.event_listener, event: entity_view.render.before, method: onBeforeViewRender, priority: -260 }
Expand Down
Expand Up @@ -6,6 +6,7 @@
use Oro\Bundle\AttachmentBundle\Tests\Unit\Stub\Entity\TestEntity1;
use Oro\Bundle\EntityConfigBundle\Config\Config;
use Oro\Bundle\EntityConfigBundle\Config\Id\FieldConfigId;
use Oro\Bundle\EntityConfigBundle\Manager\AttributeManager;
use Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider;
use Oro\Bundle\EntityExtendBundle\Event\ValueRenderEvent;
use Oro\Bundle\UIBundle\Event\BeforeFormRenderEvent;
Expand All @@ -26,15 +27,23 @@ class MultiFileBlockListenerTest extends \PHPUnit\Framework\TestCase
/** @var ConfigProvider|\PHPUnit\Framework\MockObject\MockObject */
private $configProvider;

/** @var AttributeManager|\PHPUnit\Framework\MockObject\MockObject */
private $attributeManager;

/** @var MultiFileBlockListener */
private $listener;

protected function setUp(): void
{
$this->configProvider = $this->createMock(ConfigProvider::class);
$this->translator = $this->createMock(TranslatorInterface::class);
$this->attributeManager = $this->createMock(AttributeManager::class);

$this->listener = new MultiFileBlockListener($this->configProvider, $this->translator);
$this->listener = new MultiFileBlockListener(
$this->configProvider,
$this->translator,
);
$this->listener->setAttributeManager($this->attributeManager);
}

public function testOnBeforeValueRender()
Expand Down
12 changes: 12 additions & 0 deletions src/Oro/Bundle/UIBundle/Event/BeforeFormRenderEvent.php
Expand Up @@ -34,6 +34,8 @@ class BeforeFormRenderEvent extends Event
*/
protected $twigEnvironment;

protected ?string $pageId = null;

/**
* @param FormView $form
* @param array $formData
Expand All @@ -48,6 +50,11 @@ public function __construct(FormView $form, array $formData, Environment $twigEn
$this->entity = $entity;
}

public function setPageId(?string $pageId = null): void
{
$this->pageId = $pageId;
}

/**
* @return FormView
*/
Expand Down Expand Up @@ -84,4 +91,9 @@ public function getEntity()
{
return $this->entity;
}

public function getPageId(): ?string
{
return $this->pageId;
}
}
Expand Up @@ -177,7 +177,7 @@
{% endif %}
<div class="layout-content">
{% block content_data %}
{% set data = oro_form_process(data, form, entity) %}
{% set data = oro_form_process_with_page_identifier(data, form, entity, id) %}

{% if entity and data is defined and data.dataBlocks is defined %}
{% set dataBlocks = data.dataBlocks %}
Expand Down
17 changes: 17 additions & 0 deletions src/Oro/Bundle/UIBundle/Twig/UiExtension.php
Expand Up @@ -135,6 +135,11 @@ public function getFunctions()
[$this, 'processForm'],
['needs_environment' => true]
),
new TwigFunction(
'oro_form_process_with_page_identifier',
[$this, 'processFormWithPageIdentifier'],
['needs_environment' => true]
),
new TwigFunction(
'oro_form_additional_data',
[$this, 'renderAdditionalData'],
Expand Down Expand Up @@ -213,7 +218,19 @@ public function processForm(TwigEnvironment $environment, array $data, FormView
{
$event = new BeforeFormRenderEvent($form, $data, $environment, $entity);
$this->getEventDispatcher()->dispatch($event, Events::BEFORE_UPDATE_FORM_RENDER);
return $event->getFormData();
}

public function processFormWithPageIdentifier(
TwigEnvironment $environment,
array $data,
FormView $form,
?object $entity = null,
?string $pageId = null
): array {
$event = new BeforeFormRenderEvent($form, $data, $environment, $entity);
$event->setPageId($pageId);
$this->getEventDispatcher()->dispatch($event, Events::BEFORE_UPDATE_FORM_RENDER);
return $event->getFormData();
}

Expand Down

0 comments on commit cf94df7

Please sign in to comment.