Jump to content

Fieldtype Runtime Only


Robin S
 Share

Recommended Posts

This is a module that is similar in purpose to the Runtime Markup module. I made it primarily for my own use but am posting it here because I had a request to add it to the modules directory.

Fieldtype Runtime Only

Not a proper fieldtype because it doesn't save data to the database. The fieldtype only exists to provide a convenient way to add an inputfield to templates that will render some markup at runtime.

For a field named "my_field"...

  • Inputfield markup will be rendered from a file at /site/templates/RuntimeOnly/my_field.php. In addition to the standard ProcessWire variables this file receives:
    • $page - the page being edited.
    • $field - the Field object.
    • $inputfield - the Inputfield object.
  • JS file /site/templates/RuntimeOnly/my_field.js will be added to admin if that file exists.
  • CSS file /site/templates/RuntimeOnly/my_field.css will be added to admin if that file exists.

Tips

If the RuntimeOnly field is used inside a Repeater field then you can get the Repeater page it is on via $inputfield->hasPage. E.g.

$repeater_page = $inputfield->hasPage;
// Use $repeater_page as needed
echo "The name of the repeater page is $repeater_page->name";

 

https://github.com/Toutouwai/FieldtypeRuntimeOnly
https://modules.processwire.com/modules/inputfield-runtime-only/

  • Like 9
Link to comment
Share on other sites

  • 11 months later...

It may not be a 'proper' fieldtype, but it is very useful and a key part of my ProcessDbMigrate module.

However, I have a very slight problem which is really puzzling me. My module gets data from the database and exports to .json files (among other things). I use the standard Field::getExportData() for getting field data. The curious thing is, for certain fields, I get a different result from getExportData() depending on whether it is called from inside my module or from a RuntimeOnly script.

For example, the field phits (from the Page Hit Counter module) has visibility 'hidden' (i.e. collapsed = 4). Calling getExportData() from my module (see code below) returns the correct result.

$field = $this->wire()->fields->get('phits');
bd($field->getExportData(), 'getExportData in module class');

Calling it from with a RuntimeOnly script (see code below) returns collapsed = 0.

$field = $fields->get('phits');
bd($field->getExportData(), 'getExportData in RuntimeOnly');

See pic below for the Tracy debug:

312955781_FieldgetExportDataissueinRuntimeOnly.thumb.jpg.fa23186828b3e5d504b87e8f97326133.jpg

Examining the situation in a bit more detail shows the following:

  1. Field::getExportData() calls Fieldtype::exportConfigData() viz. $typeData = $this->type->exportConfigData($this, $data); and then merges the data, viz. $data = array_merge($data, $typeData);
  2. Fieldtype::exportConfigData() calls Inputfield::exportConfigData(), viz. $data = $inputfield->exportConfigData($data);

It is the second call which returns collapsed = 0 rather than collapsed = 4. It is arguable that the array_merge should be reversed - i.e. $data = array_merge($typeData, $data); so that the generic data does not over-ride the specific - not that this really helps, as I have no idea what is causing the problem in the first place.

Any ideas? In the meantime, I guess I will need to do a work-round by trying to move the code out of the RuntimeOnly script (note that the actual code I am using is much broader than the 'phits' snippet above - I have just used that to illustrate the problem).

Link to comment
Share on other sites

6 hours ago, MarkE said:

Calling it from with a RuntimeOnly script (see code below) returns collapsed = 0.

I can't reproduce that. It's showing collapsed => 4 when I dump the field in a FieldtypeRuntimeOnly render file.

2021-06-18_162817.png.a5f1f73d5699bb65b7018a99183fec65.png

This module doesn't do anything regarding inputfield visibility so I think your issue must be a general one relating to the PW API rather than specific to this module. Really all this module is doing is rendering a TemplateFile via $files->render().

One thing though... take note of the readme regarding the variables supplied to the render file:

On 6/21/2020 at 6:39 PM, Robin S said:

In addition to the standard ProcessWire variables this file receives:

  • $page - the page being edited.
  • $field - the Field object.
  • $inputfield - the Inputfield object.

I don't know if it relates to your issue but I suggest you avoid overwriting the $field variable by choosing a different name for the field you are getting.

  • Like 1
Link to comment
Share on other sites

6 hours ago, MarkE said:

a key part of my ProcessDbMigrate module.

Given how simple FieldtypeRuntimeOnly is, it might be good to avoid the dependency in your module and just render your own markup directly. I haven't looked closely at your module but it just seems like it would make sense to keep it self-contained and avoid any fragility due to the chance that FieldtypeRuntimeOnly changes in the future.

  • Like 1
Link to comment
Share on other sites

1 hour ago, Robin S said:

Given how simple FieldtypeRuntimeOnly is, it might be good to avoid the dependency in your module

Good idea. I used it as a shortcut. I’m just completing the planned functionality of my module, after which some tidying up will follow, so I’ll take the opportunity then. 

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...