About RuntimeMarkup

Allows rendering of custom markup within page editor and frontend via custom PHP snippet set in the field.

Category 1Admin Helpers
Admin helper modules are those that provide helpful tools, UI enhancements or optimizations to the admin panel (excluding Fieldtype or Inputfield modules).
Category 2Field Types
Fieldtype modules that represent a data type used by fields.
Category 3Other Modules
Modules that have a unique purpose not covered by existing categories.
Release StateStable
Should be safe for use in production environments. *
Authorkongondo
Module Version0.0.2
Class NameFieldtypeRuntimeMarkup
Compatibility2.4, 2.5, 2.6, 2.7, 3.0
Date AddedSeptember 2, 2015
Last UpdatedNovember 30, 2016
Recommended ByNew recommendations may take up to 1 day to appear.

Instructions

This module's files should be placed in /site/modules/FieldtypeRuntimeMarkup/
How to install or uninstall modules

README

FieldtypeRuntimeMarkup

This module allows for custom markup to be dynamically (PHP) generated and output within a page's edit screen (in Admin).

The value for the fieldtype is generated at runtime. No data is saved in the database. The accompanying InputfieldRuntimeMarkup is only used to render/display the markup in the page edit screen.

The field's value is accessible from the ProcessWire API in the frontend like any other field, i.e. it has access to $page and $pages.

##Screenshots

###Backend screenshot1.png screenshot2.png

###Frontend screenshot3.png

Install

  1. Copy the files for this module to /site/modules/FieldtypeRuntimeMarkup/ OR if running later versions of ProcessWire, install directly from the Modules screen.
  2. In admin: Modules > Check for new modules. Install Fieldtype > RuntimeMarkup.
  3. Create a new field of type RuntimeMarkup, and name it whatever you want. In our examples we named it "runtime_markup".
  4. Enter your custom PHP code in your field's Details tab and save the field.
  5. Your custom PHP code must be valid PHP code and must ONLY return a string or integer. Anything else (e.g. object or array) will result in an error.
  6. Add the field to a template at a desired location. For instance, if you want your markup to be rendered before all other fields when editing a page using that template, you would make sure your RuntimeMarkup field was at the very top of the list of fields in your template.
  7. Edit a page using that template to see the returned value.

API + Usage

###Backend (Admin-side)

Enter your custom PHP snippet in the Details tab of your field. Your code can be as simple or as complicated as you want as long as in the end you return a value that is not an array or an object or anything other than a string/integer.

FieldtypeRuntimeMarkup has access to $page (the current page being edited/viewed) and $pages.

You can use the included but blank InputfieldRuntimeMarkup.css and InputfieldRuntimeMarkup.js files to add styles and scripts respectively to customise your rendered markup in the page edit in the admin, i.e. the rendered InputfieldRuntimeMarkup.

A very simple example.

return 'Hello';

Simple example.

return $page->title;

Simple example with markup.

return '<h2>' . $page->title . '</h2>';

Another simple example with markup.

$out = '<h1>hello ';
$out .= $page->title;
$out .= '</h1>';
return $out;

A more advanced example.

$p = $pages->get('/about-us/')->child('sort=random');
return '<p>' . $p->title . '</p>';

An even more complex example.

$str ='';
if($page->name == 'about-us') {
  $p = $page->children->last();
  $str = "<h2><a href='{$p->url}'>{$p->title}</a></h2>";
}
else {
  $str = "<h2><a href='{$page->url}'>{$page->title}</a></h2>";
}
return $str;

###How to access the value of RuntimeMarkup in the frontend

Access the field on this page like any other field.

echo $page->runtime_markup;

Access the field on another page like any other field.

echo $pages->get('/about-us/')->runtime_markup;

##Warnings and Considerations

  • Although access to ProcessWire's Fields' admin pages is only available to Superusers, this Fieldtype will evaluate and run the custom PHP code entered and saved in the field's settings (Details tab). Utmost care should therefore be taken in making sure your code does not perform any CRUD operations (unless of course that's intentional).
  • The value for this fieldtype is generated at runtime and thus no data is stored in the database. This means that you cannot directly query a RuntimeMarkup field from $pages->find().

##Changelog

###Version 0.0.2Added markupValue() method for correctly rendering markup output in listers.

###Version 0.0.1Initial Release.

##Resources Support Forum

##LicenseGPL(2)

##Credits

This module was sponsored by Andrey Valiev

Comments

  • Pete

    Pete 1 year ago 50

    I can't recommend this highly enough. There were a few manual tasks I was performing in the ProcessWire dev directory (copying and pasting site links into isit.pw for example, as well as into the forums to see if a member has made any posts) and using this module I was able to neatly format links directly to both the forums and isit.pw and save time.

    There are a lot more usage scenarios beyond that of course, but anything that saves me time - as well as having to create my own fieldtype each time I want to do something like this - is a huge benefit!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.