This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

BobCasarini
BobCasarini

Following the documentation I have created a plugin where I extend the form of an external plugin. This is the code:

`

public function boot()
{
    ProductsController::extendFormFields(function ($form, $model, $context) {
        if (!$model instanceof Product) {
            return;
        }

        $form->addTabFields([
            'price_us' => [
                'label' => 'Dollar price',
                'span'   => 'auto',
                'tab'   => 'offline.mall::lang.product.general',
            ],
            'price_us_old' => [
                'label' => 'Old dollar price',
                'span'   => 'auto',
                'tab'   => 'offline.mall::lang.product.general',
            ]
        ]);
    });

`

And it works but all the forms inside that backend page are involved with the addiction of these two fields. How can I add these fields only for a specific tab form? For example the "general" tab?

Last updated

daftspunky
daftspunky

You may need to include this check:

if ($widget->isNested) return;

It will prevent nested forms like repeaters from also being extended.

Last updated

BobCasarini
BobCasarini

daftspunk said:

You may need to include this check:

if ($widget->isNested) return;

It will prevent nested forms like repeaters from also being extended.

Thanks for your answer @daftpunk, but where do I find the $widget variable? is it something inside the form variable? Inside the extendFormField method form the controller class I've $model, $context and $form variable

BobCasarini
BobCasarini

Ok found it. Instead of using the extendFormField method from then controller class I used the backend.form.extendFields event. My code now is that:

`

    Event::listen('backend.form.extendFields', function($widget) {
        // Only apply this listener when the Users controller is being used
        if (!$widget->getController() instanceof ProductsController) {
            return;
        }

        // Only apply this listener when the User model is being modified
        if (!$widget->model instanceof Product) {
            return;
        }

        // Only apply this listener when the Form widget in question is a root-level
        // Form widget (not a repeater, nestedform, etc)
        if ($widget->isNested) {
            return;
        }

        $widget->addTabFields([
            'price_us' => [
                'label' => 'Dollar price',
                'span' => 'auto',
                'tab' => 'offline.mall::lang.product.general',
            ],
            'price_us_old' => [
                'label' => 'Old dollar price',
                'span' => 'auto',
                'tab' => 'offline.mall::lang.product.general',
            ]
        ]);
    });

`

called inside the boot method of the plugin class. Thanks again @daftpunk for the hint

Last updated

daftspunky
daftspunky

Good stuff! Sorry, it should have been $form->isNested

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.