Jump to content

Have a differeent title on a field across multiple templates?


OpenBayou
 Share

Recommended Posts

Hello,

This is minor question not really important. I have a field called 'item description' that I use on multiple templates. Is there a way to have a different title on that field depending on the template? Example: on template a, have the field title called Description and on template b, have the field called Example? Or is it easier to have a different field on different templates?

 

Thanks

Link to comment
Share on other sites

7 hours ago, OpenBayou said:

This is starting to become an awesome CMS of mine. Thanks.

...and that is just the tip of the iceberg ;)

just an example: put this inside your /site/ready.php

$this->addHookAfter('ProcessPageEdit::buildForm', function($event) {
    $form = $event->arguments(0);
    $field = $form->get('yourfieldname');
    $page = $event->object->getPage();

    if($page->template == 'template_a') $field->label = "this is field label on template a, it is " . date("d.m.Y H:i:s");
    elseif($page->template == 'template_b') $field->label = "this is field label on template b, it is " . date("d.m.Y H:i:s");
    else $field->label = "other template, it is " . date("d.m.Y H:i:s");
});

have fun with processwire :)

  • Like 1
Link to comment
Share on other sites

9 minutes ago, bernhard said:

 


$this->addHookAfter('ProcessPageEdit::buildForm', function($event) {

 

Still don't get used to the hooks. Any useful links? I do still prefer  the (maybe hard) way with just no hooks...

Link to comment
Share on other sites

kongondo beat me :)

it may seem complicated in the beginning, but once you get the concept it's really easy and straightforward. you just have to look a bit into the code to know what is going on behind the scenes. take my code example, it's easy:

// attach a hook whenever a page edit form is built
// this happens in the class "ProcessPageEdit" in method "buildform"
// see github how this method looks like: https://github.com/processwire/processwire/blob/master/wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module#L588-L595
$this->addHookAfter('ProcessPageEdit::buildForm', function($event) {
	// $event is the hookevent object; it holds all necessary data to execute all kinds of actions
	// https://github.com/processwire/processwire/blob/master/wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module#L591
	// here you see that the method gets 1 parameter and this parameter is the form, so if you want to get this form just do this:
    $form = $event->arguments(0);

	// to get the field of your form just do the following
	// see https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldForm.module
	// or https://processwire.com/talk/topic/2089-create-simple-forms-using-api/
    $field = $form->get('yourfieldname');

	// now we want to do something based on the page that is edited
	// that is a bit different than in template context because we are VIEWING an admin page, whereas we are EDITING a different page
	// we want to know the template of the EDITED page, so let's get it...
	// $event->object is the class where the hook is attached. in our case "ProcessPageEdit"
	// to get the edited page this class has an own method: https://github.com/processwire/processwire/blob/master/wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module#L2009-L2019
    $page = $event->object->getPage();

    if($page->template == 'template_a') $field->label = "this is field label on template a, it is " . date("d.m.Y H:i:s");
    elseif($page->template == 'template_b') $field->label = "this is field label on template b, it is " . date("d.m.Y H:i:s");
    else $field->label = "other template, it is " . date("d.m.Y H:i:s");
});

 

  • Like 3
Link to comment
Share on other sites

20 minutes ago, Harmen said:

Still don't get used to the hooks. Any useful links? I do still prefer  the (maybe hard) way with just no hooks...

always depends what you want to do. only changing the field label on different templates would perfectly be fine without a hook. adding date+time would not be possible without a hook

i just wanted to give a simple example what could be the next step ;)

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