Jump to content

Hook into a field's output? TextFormatters?


owzim
 Share

Recommended Posts

Hi,

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.

Possible?

Link to comment
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:

$page->some_field->of(true);

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.

Link to comment
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:

/site/modules/TextformatterOzwim.module

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
Link to comment
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/

Link to comment
Share on other sites

  • 3 years later...

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

https://processwire.com/blog/posts/more-repeaters-repeater-matrix-and-new-field-rendering/#processwire-3.0.5-introduces-field-rendering-with-template-files
https://processwire.com/blog/posts/processwire-3.0.7-expands-field-rendering-page-path-history-and-more/#field-rendering-with-template-files

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

  • Like 2
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

×
×
  • Create New...