1

I created a custom attribute for product called keybrand, i allow this product attribute can be editable from magento 2 backend. Now i want to add the check box input next to this custom attribute input like this:

enter image description here

the checkbox should be next to the input and has some label, and the checkbox value can be accessed from the product save after observer, how can i achieve this?

asked Mar 30, 2020 at 8:56

2 Answers 2

2

Note: It's pretty easy. U cannot add checkbox via product_form.xml. You should do this via Modifier. https://devdocs.magento.com/guides/v2.4/howdoi/customize-modifier-class.html If link is not accessible - just read theory about modifiers.

Core example:

@see \Magento\CatalogUrlRewrite\Ui\DataProvider\Product\Form\Modifier\ProductUrlRewrite
@see vendor/magento/module-catalog-url-rewrite/etc/adminhtml/di.xml

These files adds checkbox Create Permanent Redirect for old URL:

Checkbox

How to add your own checkbox:

  1. In your module create vendor/yourmodule/etc/adminhtml/di.xml with content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
 <arguments>
 <argument name="modifiers" xsi:type="array">
 <item name="custom_name" xsi:type="array">
 <item name="class" xsi:type="string">Vendor\Yourmodule\Ui\DataProvider\Product\Form\Modifier\ProductUrlRewriteOptions</item>
 <item name="sortOrder" xsi:type="number">100</item>
 </item>
 </argument>
 </arguments>
 </virtualType>
</config>
  1. Create class Vendor\Yourmodule\Ui\DataProvider\Product\Form\Modifier\ProductUrlRewriteOptions
<?php
namespace Vendor\Yourmodule\Ui\DataProvider\Product\Form\Modifier;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Ui\Component\Form\Element\Checkbox;
use Magento\Ui\Component\Form\Element\DataType\Text;
use Magento\Ui\Component\Form\Field;
use Magento\Framework\Stdlib\ArrayManager;
/**
 * Class adds a checkbox "url_key_generate_rewrites_for_options" after input "url_key" for product form
 */
class ProductUrlRewriteOptions extends AbstractModifier
{
 public const URL_KEY_GENERATE_REWRITES_FOR_OPTIONS_NAME = 'url_key_generate_rewrites_for_options';
 /**
 * @var LocatorInterface
 */
 private $locator;
 /**
 * @var ArrayManager
 */
 private $arrayManager;
 /**
 * @param LocatorInterface $locator
 * @param ArrayManager $arrayManager
 */
 public function __construct(
 LocatorInterface $locator,
 ArrayManager $arrayManager
 ) {
 $this->locator = $locator;
 $this->arrayManager = $arrayManager;
 }
 /**
 * @inheritdoc
 */
 public function modifyMeta(array $meta)
 {
 //To get the product
 $product = $this->locator->getProduct();
 return $this->addUrlRewriteWithOptionsCheckbox($meta);
 }
 /**
 * @inheritdoc
 */
 public function modifyData(array $data)
 {
 return $data;
 }
 /**
 * Adding URL rewrite checkbox to meta
 *
 * @param array $meta
 * @return array
 */
 protected function addUrlRewriteWithOptionsCheckbox(array $meta)
 {
 //I guess u should specify your attribute here - to add checkbox after
 //but I've not tested it.
 $urlPath = $this->arrayManager->findPath(
 ProductAttributeInterface::CODE_SEO_FIELD_URL_KEY,
 $meta,
 null,
 'children'
 );
 if ($urlPath) {
 $containerPath = $this->arrayManager->slicePath($urlPath, 0, -2);
 $urlKey = $this->locator->getProduct()->getData('url_key');
 $meta = $this->arrayManager->merge(
 $containerPath,
 $meta,
 [
 'arguments' => [
 'data' => [
 'config' => [
 'component' => 'Magento_Ui/js/form/components/group',
 'label' => false,
 'required' => false,
 ],
 ],
 ],
 ]
 );
 $checkbox['arguments']['data']['config'] = [
 'componentType' => Field::NAME,
 'formElement' => Checkbox::NAME,
 'dataType' => Text::NAME,
 'description' => __('Generate URLs with options'),
 'dataScope' => self::URL_KEY_GENERATE_REWRITES_FOR_OPTIONS_NAME,
 'value' => $urlKey,
 ];
 $meta = $this->arrayManager->merge(
 $urlPath . '/arguments/data/config',
 $meta,
 ['valueUpdate' => 'keyup']
 );
 $meta = $this->arrayManager->merge(
 $containerPath . '/children',
 $meta,
 [self::URL_KEY_GENERATE_REWRITES_FOR_OPTIONS_NAME => $checkbox]
 );
 $meta = $this->arrayManager->merge(
 $containerPath . '/arguments/data/config',
 $meta,
 ['breakLine' => true]
 );
 }
 return $meta;
 }
}
answered Oct 28, 2020 at 13:11
0

for right value synchronization i also added this part:

'valueMap' => [
 'false' => '0',
 'true' => '1'
]
answered Aug 18, 2022 at 11:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.