AddFieldsToAttributeObserver.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Observer;
  7. use Magento\Config\Model\Config\Source;
  8. use Magento\Framework\Module\Manager;
  9. use Magento\Framework\Event\Observer as EventObserver;
  10. use Magento\Framework\Event\ObserverInterface;
  11. /**
  12. * Observer model
  13. */
  14. class AddFieldsToAttributeObserver implements ObserverInterface
  15. {
  16. /**
  17. * @var \Magento\Config\Model\Config\Source\Yesno
  18. */
  19. protected $yesNo;
  20. /**
  21. * @var \Magento\Framework\Module\Manager
  22. */
  23. protected $moduleManager;
  24. /**
  25. * @param Manager $moduleManager
  26. * @param Source\Yesno $yesNo
  27. */
  28. public function __construct(Manager $moduleManager, Source\Yesno $yesNo)
  29. {
  30. $this->moduleManager = $moduleManager;
  31. $this->yesNo = $yesNo;
  32. }
  33. /**
  34. * @param \Magento\Framework\Event\Observer $observer
  35. * @return void
  36. */
  37. public function execute(EventObserver $observer)
  38. {
  39. if (!$this->moduleManager->isOutputEnabled('Magento_Swatches')) {
  40. return;
  41. }
  42. /** @var \Magento\Framework\Data\Form $form */
  43. $form = $observer->getForm();
  44. $fieldset = $form->getElement('base_fieldset');
  45. $yesnoSource = $this->yesNo->toOptionArray();
  46. $fieldset->addField(
  47. 'update_product_preview_image',
  48. 'select',
  49. [
  50. 'name' => 'update_product_preview_image',
  51. 'label' => __('Update Product Preview Image'),
  52. 'title' => __('Update Product Preview Image'),
  53. 'note' => __('Filtering by this attribute will update the product image on catalog page'),
  54. 'values' => $yesnoSource,
  55. ],
  56. 'is_filterable'
  57. );
  58. $fieldset->addField(
  59. 'use_product_image_for_swatch',
  60. 'select',
  61. [
  62. 'name' => 'use_product_image_for_swatch',
  63. 'label' => __('Use Product Image for Swatch if Possible'),
  64. 'title' => __('Use Product Image for Swatch if Possible'),
  65. 'note' => __('Allows use fallback logic for replacing swatch image with product swatch or base image'),
  66. 'values' => $yesnoSource
  67. ],
  68. 'is_filterable'
  69. );
  70. }
  71. }