Jump to content

[SOLVED] How to manipulate page title for ONLY the default language?


PWaddict
 Share

Recommended Posts

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

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

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

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

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

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

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

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

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

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');
    }

});

 

  • Like 1
Link to comment
Share on other sites

  • PWaddict changed the title to [SOLVED] How to manipulate page title for ONLY the default language?

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