PWaddict Posted September 1, 2020 Share Posted September 1, 2020 I'm using the following hook in site/ready.php to manipulate page title in admin section and it work fine in localhost but I just noticed that it doesn't work on live server. I'm always getting "Untitled" even though all 4 fields have values. $wire->addHookAfter("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments[0]; if($page->template->name != 'mytemplate') return; if($page->myfield_text_a && $page->myfield_text_b && $page->myfield_options && $page->myfield_date) { $page->title = $page->myfield_text_a . ", " . $page->myfield_text_b . ", " . $page->myfield_options->title . ", " . $page->getFormatted("myfield_date"); } else { $page->title = "Untitled"; } }); How is this possible??? Link to comment Share on other sites More sharing options...
PWaddict Posted September 1, 2020 Author Share Posted September 1, 2020 On localhost where it's working properly I'm having 3 languages. On live server I have only 1 language and after I delete the other 2 additional languages on localhost the problem now happens there too. Link to comment Share on other sites More sharing options...
PWaddict Posted September 1, 2020 Author Share Posted September 1, 2020 How to manipulate page title on a hook after page save for ONLY the default language? Link to comment Share on other sites More sharing options...
clsource Posted September 1, 2020 Share Posted September 1, 2020 $defaultLang = wire("languages")->get("default"); $page->title->setLanguageValue($defaultLang, "New Value"); Link to comment Share on other sites More sharing options...
PWaddict Posted September 1, 2020 Author Share Posted September 1, 2020 It doesn't work. The title field is still empty. $wire->addHookAfter("Pages::saveReady", function(HookEvent $event) { if($page->template->name != 'mytemplate') return; $defaultLang = wire("languages")->get("default"); $page->title->setLanguageValue($defaultLang, "New Value"); }); Link to comment Share on other sites More sharing options...
Sergio Posted September 1, 2020 Share Posted September 1, 2020 14 minutes ago, PWaddict said: It doesn't work. The title field is still empty. Your code seems to be missing one line: $wire->addHookAfter("Pages::saveReady", function(HookEvent $event) { $page = $event->object; //this line if($page->template->name != 'mytemplate') return; $defaultLang = wire("languages")->get("default"); $page->title->setLanguageValue($defaultLang, "New Value"); }); Link to comment Share on other sites More sharing options...
PWaddict Posted September 1, 2020 Author Share Posted September 1, 2020 Still not working... Link to comment Share on other sites More sharing options...
psy Posted September 1, 2020 Share Posted September 1, 2020 Some things to try as results can vary depending on the PHP version: Be more explicit in your if statement, and turning off outputting formatting: <?php if(!empty($page->myfield_text_a) && !empty($page->myfield_text_b) && !empty($page->myfield_options) && !empty($page->myfield_date)) { $page->of(false); //.... } Link to comment Share on other sites More sharing options...
PWaddict Posted September 1, 2020 Author Share Posted September 1, 2020 Not working... Link to comment Share on other sites More sharing options...
clsource Posted September 1, 2020 Share Posted September 1, 2020 May be this can work? https://processwire.com/api/ref/pages-type/save-ready/ $wire->addHookBefore("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments(0); if($page->template->name != 'mytemplate') return; $defaultLang = wire("languages")->get("default"); $page->title->setLanguageValue($defaultLang, "New Value"); $event->arguments(0, $page); }); Link to comment Share on other sites More sharing options...
PWaddict Posted September 1, 2020 Author Share Posted September 1, 2020 7 minutes ago, clsource said: May be this can work? https://processwire.com/api/ref/pages-type/save-ready/ $wire->addHookBefore("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments(0); if($page->template->name != 'mytemplate') return; $defaultLang = wire("languages")->get("default"); $page->title->setLanguageValue($defaultLang, "New Value"); $event->arguments(0, $page); }); Thanks, this seems to work if I have 2 languages but it doesn't work if I have only 1 and I don't understand why cause it's the default language for both cases. I need it to work with 1 or additional languages. Link to comment Share on other sites More sharing options...
clsource Posted September 2, 2020 Share Posted September 2, 2020 1 hour ago, PWaddict said: Thanks, this seems to work if I have 2 languages but it doesn't work if I have only 1 and I don't understand why cause it's the default language for both cases. I need it to work with 1 or additional languages. https://processwire.com/api/ref/languages/get-default/ $wire->addHookBefore("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments(0); if($page->template->name != 'mytemplate') return; if(count(wire("languages")) > 1) { $defaultLang = wire("languages")->getDefault(); $page->title->setLanguageValue($defaultLang, "New Value"); } else { $page->title = "New value"; } $event->arguments(0, $page); }); Link to comment Share on other sites More sharing options...
PWaddict Posted September 2, 2020 Author Share Posted September 2, 2020 5 minutes ago, clsource said: https://processwire.com/api/ref/languages/get-default/ $wire->addHookBefore("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments(0); if($page->template->name != 'mytemplate') return; if(count(wire("languages")) > 1) { $defaultLang = wire("languages")->getDefault(); $page->title->setLanguageValue($defaultLang, "New Value"); } else { $page->title = "New value"; } $event->arguments(0, $page); }); That was I'm trying and it works but it doesn't work with only just 1 language when using fields to get the title value. $wire->addHookBefore("Pages::saveReady", function(HookEvent $event) { $page = $event->arguments(0); if($page->template->name != 'mytemplate') return; if(count(wire("languages")) > 1) { $defaultLang = wire("languages")->getDefault(); $page->title->setLanguageValue($defaultLang, $page->venue . ", " . $page->city . ", " . $page->country->title . ", " . $page->getFormatted("date")); } else { $page->title = $page->venue . ", " . $page->city . ", " . $page->country->title . ", " . $page->getFormatted("date"); } $event->arguments(0, $page); }); Link to comment Share on other sites More sharing options...
PWaddict Posted September 2, 2020 Author Share Posted September 2, 2020 If I use the following hook, with 2 languages works fine but with 1 language I have to press the Save button twice for the title field to get updated: $wire->addHookBefore('ProcessPageEdit::processInput', function(HookEvent $event) { $page = $event->object->getPage(); if($page->template == "mytemplate") { $page->title = $page->venue . ", " . $page->city . ", " . $page->country->title . ", " . $page->getFormatted("date"); } }); ? Link to comment Share on other sites More sharing options...
PWaddict Posted September 2, 2020 Author Share Posted September 2, 2020 Nobody knows the proper way to achieve it with just 1 language? Link to comment Share on other sites More sharing options...
PWaddict Posted September 2, 2020 Author Share Posted September 2, 2020 This works properly: $this->pages->addHookAfter('save', function($event) { $page = $event->arguments[0]; if($page->template != 'mytemplate') return; if($page->venue && $page->city && $page->country && $page->date) { $page->setAndSave('title', $page->venue . ", " . $page->city . ", " . $page->country->title . ", " . $page->getFormatted("date")); } else { $page->setAndSave('title', 'Untitled'); } }); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now