Advertisement
dotancohen

Untitled

Nov 23rd, 2022 (edited)
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 31.28 KB | Source Code | 0 0
  1. <?php
  2. namespace Dotancohen\Foo\Ui\DataProvider\Product\Form\Modifier;
  3.  
  4. use Magento\Catalog\Model\Locator\LocatorInterface;
  5. use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
  6.  
  7. class Features extends AbstractModifier {
  8.  
  9.     protected $_product;
  10.  
  11.     /**
  12.      * @param LocatorInterface $locator
  13.      */
  14.     public function __construct(LocatorInterface $locator)
  15.     {
  16.         $this->_product = $locator->getProduct();
  17.     }
  18.  
  19.  
  20.     public function modifyData(array $data) : array
  21.     {
  22.         $product_id = $this->_product->getId();
  23.         $data[$product_id]['product']['foo-and-bar-features'] = array (
  24.             0 => array (
  25.                 'option_id' => '66',
  26.                 'product_id' => $product_id,
  27.                 'type' => 'radio',
  28.                 'is_require' => '0',
  29.                 'sort_order' => '1',
  30.                 'title' => 'Bars',
  31.                 'display_name' => 'Bars',
  32.                 'values' => array (
  33.                     0 => array (
  34.                         'option_id' => $product_id,
  35.                         'sort_order' => '2',
  36.                         'title' => 'A5',
  37.                         'display_name' => 'A5',
  38.                         'price' => '2.000000',
  39.                         'enabled' => '1',
  40.                     ),
  41.                     1 => array (
  42.                         'option_id' => $product_id,
  43.                         'sort_order' => '3',
  44.                         'title' => 'A4',
  45.                         'display_name' => 'A4',
  46.                         'price' => '4.000000',
  47.                         'enabled' => '0',
  48.                     ),
  49.                 ),
  50.             ),
  51.             1 => array (
  52.                 'option_id' => '67',
  53.                 'product_id' => $product_id,
  54.                 'type' => 'radio',
  55.                 'is_require' => '0',
  56.                 'sort_order' => '2',
  57.                 'title' => 'Foos',
  58.                 'display_name' => 'Foos',
  59.                 'values' => array (
  60.                     0 => array (
  61.                         'option_id' => '67',
  62.                         'sort_order' => '1',
  63.                         'title' => 'Square',
  64.                         'display_name' => 'Square',
  65.                         'price' => '0.000000',
  66.                         'enabled' => '0',
  67.                     ),
  68.                     1 => array (
  69.                         'option_id' => '67',
  70.                         'sort_order' => '2',
  71.                         'title' => 'Landscape',
  72.                         'display_name' => 'Landscape',
  73.                         'price' => '0.000000',
  74.                         'enabled' => '1',
  75.                     ),
  76.                     2 => array (
  77.                         'option_id' => '67',
  78.                         'sort_order' => '3',
  79.                         'title' => 'Portrait',
  80.                         'display_name' => 'Portrait',
  81.                         'price' => '0.000000',
  82.                         'enabled' => '1',
  83.                     ),
  84.                 ),
  85.             ),
  86.         );
  87.  
  88.  
  89.         return $data;
  90.     }
  91.  
  92.  
  93.     public function modifyMeta(array $meta) : array
  94.     {
  95.         if ( !$this->_product ) {
  96.             return $meta;
  97.         }
  98.         $product_id = $this->_product->getId();
  99.  
  100.         $meta['exclude_modal'] = array (
  101.             'arguments' => array (
  102.                 'data' => array (
  103.                     'config' => array (
  104.                         'componentType' => 'modal',
  105.                         'dataScope' => '',
  106.                         'provider' => 'product_form.product_form_data_source',
  107.                         'ns' => 'product_form',
  108.                         'options' => array (
  109.                             'title' => 'Configure Particular Foo or Bar', /* How to Display the Foo ID or Bar ID here? */
  110.                             'buttons' => array (
  111.                                 0 => array (
  112.                                     'text' => 'Save',
  113.                                     'class' => 'action-primary',
  114.                                     'actions' => array (
  115.                                         0 => array (
  116.                                             'targetName' => 'index = product_form',
  117.                                             'actionName' => 'save',
  118.                                         ),
  119.                                         1 => 'closeModal',
  120.                                     ),
  121.                                 ),
  122.                             ),
  123.                         ),
  124.                     ),
  125.                 ),
  126.             ),
  127.             'children' => array (
  128.                 'modal_content' => array (
  129.                     'arguments' => array (
  130.                         'data' => array (
  131.                             'config' => array (
  132.                                 'autoRender' => false,
  133.                                 'componentType' => 'container',
  134.                                 'dataScope' => 'data.product',
  135.                                 'externalProvider' => 'data.product_data_source',
  136.                                 'ns' => 'product_form',
  137.                                 'render_url' => 'https://example.com/admin/mui/index/render/key/123abc/',
  138.                                 'realTimeLink' => true,
  139.                                 'behaviourType' => 'edit',
  140.                                 'externalFilterMode' => true,
  141.                                 'currentProductId' => $product_id,
  142.                             ),
  143.                         ),
  144.                     ),
  145.                     'children' => array (
  146.                         'modal-fieldset' => array (
  147.                             'arguments' => array (
  148.                                 'data' => array (
  149.                                     'config' => array (
  150.                                         'label' => 'Fieldset',
  151.                                         'componentType' => 'fieldset',
  152.                                         'dataScope' => 'foos_features',
  153.                                         'collapsible' => true,
  154.                                         'sortOrder' => 10,
  155.                                         'opened' => true,
  156.                                     ),
  157.                                 ),
  158.                             ),
  159.                             'children' => array (
  160.                                 /* These "Features" will be different for each Foo and Bar! How to get the Foo or Bar ID to invoke a method which will return the proper data? */
  161.                                 'first_feature' => array (
  162.                                     'arguments' => array (
  163.                                         'data' => array (
  164.                                             'config' => array (
  165.                                                 'label' => 'Feature the first',
  166.                                                 'dataScope' => 'first_feature',
  167.                                                 'componentType' => 'field',
  168.                                                 'dataType' => 'text',
  169.                                                 'formElement' => 'checkbox',
  170.                                                 'valueMap' => array (
  171.                                                     'true' => 1,
  172.                                                     'false' => 0,
  173.                                                 ),
  174.                                                 'sortOrder' => 10,
  175.                                             ),
  176.                                         ),
  177.                                     ),
  178.                                 ),
  179.                                 'second_feature' => array (
  180.                                     'arguments' => array (
  181.                                         'data' => array (
  182.                                             'config' => array (
  183.                                                 'label' => 'Feature the second',
  184.                                                 'dataScope' => 'second_feature',
  185.                                                 'componentType' => 'field',
  186.                                                 'dataType' => 'text',
  187.                                                 'formElement' => 'checkbox',
  188.                                                 'valueMap' => array (
  189.                                                     'true' => '1',
  190.                                                     'false' => '0',
  191.                                                 ),
  192.                                                 'sortOrder' => 20,
  193.                                             ),
  194.                                         ),
  195.                                     ),
  196.                                 ),
  197.                             ),
  198.                         ),
  199.                     ),
  200.                 ),
  201.             ),
  202.         );
  203.         /* */
  204.  
  205.         $meta['foos_and_bars'] = array (
  206.             'arguments' => array (
  207.                 'data' => array (
  208.                     'config' => array (
  209.                         'label' => 'Foos and Bars',
  210.                         'dataScope' => 'data.product',
  211.                         'componentType' => 'fieldset',
  212.                         'collapsible' => true,
  213.                         'opened' => true,
  214.                         'sortOrder' => 10,
  215.                     ),
  216.                 ),
  217.             ),
  218.             'children' => array (
  219.                 'foo-and-bar-features' => array (
  220.                     'arguments' => array (
  221.                         'data' => array (
  222.                             'config' => array (
  223.                                 'remove' => 'remove',
  224.                                 'addButtonLabel' => 'Add Option',
  225.                                 'componentType' => 'dynamicRows',
  226.                                 'component' => 'Magento_Catalog/js/components/dynamic-rows-import-custom-options',
  227.                                 'template' => 'ui/dynamic-rows/templates/collapsible',
  228.                                 'additionalClasses' => 'admin__field-wide',
  229.                                 'deleteProperty' => 'is_delete',
  230.                                 'deleteValue' => '1',
  231.                                 'addButton' => false,
  232.                                 'renderDefaultRecord' => false,
  233.                                 'columnsHeader' => false,
  234.                                 'collapsibleHeader' => true,
  235.                                 'sortOrder' => 30,
  236.                                 'dataProvider' => 'foo_product_custom_options_listing',
  237.                                 'imports' => array (
  238.                                     'insertData' => '${ $.provider }:${ $.dataProvider }',
  239.                                     '__disableTmpl' => array (
  240.                                         'insertData' => false,
  241.                                     ),
  242.                                 ),
  243.                             ),
  244.                         ),
  245.                     ),
  246.                     'children' => array (
  247.                         'record' => array (
  248.                             'arguments' => array (
  249.                                 'data' => array (
  250.                                     'config' => array (
  251.                                         'headerLabel' => 'New Option',
  252.                                         'componentType' => 'container',
  253.                                         'component' => 'Magento_Ui/js/dynamic-rows/record',
  254.                                         'positionProvider' => 'container_option.sort_order',
  255.                                         'isTemplate' => true,
  256.                                         'is_collection' => true,
  257.                                     ),
  258.                                 ),
  259.                             ),
  260.                             'children' => array (
  261.                                 'container_option' => array (
  262.                                     'arguments' => array (
  263.                                         'data' => array (
  264.                                             'config' => array (
  265.                                                 'label' => NULL,
  266.                                                 'componentType' => 'fieldset',
  267.                                                 'collapsible' => true,
  268.                                                 'sortOrder' => 10,
  269.                                                 'opened' => true,
  270.                                             ),
  271.                                         ),
  272.                                     ),
  273.                                     'children' => array (
  274.                                         'sort_order' => array (
  275.                                             'arguments' => array (
  276.                                                 'data' => array (
  277.                                                     'config' => array (
  278.                                                         'dataScope' => 'sort_order',
  279.                                                         'componentType' => 'field',
  280.                                                         'dataType' => 'number',
  281.                                                         'formElement' => 'hidden',
  282.                                                         'visible' => false,
  283.                                                         'sortOrder' => 40,
  284.                                                     ),
  285.                                                 ),
  286.                                             ),
  287.                                         ),
  288.                                         'container_common' => array (
  289.                                             'arguments' => array (
  290.                                                 'data' => array (
  291.                                                     'config' => array (
  292.                                                         'componentType' => 'container',
  293.                                                         'formElement' => 'container',
  294.                                                         'component' => 'Magento_Ui/js/form/components/group',
  295.                                                         'breakLine' => false,
  296.                                                         'showLabel' => false,
  297.                                                         'additionalClasses' => 'admin__field-group-columns admin__control-group-equal',
  298.                                                         'sortOrder' => 10,
  299.                                                     ),
  300.                                                 ),
  301.                                             ),
  302.                                             'children' => array (
  303.                                                 'option_id' => array (
  304.                                                     'arguments' => array (
  305.                                                         'data' => array (
  306.                                                             'config' => array (
  307.                                                                 'dataScope' => 'option_id',
  308.                                                                 'componentType' => 'field',
  309.                                                                 'formElement' => 'input',
  310.                                                                 'sortOrder' => 10,
  311.                                                                 'visible' => false,
  312.                                                             ),
  313.                                                         ),
  314.                                                     ),
  315.                                                 ),
  316.                                                 'display_name' => array (
  317.                                                     'arguments' => array (
  318.                                                         'data' => array (
  319.                                                             'config' => array (
  320.                                                                 'label' => 'Display Name',
  321.                                                                 'dataScope' => 'display_name',
  322.                                                                 'componentType' => 'field',
  323.                                                                 'dataType' => 'text',
  324.                                                                 'formElement' => 'input',
  325.                                                                 'sortOrder' => 20,
  326.                                                                 'validation' => array (
  327.                                                                     'required-entry' => false,
  328.                                                                 ),
  329.                                                                 'component' => 'Magento_Catalog/component/static-type-input',
  330.                                                                 'valueUpdate' => 'input',
  331.                                                                 'imports' => array (
  332.                                                                     'optionId' => '${ $.provider }:${ $.parentScope }.option_id',
  333.                                                                     'isUseDefault' => '${ $.provider }:${ $.parentScope }.is_use_default',
  334.                                                                     '__disableTmpl' => array (
  335.                                                                         'optionId' => false,
  336.                                                                         'isUseDefault' => false,
  337.                                                                     ),
  338.                                                                 ),
  339.                                                             ),
  340.                                                         ),
  341.                                                     ),
  342.                                                 ),
  343.                                                 'type' => array (
  344.                                                     'arguments' => array (
  345.                                                         'data' => array (
  346.                                                             'config' => array (
  347.                                                                 'label' => 'Display Location',
  348.                                                                 'dataScope' => 'display_location',
  349.                                                                 'componentType' => 'field',
  350.                                                                 'dataType' => 'text',
  351.                                                                 'formElement' => 'select',
  352.                                                                 'component' => 'Magento_Catalog/js/custom-options-type',
  353.                                                                 'elementTmpl' => 'ui/grid/filters/elements/ui-select',
  354.                                                                 'options' => array (
  355.                                                                     0 => array (
  356.                                                                         'label' => 'Before Editor',
  357.                                                                         'value' => 'before_editor',
  358.                                                                     ),
  359.                                                                     1 => array (
  360.                                                                         'label' => 'After Editor',
  361.                                                                         'value' => 'after_editor',
  362.                                                                     ),
  363.                                                                 ),
  364.                                                                 'disableLabel' => true,
  365.                                                                 'multiple' => false,
  366.                                                                 'selectedPlaceholders' => array (
  367.                                                                     'defaultPlaceholder' => '-- Please select --',
  368.                                                                 ),
  369.                                                                 'validation' => array (
  370.                                                                     'required-entry' => false,
  371.                                                                 ),
  372.                                                                 'values' => array (
  373.                                                                     0 => 'before_editor',
  374.                                                                     1 => 'after_editor',
  375.                                                                 ),
  376.                                                                 'indexes' => array (
  377.                                                                     0 => 'container_type_static',
  378.                                                                 ),
  379.                                                                 'sortOrder' => 30,
  380.                                                             ),
  381.                                                         ),
  382.                                                     ),
  383.                                                 ),
  384.                                             ),
  385.                                         ),
  386.                                         'values' => array (
  387.                                             'arguments' => array (
  388.                                                 'data' => array (
  389.                                                     'config' => array (
  390.                                                         'componentType' => 'dynamicRows',
  391.                                                         'component' => 'Magento_Ui/js/dynamic-rows/dynamic-rows',
  392.                                                         'additionalClasses' => 'admin__field-wide',
  393.                                                         'renderDefaultRecord' => false,
  394.                                                         'sortOrder' => 30,
  395.                                                     ),
  396.                                                 ),
  397.                                             ),
  398.                                             'children' => array (
  399.                                                 'record' => array (
  400.                                                     'arguments' => array (
  401.                                                         'data' => array (
  402.                                                             'config' => array (
  403.                                                                 'componentType' => 'container',
  404.                                                                 'component' => 'Magento_Ui/js/dynamic-rows/record',
  405.                                                                 'positionProvider' => 'sort_order',
  406.                                                                 'isTemplate' => true,
  407.                                                                 'is_collection' => true,
  408.                                                             ),
  409.                                                         ),
  410.                                                     ),
  411.                                                     'children' => array (
  412.                                                         'enabled' => array (
  413.                                                             'arguments' => array (
  414.                                                                 'data' => array (
  415.                                                                     'config' => array (
  416.                                                                         'label' => 'Enabled',
  417.                                                                         'dataScope' => 'enabled',
  418.                                                                         'componentType' => 'field',
  419.                                                                         'dataType' => 'text',
  420.                                                                         'formElement' => 'checkbox',
  421.                                                                         'sortOrder' => 5,
  422.                                                                         'valueMap' => array (
  423.                                                                             'true' => '1',
  424.                                                                             'false' => '0',
  425.                                                                         ),
  426.                                                                     ),
  427.                                                                 ),
  428.                                                             ),
  429.                                                         ),
  430.                                                         'display_name' => array (
  431.                                                             'arguments' => array (
  432.                                                                 'data' => array (
  433.                                                                     'config' => array (
  434.                                                                         'label' => 'Display Name',
  435.                                                                         'dataScope' => 'display_name',
  436.                                                                         'componentType' => 'field',
  437.                                                                         'dataType' => 'text',
  438.                                                                         'formElement' => 'input',
  439.                                                                         'sortOrder' => 10,
  440.                                                                         'validation' => array (
  441.                                                                             'required-entry' => false,
  442.                                                                         ),
  443.                                                                     ),
  444.                                                                 ),
  445.                                                             ),
  446.                                                         ),
  447.                                                         'price' => array (
  448.                                                             'arguments' => array (
  449.                                                                 'data' => array (
  450.                                                                     'config' => array (
  451.                                                                         'label' => 'Price',
  452.                                                                         'dataScope' => 'price',
  453.                                                                         'componentType' => 'field',
  454.                                                                         'dataType' => 'number',
  455.                                                                         'formElement' => 'input',
  456.                                                                         'component' => 'Magento_Catalog/js/components/custom-options-component',
  457.                                                                         'addbefore' => '$',
  458.                                                                         'template' => 'Magento_Catalog/form/field',
  459.                                                                         'sortOrder' => 20,
  460.                                                                         'validation' => array (
  461.                                                                             'validate-number' => true,
  462.                                                                         ),
  463.                                                                     ),
  464.                                                                 ),
  465.                                                             ),
  466.                                                         ),
  467.                                                         'sort_order' => array (
  468.                                                             'arguments' => array (
  469.                                                                 'data' => array (
  470.                                                                     'config' => array (
  471.                                                                         'dataScope' => 'sort_order',
  472.                                                                         'componentType' => 'field',
  473.                                                                         'dataType' => 'number',
  474.                                                                         'formElement' => 'hidden',
  475.                                                                         'visible' => false,
  476.                                                                         'sortOrder' => 50,
  477.                                                                     ),
  478.                                                                 ),
  479.                                                             ),
  480.                                                         ),
  481.  
  482.                                                         'cofigure_modal' => array (
  483.                                                             'arguments' => array (
  484.                                                                 'data' => array (
  485.                                                                     'config' => array (
  486.                                                                         'title' => 'Configure Foo',
  487.                                                                         'formElement' => 'container',
  488.                                                                         'componentType' => 'container',
  489.                                                                         'component' => 'Magento_Ui/js/form/components/button',
  490.                                                                         'actions' => [
  491.                                                                             [
  492.                                                                                 'targetName' => 'ns=product_form, index=exclude_modal',
  493.                                                                                 'actionName' => 'openModal',
  494.                                                                             ],
  495.                                                                         ],
  496.                                                                         'sortOrder' => 80,
  497.                                                                     ),
  498.                                                                 ),
  499.                                                             ),
  500.                                                         ),
  501.                                                     ),
  502.                                                 ),
  503.                                             ),
  504.                                         ),
  505.                                     ),
  506.                                 ),
  507.                             ),
  508.                         ),
  509.                     ),
  510.                 ),
  511.             ),
  512.         );
  513.  
  514.         return $meta;
  515.     }
  516.  
  517.  
  518. }
  519.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement