Jump to content

[deprecated] RockMarkup2 - Inputfield to easily include any PHP/HTML/JS/CSS in the PW Admin


bernhard
 Share

Recommended Posts

Please don't use this module any more. I think in the end it just adds more complexity (and dependencies) than benefits. See this tutorial how simple it is to create a custom runtime-only Inputfield:

WHY?

I've started building this module because the existing solutions by @kongondo and @kixe (https://modules.processwire.com/modules/fieldtype-runtime-markup/ and https://github.com/kixe/FieldtypeMarkup) did not exactly fit my needs. Actually this module is aimed to be a base module that can easily be extended by other modules. It takes care of the heavy lifting that has to be done when working with custom fieldtypes in ProcessWire (injecting scripts and styles, handling JS events, doing translations). See RockTabulator as an example. I'm quite sure more will follow (eg ChartJS)...

WHAT?

This module helps you injecting ANY php/html/js/css into any PW backend form (either on a page or in custom process modules). It also comes with a sandbox process module that helps you setup your fields and provides handy shortcuts that integrate with TracyDebugger and your IDE:

 

 

WHERE

...to get it? At the moment the module is released as early alpha and available only on github: https://github.com/BernhardBaumrock/RockMarkup2

 

If you have any questions or ideas please let me know ? 

PS: This module shows how easy it is to extend this module for your very own needs. All you need to do is providing the module's info arrays and then overwrite any methods that you have to modify (eg the InputField's render() method): https://github.com/BernhardBaumrock/RockMarkupExtensionExample

  • Like 12
Link to comment
Share on other sites

Can I save a value for the custom markup field and output it's value on the edit page?

Right now I have a calculation field whose value is calculated via JavaScript (depending on other fields). But now on page save, I want to save the calculated value to the field, so I can possibly output it in a Lister Pro column. Is this possible or can you make it possible? I think this would be a cool addition, because you can use the value on pages/listers, where the other dependend fields are not available.

Link to comment
Share on other sites

Well... the proper way would be to develop a fieldtype for that. Hacking this together with RockMarkup should be quite easy though: Just add a hidden inputfield for that purpose and populate it via JS whenever you update the value of the RockMarkup field. But you need to be careful with that approach because the value transmitted can be manipulated easily via browser devtools if you don't do server side validation!

  • Like 1
Link to comment
Share on other sites

On 7/23/2019 at 4:59 PM, jens.martsch said:

Right now I have a calculation field whose value is calculated via JavaScript (depending on other fields). But now on page save, I want to save the calculated value to the field

I guess this should be possible with a good old hook, too.

Link to comment
Share on other sites

@dragan Sure, this is possible with a hook and thats how I do it now (without using RockMarkup).

@bernhard It would be ideal, if RockMarkup could create an additional field on demand for saving the value (but then we should be able to select the options, like an integer with max 10 digits, for this hidden field), or if you could connect/link RockMarkup to an existing field, which has it's type and validation defined, because it is a regular ProcessWire field. So the result of a calculation would be saved in this field.

Right now I just experimented with RockMarkup and it worked as expected, but saving the value is stopping me from using it right now for this purpose. But I think I will have other uses for it in the future.

Link to comment
Share on other sites

18 minutes ago, jens.martsch said:

It would be ideal, if RockMarkup could create an additional field on demand for saving the value (but then we should be able to select the options, like an integer with max 10 digits, for this hidden field), or if you could connect/link RockMarkup to an existing field, which has it's type and validation defined, because it is a regular ProcessWire field. So the result of a calculation would be saved in this field.

I have to think about that... It was not built for such tasks, but it might be a good addition. We'd have to think about a new name for the module then ^^

Link to comment
Share on other sites

Pushed a new version yesterday that fixes the issue that .ready and .hooks files might be publicly readable thanks to @jens.martsch and @teppo

Please update the module to the latest master version and rename all your .ready and .hooks files to .ready.php and .hooks.php!

https://github.com/BernhardBaumrock/RockMarkup2/commit/e9c0bcf43d84078e36ee9e219d19b911c180e168

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...

this looks very promising! Can you give an example on how to render a page reference field with this module.
I tried it with this code in the the markup field:

<?php
// Render for frontend and Backend
if($page->block_page) echo "{$page->block_page->render()}";
?>

My template has the block_page as well as the markup field.

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 months later...

I realise this module is deprecated but it is in use on a site I'm working on. I needed to add a RM2 field into a repeater and it didn't work due to the unique nature of repeaters. A little hack to RM2 Inputfield module solved 2 problems at once. The Inputfield works in repeaters AND the repeater item ID is accessible in the parent page via the $session var.

<?php  
/**
   * Set the field content from the file with the same name
   */
  public function ___getContent() {
    $out = '';

    // get file
    $name = $this->name;

    /******  hack - breaks on repeater fields ******/
      if (strpos($name,'_repeater') !== false) {
          // it's in a repeater field
          $ary = explode('_repeater', $name);
          $name = $ary[0];
          // save the repeater item ID with the original fieldname as the key for future use
          wire('session')->set($name, $ary[1]);
      }
     /****** end of  hack - breaks on repeater fields ******/

    $file = $this->main()->getFile($name);
    if(!$file) return "No file found for field $name";
    
    // if a value was set return it
    if($this->value) $out = $this->value;
    else {
      // otherwise try to render the file
      try {
        // get page object
        $page = $this->page;

        if($this->process == 'ProcessPageEdit') {
          $page = $this->process->getPage();
        }

        // get markup
        $out = $this->files->render($file->path, [
          'inputfield' => $this,
          'rm' => $this->rm,
        ], [
          'allowedPaths' => [$file->path],
        ]);
      } catch (\Throwable $th) {
        $out = $th->getMessage();
      }
    }
    return $out;
  }

It's a bit rough but it works for me and maybe this tip can help others working with repeater fields.

 

 

  • Thanks 1
Link to comment
Share on other sites

Thanks for liking @bernhard

Another tip... not only doesn't it work, but who wants multiple CSS files for a repeater? In this scenario, the page template had another RM2 field - simple field on the main template. All CSS for the repeater RM2 field went into the other RM2 field's CSS file, not the repeater field's CSS.

I'm sure there are other ways to get the RM2-in-a-repeater's CSS to work - just needs thinking outside the square ? 

 

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