Jump to content

Inputfield::renderReady


Recommended Posts

I'm having some troubles with using the renderReady method of inputfields.

The first problem that came up was discussed with @adrian here: https://github.com/baumrock/RockAdminTweaks/commit/1c327039f22138725c2dfdc9e659525f2f59011b#commitcomment-142442415

It looks like the renderReady() method is never called for InputfieldCheckboxes.

Another problem came up today when developing RockGrid. That inputfield needs some JS+CSS assets to be loaded whenever an inputfield of this type is rendered in the backend. I thought loading the assets in renderReady would be the right thing to do. But it does not work if the field is inside a repeater. I used this hook to fix this:

<?php
  public function init(): void
  {
    wire()->addHookAfter('Inputfield::render',              $this, 'preloadRockGrid');
  }

  protected function preloadRockGrid(HookEvent $event): void
  {
    // if it's not the repeater field exit
    $f = $event->object;
    if ($f->name !== self::field_variations) return;
    
    // loop through all fields of the repeater and load their assets
    foreach ($f->hasField->fields as $fname) {
      $field = wire()->fields->get($fname);
      $inputfield = $field->getInputfield($event->process->getPage());
      $inputfield->renderReady();
    }
  }

This works but it is really hacky and I'd like to have a proper solution for the problem and try to understand better what's going on and why things do not work as expected.

Can anybody shed some light on this?

Link to comment
Share on other sites

Thx @adrian I have to clarify this:

8 hours ago, bernhard said:

Another problem came up today when developing RockGrid. That inputfield needs some JS+CSS assets to be loaded whenever an inputfield of this type is rendered in the backend. I thought loading the assets in renderReady would be the right thing to do. But it does not work if the field is inside a repeater.

In my inputfield (InputfieldRockGrid, but could be any inputfield) I have added a method "renderReady" like so:

<?php
  public function renderReady(Inputfield $parent = null, $renderValueMode = false)
  {
    $grid = rockgrid()->getGrid($this);
    if (!$grid) return;
    wire()->modules->get('JqueryUI')->use('modal');

    // load all dependencies
    // this can safely be called multiple times
    // it will only load the assets once
    $this->wire->config->scripts->add(
      rockgrid()->url(__DIR__ . '/lib/tabulator.min.js', true)
    );
    
    // some more assets here

    return parent::renderReady($parent, $renderValueMode);
  }

I always thought this would be the way to go when creating inputfields/fieldtypes, but renderReady is never triggered. That's why I created the hook shown in the first post, which is executed on the parent's "render".

Link to comment
Share on other sites

  • 2 weeks later...

@bernhardTwo things I can spot here:

1. You are calling renderReady() from a hook after Inputfield::render. That is too late. What @adrian mentioned about hooking Inputfield::renderReadyHook() instead would likely enable you to do what you want to do. 

2. Your renderReady() method has if(!$grid) return; and you would need to change that to if(!$grid) return parent::renderReady($parent, $renderValueMode); 

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...