Jump to content

Getting Fieldgroup specific settings for an Inputfield inside a hook


abdus
 Share

Recommended Posts

I'm trying to add a new option to InputfieldTextarea. Depending on that option, I want to change how the input is rendered. I also want to change this option depending on different templates and repeaters, meaning it can have different values for different fieldgroups.

I hooked into three methods:

$this->addHookBefore('InputfieldTextarea::render', $this, 'hookInputRender');
$this->addHookAfter('InputfieldTextarea::getConfigInputfields', $this, 'hookInputSettings');
$this->addHookAfter('InputfieldTextarea::getConfigAllowContext', $this, 'hookInputContext');

In hookInputSettings, I build the additional option

protected function hookInputSettings(HookEvent $e)
{
    /** @var InputfieldTextarea $field */
    $wrapper = $e->return;
    $field = $e->object;

    /** @var InputfieldSelect $font */
    $font = $this->modules->get('InputfieldSelect');
    $font->label = $this->_('Font');
    $font->name = 'fontFamily';
    $font->addOptions(self::fontOptions);
    $font->attr('value', $field->fontFamily);
    $wrapper->add($font);

    $e->return = $wrapper;
}

It shows up in field settings with no problem

59954b2f40fff_ClipboardImage.jpg.993c93e8ed909ec83569dd9d05565f2a.jpg

When I pick an option and save, it even shows up in the database. However, I cannot get the properties of that field in that fieldgroup context. Most other inputfields can get their inputfield settings because are inside a class that extends Inputfield, so $this->myOption works.

If I hook into FieldtypeTextarea::getConfigInputfields, it works too, because getConfigInputfields method is called with $this as its argument, inside hooks it's possible to access fieldgroup specific settings. But for Inputfield, it's not given any arguments, so hooking Inputfield::getConfigInputfields, you won't be able to get any information about the context.

// /wire/core/Field.php

public function ___getConfigInputfields() {
    // ...
    if(!$fieldgroupContext || count($allowContext)) {
        // ...
        try {
            $fieldtypeInputfields = $this->type->getConfigInputfields($this);
            // ...
        }
        // ...
    }

    $inputfields = $this->wire(new InputfieldWrapper());
    // ...
    if($inputfield) {
        if($fieldgroupContext) {
            $allowContext = array('visibility', 'collapsed', 'columnWidth', 'required', 'requiredIf', 'showIf');
            $allowContext = array_merge($allowContext, $inputfield->getConfigAllowContext($this));
        } 
        // ...
        $inputfieldInputfields = $inputfield->getConfigInputfields();
        // ...
    }
    // ...
}

So, as a solution I gave it $this as a parameter

// $inputfieldInputfields = $inputfield->getConfigInputfields();
$inputfieldInputfields = $inputfield->getConfigInputfields($this);

Now everything works without any hacks. It works in field settings, templates, repeaters just fine.

protected function hookInputSettings(HookEvent $e)
{
    // ...
    $field = $e->arguments(0);
    // ...
    $font->attr('value', $field->fontFamily); // WORKS!
    // ...
}

I wanted to write this post because it drove me mad last night. I guess the next step is to make a pull request.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...