Override a core class mehod, possible with Dependency Injection?

I need to change the html markup of checkbox and radio inputs in order to allow a cross browser customization.

I know that active field has a ‘template’ option, but i want to avoid change the various views in my system using checkbox and dont want to customize all my gridviews and possible future widgets that render checkbox/radio.

Would bee good if I could override the ‘tag’ method of BaseHtml, so I can check if is a radio or checkbox and change the markup and all checkbox/radio used will have the new markup.

Is this possible with dependency injection? If not, I would like to know if I can use other solution that dont force me to change code in various places.

extend the ActiveField class and customize your controls and use your extended class throughout

Hi alirz23, thanks for the sugestion but this is the way I want to avoid (change code in various places). Also, extending ActiveField will not change the CheckboxColumn used in gridview.

As an example, you can configure the gridview pager in every view you has but if the config is the same in all views, is much better configue the pager once for all system with Dependency Injection: http://www.yiiframework.com/doc-2.0/guide-concept-di-container.html#practical-usage.

But I’m not sure if I can use Dependency Injection to override a core class method or how accomplish this.

Based on this link http://www.yiiframework.com/forum/index.php/topic/28979-how-to-override-yii-core-classes/ I’m thinking too about use a classMap/autoloader solution to instruct Yii to use my version of the Html or BaseHtml when requested. But if the same is possible with Dependency Injection maybe it can be made more easily.

You can’t override a single method but you can override a class with a class extending from original one. Create it and then put it in DI container under a name of the original class.

Hi samdark, maybe I’m missing something but I configured the container and nothing happens. The custom class is not being called. There is someting wrong with my code?




//file app/helpers/CustomBaseHtml.php

// >>>>>>> I've other classes in this namespace working properly


namespace app\helpers;


class CustomBaseHtml extends \yii\helpers\BaseHtml

{

    public static function tag($name, $content = '', $options = [])

    {

	var_dump($name);die;


        if ($name === null || $name === false) {

            return $content;

        }

        $html = "<$name" . static::renderTagAttributes($options) . '>';

		

        return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content</$name>";

    } 

}






// file config/web.php

// >>>>>>> I've other configurations working properly


$config = [

    [...]


    'container' => [

        'definitions' => [

            'yii\helpers\BaseHtml' => 'app\helpers\CustomBaseHtml'

        ]

    ],


    [...]

]



Name your class Html.

samdark, I tried your suggestion but nothing changed.




// file app/helpers/Html.php

namespace app\helpers;


class Html extends \yii\helpers\Html

{

    public static function tag($name, $content = '', $options = [])

    {

       [...]

    } 

}






    'container' => [

        'definitions' => [

            'yii\helpers\Html' => 'app\helpers\Html'

        ]

    ],



I tried this too




    'container' => [

        'definitions' => [

            'yii\helpers\BaseHtml' => 'app\helpers\Html'

        ]

    ],



And after, I tried this




// file app/helpers/Html.php

namespace app\helpers;

Html extends \yii\helpers\BaseHtml

[...]



Have you had success using this feature (class overriding with DI) before?

Oh, just noticed you’re trying with helpers. It’s a bit different in that case since these are static: http://www.yiiframework.com/doc-2.0/guide-helper-overview.html#customizing-helper-classes

It worked, thanks a lot samdark!!!