Jump to content

Module: RuntimeMarkup Fieldtype & Inputfield


kongondo

Recommended Posts

Hey @kongondo - these changes seem to work well and prevent any unnecessary re-rendering of the output.

In the fieldtype, I change these three methods to return true.

    public function ___sleepValue(Page $page, Field $field, $value) {
        //return $this->renderMarkup($page, $field);
        return true;
    }

    public function getLoadQuery(Field $field, DatabaseQuerySelect $query) {
        // prevent loading from DB
        return $query;
    }

    public function ___loadPageField(Page $page, Field $field) {
        // generate value at runtime rather than loading from DB
        //return $this->renderMarkup($page, $field);
        return true;
    }

And in the inputfield I replace the ___render() method with the following which returns the wakeup value without re-rendering.

    public function ___render() {

        //if code field is empty return early
        if(!$this->runtimeFields) return;

        //so that $page and $pages are locally scoped to the eval
        $process = $this->wire('process');
        if($process && ($process->className() == 'ProcessPageEdit' || $process->className() == 'ProcessUser')) {
            $page = $process->getPage();
            return $page->{$this->name};
        }
        else {
            return;
        }

    }

Let me know if you have any questions / comments about this approach.

Thanks again for an invaluable module!

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

On 2/13/2019 at 6:20 PM, adrian said:

Let me know if you have any questions / comments about this approach.

Thanks @adrian. I'll have a look (but not sure when :)). I'm swamped all around!

 

If it's not too much to ask, could you please create an issue and reference your post above? Thanks.

Edited by kongondo
Link to comment
Share on other sites

  • 2 weeks later...

I've been using this field type for a while and very good it is too, but now I am scratching my head:

I have a runtime_markup field ('runtime_markup_expiryDate') with PHP as follows

return ($page->parent->name == 'memberships' and $page->latestSubscription()->id) ? date('d/m/Y', $page->latestSubscription()->subsEndDate) : 'Non-subscriber';

This works well in the back-end, but gives the wrong result in the API. If I retrieve the field in the API, it just gives 01/01/1970.

However, if I remove the formatting in the PHP, thus:

return ($page->parent->name == 'memberships' and $page->latestSubscription()->id) ? $page->latestSubscription()->subsEndDate : 'Non-subscriber';

it works correctly in the API (gives the right date, e.g. 30/09/2019) but obviously just gives the integer in the back end (e.g. 1569798000). The curious thing is that, in this case I can't work out where the formatting is applied in the API (which might be related to the cause of the problem)

What is going on? Any suggestions for a fix or work-round?

Thanks.

Link to comment
Share on other sites

  • 2 months later...

Update version 006 - 11 May 2019

Finally got a bit of time to work on this module.

