11

Like this thread said: Override abstract class in Magento 2 in Magento 1, I can just create a full new class. In Magento 2, we should use plugins instead, but plugins only allow me to modified existent methods. What if I need to add a new method? Example: this class vendor/magento/module-ui/Component/AbstractComponent.php, has an array of components: $components, there is no function to unset/delete elements for that array. So how can I create that function?

0

I don't see how you can do that without completely overriding the class. In the case of your example, you can disable individual components by setting the "disabled" item to the "data" argument in the XML. For instance:

<?xml version="1.0" encoding="UTF-8"?>

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="title">
            <argument name="data" xsi:type="array">
                <item name="disabled" xsi:type="boolean">true</item>
            </argument>
        </field>
    </fieldset>
</form>

This effectively removes 'title' from the $components array.

This is due to the createChildComponent method in the Magento\Framework\View\Element\UiComponentFactory class:

 protected function createChildComponent(
        array $bundleComponents,
        ContextInterface $renderContext,
        $identifier
    ) {
        list($className, $arguments) = $this->argumentsResolver($identifier, $bundleComponents);
        if (isset($arguments['data']['disabled']) && (int)$arguments['data']['disabled']) {
            return null;
        }
        $components = [];
        foreach ($bundleComponents['children'] as $childrenIdentifier => $childrenData) {
            $children = $this->createChildComponent(
                $childrenData,
                $renderContext,
                $childrenIdentifier
            );
            $components[$childrenIdentifier] = $children;
        }
        $components = array_filter($components);
        $arguments['components'] = $components;
        if (!isset($arguments['context'])) {
            $arguments['context'] = $renderContext;
        }

        return $this->objectManager->create($className, $arguments);
    }
  • This is not what I'm looking for... I want a way to add new methods to an Abstract class... this is just an example... for example, what if I want to remove the elements dynamically? In your comment you mention "complete overriding" how you do that?? – Matias Aug 23 '16 at 20:48
  • Then you'll need to define your new methods in a class that extends the abstract class and then create classes for the abstract class's subclasses that instead inherit from your class and set preferences in di.xml. That's what I mean by 'completely overriding the class.' I was trying to show an example of how to avoid doing that. – Aaron Allen Aug 23 '16 at 21:05
  • Yes, I understand you... but the solution is not scalable at all... I cannot believe that M2 removed the possibility of overriding abstract classes... I thought that they were going to improve it, instead of removing it... – Matias Aug 24 '16 at 12:25
0

You can use class preference ,using before, around and after in the required method . Please check the docs for further details http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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