0

I have a form UI component I'm using in the backend of a Magento 2.2 installation. I'd like to adjust some items across all elements within the fieldsets of this form.

In the pre-2.2 days, you could just override the Adminhtml\Block\Form class to specify in the _prepareLayout method:

    \Magento\Framework\Data\Form::setFieldsetRenderer(
        $this->getLayout()->createBlock(
            \My\Module\Block\Widget\Form\Renderer\Fieldset::class,
            $this->getNameInLayout() . '_fieldset'
        )
    );

But with the move to UiComponents, this way has been deprecated. So how do we replicate this within the new UiComponent XML declarations? Is it even possible within the XML, or will I have to use a plugin to change the renderers?

0

The original purpose for this was to override the "Use Default" checkbox when dealing with scoped attributes / values. The checkbox is checked if "isUseDefault" is set to true in the knockout view model.

So it turns out, the DataProvider generates the data model used in Knockout. By taking the logic needed and putting it in the DataProvider for my form, I was able to control whether the checkbox was checked or not.

For posterity's sake, here's what I did:

Within getAttributesMeta of my DataProvider class I added: $meta[$code]['isUseDefault'] = $this->getIsAttributeDefaultValue($attribute);

I then created the functionality to return a boolean as to whether the current value is the default value or not.

protected function getAttributeDefaultValue($attributeCode)
{
    if ($this->defaultValues === null) {
        $this->defaultValues = $this->scopeOverriddenValue->getDefaultValues(CategoryInterface::class, $this->getCurrentCategory());
    }

    return array_key_exists($attributeCode, $this->defaultValues) ? $this->defaultValues[$attributeCode] : false;
}

protected function getExistsStoreValueFlag($attributeCode)
{
    if ($this->storeValuesFlags === null) {
        return $this->scopeOverriddenValue->containsValue(
            CategoryInterface::class,
            $this->getCurrentCategory(),
            $attributeCode,
            $this->request->getParam($this->requestScopeFieldName, Store::DEFAULT_STORE_ID)
        );
    }

    return array_key_exists($attributeCode, $this->storeValuesFlags);
}

protected function getIsAttributeDefaultValue(Attribute $attribute)
{
    $attributeCode = $attribute->getAttributeCode();
    $defaultValue = $this->getAttributeDefaultValue($attributeCode);

    if (!$this->getExistsStoreValueFlag($attributeCode)) {
        return true;
    } elseif (
        $this->getCurrentCategory()->getData($attributeCode) == $defaultValue &&
        $this->getCurrentCategory()->getStoreId() != Store::DEFAULT_STORE_ID
    ) {
        return false;
    }
    if ($defaultValue === false && !$attribute->getIsRequired() && $this->getCurrentCategory()->getData($attributeCode)) {
        return false;
    }
    return $defaultValue === false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.