Changelog

  • Namespacing: ProcessWire 3.x Support only from now on.
  • Removed the module's CSS and JS files. They are unnecessary.
  •  Switched to $files->render() instead of wireRenderFile().
  • Option to check for and optionally use InputfieldAceExtended for code highlighting in the PHP custom code textarea. Thanks to @adrian , @diogo and others who suggested this.
  • Option to specify number of rows for the PHP custom code textarea (@note: for some reason, it's not working with Ace Extended. It does work with plain textarea).
  • Removed the option (in field settings) to search for files to render under /site/modules/FieldTypeRuntimeMarkup/ as these could get overwritten during upgrades.
  • Refactored code to prevent unnecessary re-rendering of output, thanks @adrian
  • Render PHP file mode is now supported for frontend use as well, thanks @Noel Boss
  • Changed license to MIT.

Please note this is the new master and is available in the modules directory.

Screenshots

rtm_005_code_highlight_and_rows_config.thumb.jpg.2a390851d3dd63540b8828b87ed62c37.jpg

Over to you @bernhard ?.

 

Edited by kongondo
  • Like 6
Link to comment
Share on other sites

Btw, mainly for @bernhard and @adrian who have previously looked at RTM code, InputfieldRuntimeMarkup now behaves similar to many inputfields in that it gets its values from the Fieldtype's wakeupValue(), i.e. from $this->attr('value').  The previous implementation seemed a bit unclean to me when I looked at it recently. We had the inputfield doing its own eval() or wireRenderFile(). Now I've moved everything to FieldtypeRuntimeMarkup plus added a small helper class in there to handle such things. All the inputfield does now is serve values, as inputfields are meant to do.

  • Like 3
Link to comment
Share on other sites

  • 1 month later...

Hello @kongondo

I updated a site to v.0.0.6 and now when trying to edit a – seemingly unrelated – TextareaLanguage field by using the field overwrite editor modal of a template I get this:

Fatal Error: Uncaught Error: Class 'ProcessWire\RuntimeMarkupUtilities' not found in .../site/modules/FieldtypeRuntimeMarkup/InputfieldRuntimeMarkup.module:68
Stack trace:
#0 .../wire/core/Modules.php(624): ProcessWire\InputfieldRuntimeMarkup->init()
#1 .../wire/core/Modules.php(1336): ProcessWire\Modules->initModule(Object(ProcessWire\InputfieldRuntimeMarkup), Array)
#2 .../wire/core/Modules.php(1193): ProcessWire\Modules->getModule('InputfieldRunti...')
#3 .../wire/core/Modules.php(1566): ProcessWire\Modules->get('InputfieldRunti...')
#4 .../wire/modules/Fieldtype/FieldtypeTextareaHelper.php(40): ProcessWire\Modules->find('className^=Inpu...')
#5 .../wire/modules/Fieldtype/FieldtypeTextarea.module(338): ProcessWire\FieldtypeTextareaHelper->getConfigInputfields(Object(Proce (line 68 of .../site/modules/FieldtypeRuntimeMarkup/InputfieldRuntimeMarkup.module)

This error message was shown because: site is in debug mode. ($config->debug = true; => /site/config.php). Administrator has been notified. Error has been logged.

I tried uninstalling the module (because the site does not use it yet anyway, meaning there is no field using RuntimeMarkup) and when I click on the uninstall button I also get: 

Class 'ProcessWire\RuntimeMarkupUtilities' not found 

However, if I create a RuntimeMarkup field then the error goes away in both cases described above. It looks like the case when there is no RuntimeMarkup filed in the system is not taken into account. So with a RuntimeMarkup field created, I could uninstall the module as the fatal error did not crop up in that case.

Also note that I find it strange that both FieldtypeRuntimeMarkup and InputfieldRuntimeMarkup show up as RuntimeMarkup in the module list, see the screenshots:

Spoiler

RuntimeMarkup-module-shots.thumb.gif.4f86c5662891c2fe7e6f38c6953bb6ff.gif

 

Edited by szabesz
Also see my next post below.
Link to comment
Share on other sites

21 hours ago, szabesz said:

Also note that I find it strange that both FieldtypeRuntimeMarkup and InputfieldRuntimeMarkup show up as RuntimeMarkup in the module list, see the screenshots:

Sorry, rookie mistake on my part :-). I had them both with identical titles. Corrected in 007.

21 hours ago, szabesz said:

I updated a site to v.0.0.6 and now when trying to edit a – seemingly unrelated – TextareaLanguage field by using the field overwrite editor modal of a template I get this:

Hmm. That's very strange. By 'overwrite editor modal', do you mean the one where you can overwrite a field's 'original' label, description, notes, etc for a specific template?

Are you able give me more details please?

  1. Which field values did you try to edit?
  2. Did the error crop up after saving or as soon as you opened the modal?
  3. ProcessWire version

Thanks.

  • Like 1
Link to comment
Share on other sites

11 minutes ago, kongondo said:

By 'overwrite editor modal', do you mean the one where you can overwrite a field's 'original' label, description, notes, etc for a specific template?

Yes.

11 minutes ago, kongondo said:

Are you able give me more details please?

I'd rather give you access to the site (as it is not yet live anyway) so that you can also use Tracy's file editor to poke around. What do you think?

BTW, I could not really uninstall the module, it was just FieldtypeRuntimeMarkup which seemingly went through the uninstall process, but PW – as expected – kept complaining about InputfieldRuntimeMarkup being used when I also clicked the uninstall button of that one. However, if I delete the RuntimeMarkup filed, I am back where I started.

Link to comment
Share on other sites

On 6/13/2019 at 1:36 PM, szabesz said:

I'd rather give you access to the site (as it is not yet live anyway) so that you can also use Tracy's file editor to poke around. What do you think?

OK, but before that, since another user has experienced some issues with the Utility Class, I think I'll just move the class to its own file and require it. We'll try that first to see if it solves your issue. Thanks.

  • Like 1
Link to comment
Share on other sites

  • 3 months later...
On 6/21/2019 at 10:41 AM, kongondo said:

OK, but before that, since another user has experienced some issues with the Utility Class, I think I'll just move the class to its own file and require it. We'll try that first to see if it solves your issue.

Hello @kongondo

Any news on this issue? I also run into it when trying to export a textarea field via the standard field export feature of the admin. I don't really need Runtime Markup, maybe you can give me a quick guide on how to remove it manually (inluding how to clean up the db if needed?).

