Jump to content

Hook into a field's output? TextFormatters?

Recommended Posts


I was wondering if it is possible to hook some logic into the output of a certain field. System wide, front- and backend

I'm thinking TextFormatters.

What I want for example is to:

echo $page->some_fancy_field;

And in the background it is making some very individual calculations and other fancy stuff with other fields of that same $page.

The field itself holds no actual value.


Share this post

Link to post
Share on other sites

OK, I am one step closer to what I want to achieve, still learning this hooking stuff.

I put everything into a module, and add a hook into the init() function:

$this->addHookAfter('FieldtypeText::formatValue', $this, 'myFormattedTextField');
public function myFormattedTextField($event) {

	$page = $event->arguments(0); 
	$field = $event->arguments(1); 
	$fieldValue = $event->arguments(2);
	if((string)$field->type === "FieldtypeText" && $field->name === "my_special_field") {	
		$event->return = $page->title + " - modified Page Title ftw";

So here I have access to all field of the field's page, so I can fiddle around with that.

One thing that passed on by me is that all fields of that field's parent page have formatting off:


threw an error.

(edit: as Wanze stated it's $page->of(true), additionally I mixed something up)

The ultimate goal for me would be an extra field type, where I can set a custom php output via admin (like the PHP you can set as a selector for pages on a PageField).

If I am getting things wrong here, please let me know.

Share this post

Link to post
Share on other sites

As far as I know, you can't enable/disable outputFormatting on fields, but on pages.

Did you mean:


Share this post

Link to post
Share on other sites

Wanze, yes my bad. I mixed something up. Edited it out my post. Everything is working as expected in that case.

Share this post

Link to post
Share on other sites
if((string)$field->type === "FieldtypeText"

You can just do: if($field->type == 'FieldtypeText'. But it looks like you are intending to operate on the 'title' field? In that case, I think what you really want is FieldtypePageTitle. 

$event->return = $page->title + " - modified Page Title ftw";

The "+" won't concatenate strings in PHP. You want to use "." instead. 

With regard to everything else, it looks like you are on the right track. However, it might make more sense for you to just implement a Textformatter module instead, as this is exactly what they are designed for:


class TextformatterOwzim extends Textformatter {
  public static function getModuleInfo() {
    return array('title'=>'Ozwim','version'=>1,'summary'=>'Ozwim summary'); 
  public function formatValue($page, $field, &$value) {
    $value .= "- modified page title FTW";

Once you've got that, install it and select it as the Textformatter for your page title field (or whatever field you want to apply it to). 

  • Like 2

Share this post

Link to post
Share on other sites

The "+" won't concatenate strings in PHP. You want to use "." instead.

I know, I was writing that on the fly into the comment box. That happens a lot to me when I am switching between JS and PHP =)

Thanks, I will definitely make use of the Textformatter version/

Share this post

Link to post
Share on other sites

For future reference, only for front end though (as far as I'm aware of, because you output it slightly differently) there's field rendering template files


they can be a nice alternative for textformatters at least front-end wise, and maybe easier for newcomers :)

  • Like 2

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Hardoman
      Hello community,
      we have a website running version 3.0.118. The owner would like to have a watermark merged to the images, that are being uploaded in the CKEditor as a requirement.
      Image upload besides the CKEditor within galleries and single images works as a charm already. We also use croppable image 3 there. (PIM2)
      To realize this requirement, I thought of using a hook in the admin area. So, I read a lot in our forums and tested this by adding a hook into the ready.php file.
      $this->addHookAfter('InputfieldFile::fileAdded',function(HookEvent$event){ wire('log')->save('test','Image upload works'); ... The log entry is being created correctly. But when I try to use the pim/watermark-function like in a template, he cannot find the watermark-image anymore. Furthermore, when I try to get the page-id, it does not seem to be accessible, because the application does not seem to know how to reference it, or I dont know the right way to do so…
      So my questions are:
      Is this the right attempt at all or will there be another, better workaround? It seems, I cannot access the page object (of the content page) within this scope or file but I would need it to save the processed image inside the right files/id folder Would it be better to place the hook into the admin-template? (or admin.php)
        Thanks for any hints in advance. 🙂
    • By VeiJari
      Hello forum!
      I've yet again stumbled on a head-scratching situation. We have enabled the option on our articles template and events template that it skips the title adding part and goes straight to the form. This is what our customer wants. So when you add a new article or event it automatically names it temporary to "article-0000000" and same with event. Now the problem is that obviously after saving the form we want to change to page url or "name" to the title, like it's normally. 
      Now here's the code for the hook:
      wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
        $page = $hook->arguments(0);
        $newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
        $page->setAndSave('name', $newUrl);
      I get the correct page and the name and path changes when I log them, but when I try to save it. It just loads and then I get: 
      Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) This happens in sanitizer.php
      and then another error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0
      What is happening? Am I not suppose to use sanitizer in this way? When we made a temporary page object in out other hook, the sanitizer worked perfectly.
      Thanks for the help!
    • By VeiJari
      Hello forum! I'm trying to learn how to time up my functions by using lazycron but I can't get it fired up. 
      Here's my code in site/init.php:
      function setEventToRepeat(HookEvent $e) { $wire->log->message("kutsutaan cron"); $events = $wire->pages->find('template=tapahtuma'); die(var_dump($events)); $wire->log->message('Kaikki tapahtumat haettu:' .$events); }   $wire->addHook('LazyCron::every30Seconds', null, 'setEventToRepeat'); I get nothing in the logs and can't echo or dump anything
      Thanks for the help!
    • By louisstephens
      So I have been diving into hooks lately, and I am enjoying them thus far. However, I guess I am a bit stumped on how to achieve what I want too. I am trying to set up a hook that would create a new child page when the parent page is saved. However, when you save the parent page a second time, I just need to update the child page without creating multiple child pages. What would be the best way to go about this?
      So after rereading my post, I believe it is a bit vague so I will try to explain more. 
      The Goal:
      Create a page with a template "one". Once the page is created/saved => create a new child page with the template of "two" If the parent is saved anytime after, do nothing to the child page (limit the parent page to one child page) The parent page is really just being used to output content, whereas the child page is being used to pull out the some fields from the parent to be used elsewhere. I might have made this too complicated in my head.
    • By quickjeff
      Hi Guys,
      I am running a page template that contains a filter form that allows users to display results based on number of  comments. 
      Here is what I am thinking about doing: 
      -add new field called total_comments 
      -somehow populate this field every time a new comment is added or removed by admin with count($page->comments)
      -use form filter to display results that the selector form has found.
      Example: $selector = “total_comments>=$q”; 
      My question to everyone: Is this a good approach? If so how do I get another field to update based on the comment module adding or removing a comment? 
  • Create New...