On 6/13/2019 at 2:17 PM, kongondo said:

Corrected in 007.

Has this version been version published yet?

Thanks in advance.

Link to comment
Share on other sites

  • 2 months later...

Are there known limitations in the plugin?

For example i cannot resize an image.

Then I wanted to get the original image but I cannot get the url.
$image->url  gives me just the folder where the image is but not the file-name.
$image->basename gives me nothing.

Textfields are working as expected.

"PHP Runtime Code Mode" is set to "Paste PHP code".

Link to comment
Share on other sites

1 hour ago, 2hoch11 said:

For example i cannot resize an image.

Then I wanted to get the original image but I cannot get the url.
$image->url  gives me just the folder where the image is but not the file-name.
$image->basename gives me nothing.

Hard to tell without seeing some code. Maybe you image field is set to contain multiple images. In that case $image->url may not work. Could we see some code please.

1 hour ago, 2hoch11 said:

"PHP Runtime Code Mode" is set to "Paste PHP code".

Please note that it is advisable to use the option Render PHP file(s) instead.

Link to comment
Share on other sites

@kongondo

So in other words resizing images should work the same way like it works in the frontend…

However, now I have an external file (instead of pasting php in the backend) but there was no difference… then I added "first()" to the image and now it works as expected:

$imageResized = $image->first()->size(400,400);

This is pretty strange because I already have an output of this image field in the frontend and it works without "first()"…

Link to comment
Share on other sites

51 minutes ago, 2hoch11 said:

This is pretty strange because I already have an output of this image field in the frontend and it works without "first()"…

Aah. I see what is happening. It isn't strange, actually, but working as expected. When output formatting is off, the value of an image or file field is always a WireArray irrespective of the field's maximum files allowed or the setting in formatted value. There is a note on the field. Please see the screenshot on this post.

The reason you didn't need first() in the frontend is because output formatting is on there. In the backend, where your rtm external file is called, output formatting is off, hence the need for first(). You can test this by using TracyDebugger to dump the results of the image field in both your external rtm file and in the template file used in the frontend. You should see the backend dump returning Pageimages and the frontend one Pageimage as illustrated below.

bd(get_class($page->image));

Backend dump

single_image_of_off.png.a3609c441e27a2deece4ff05b7a70d39.png

Frontend dump

single_image_of_on.png.cfd1dd0022c15587455f5de9e5427fba.png

  • Thanks 1
Link to comment
Share on other sites

  • 2 months later...

I have been using this module successfully for some time. Now I have a more complex use case. I want to update the page (in the admin) using ajax, rather than having to manually select "save". However, in this case, the data is submitted OK but the RuntimeMarkup script does not run and so the page is not properly updated. Any ideas?

Thanks.

Edit. I'm not entirely sure if the problem just lies with this module since, although the ajax is returning 'success' the normal fields don't appear to update either.

Link to comment
Share on other sites

13 hours ago, MarkE said:

Edit. I'm not entirely sure if the problem just lies with this module since, although the ajax is returning 'success' the normal fields don't appear to update either.

Solved now - the problem is in the jquery ajax call - using method: with version 1.8.3 - it should be type: prior to 1.9. See 

 

Link to comment
Share on other sites

  • 2 weeks later...
14 hours ago, szabesz said:

I forgot to post here that I could uninstall the module by commenting out this line (as far as I can remember):

https://github.com/kongondo/FieldtypeRuntimeMarkup/blob/148626c7d3e9dc16cb9303c96b6f4c5b9ee96a75/InputfieldRuntimeMarkup.module#L68


// $this->rtmUtilities = new RuntimeMarkupUtilities();

 

Yes it work ! thanks a lot 

 

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...
On 3/27/2020 at 2:42 AM, kongondo said:

Sorry all, I haven't yet found time to act on this. I need time to investigate it properly.

Encountered this issue too. The following change to InputfieldRuntimeMarkup appears to have fixed the missing utilities class:

<?php

	public function init() {
		// parent::init();
		// get the helper class (included in FieldtypeRuntimeMarkup.module file)
        $this->classLoader->loadClass('FieldtypeRuntimeMarkup'); // add this line to ensure the RuntimeMarkupUtilities class is available
		$this->rtmUtilities = new RuntimeMarkupUtilities();
	}

Can now install, uninstall, create & delete fields, etc without problems.

I needed this module for a FormBuilder form but sadly the module's configurable fields aren't available in FormBuilder ?

  • 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
×
×
  • Create New...