Jump to content

How do i overwrite title field using hook ?


picarica
 Share

Recommended Posts

so i started recently working on hooks i makde hook that after page create i upload .xml file and from that i get title into variable and i paste it into name(url) and title, the name(url) is replaced but title isnt? why is that

i have it like  this

  $page->title = $newtitle;
  $page->name = $newtitle;

and i have that in
 

$this->addHookAfter('Pages::saveReady', function(HookEvent $event) {

  // Get the object the event occurred on, if needed
  $pages = $event->object;

    // An 'after' hook can retrieve and/or modify the return value
  $return = $event->return;

  // Get values of arguments sent to hook (if needed)
  $page = $event->arguments(0);

  // tento hook pojde len na jeden template
  if($page->template != 'hry_home') return;
  // pokail stranka este neni vytvorena (Nema ID) tak nepokracuj zo scriptom
  if(empty($page->id)) return;

these are also my if statements so that it doesnt run off when it shouldn't, also this is in my ready.php file

so question is how ca ni force to overwrite my title field ? after save

Link to comment
Share on other sites

<?php
// site/ready.php
$wire->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments(0);
  $page->title = date("Y-m-d H:i:s");
});

Start with that hook, save the page and see the page title being updated. Then add conditions step by step (eg checking for template) and always check if the page title still updates when saving the page ? 

  • Like 2
Link to comment
Share on other sites

13 minutes ago, bernhard said:
<?php
// site/ready.php
$wire->addHookAfter("Pages::saveReady", function($event) {
  $page = $event->arguments(0);
  $page->title = date("Y-m-d H:i:s");
});

Start with that hook, save the page and see the page title being updated. Then add conditions step by step (eg checking for template) and always check if the page title still updates when saving the page ? 

yes the title updates everything else updates but title only updates if its empty before saving, thats the issue

Link to comment
Share on other sites

On 8/4/2021 at 10:05 AM, bernhard said:

No idea why this should be the case. Maybe you have some other hooks firing and making conflicts?

well u can check ill put all hooks i have, and the behavies is still the same, i crea page with temporary title ex. "ss" i upload .xml file then save, name update but title remains "ss"

here is my whole hook
 

<?php
$this->addHookAfter('Pages::saveReady', function(HookEvent $event) {

  // Get the object the event occurred on, if needed
  $pages = $event->object;

    // An 'after' hook can retrieve and/or modify the return value
  $return = $event->return;

  // Get values of arguments sent to hook (if needed)
  $page = $event->arguments(0);

  // tento hook pojde len na jeden template
  if($page->template != 'hry_home') return;
  // pokail stranka este neni vytvorena (Nema ID) tak nepokracuj zo scriptom
  if(empty($page->id)) return;
  if(count($page->subor_hry) < 1) return;

if (count($page->subor_hry) > 1) {

  // TENTO HOOK VHADZUJE TITLE URL A DESSCRIPTION Z XMLka VELMI NEBEZPECNE
  $page->size = strlen($page->body);
  if(strlen($page->body)){
    //not empty
  } else {
    $meta = "meta.xml";
    foreach($page->subor_hry as $file) {
      if(strpos($file, $meta) !== false){
        $xmlmeta = file_get_contents($file->filename());       
      }
    }
    $desc = new SimpleXMLElement($xmlmeta);
 
    $page->body = $desc->description;
      // $page->size = '222';
  }


 
  $meta = "meta.xml";
  foreach($page->subor_hry as $file) {
    if(strpos($file, $meta) !== false){
      $xmlmeta = file_get_contents($file->filename());       
    }
  }
  $desc = new SimpleXMLElement($xmlmeta);

  // Provides: Hll Wrld f PHP
  $onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
 
  $badstring = array(" (Flash Game)", "(Flash Game)", "(flash game)", " (flash game)", "flash game", " flash game");
  $newtitle = str_replace($badstring, "", $desc->title);

  $page->title = $newtitle;
  $page->name = $newtitle;

  // TENTO HOOK VHADZUJE TITLE URL A DESSCRIPTION Z XMLka VELMI NEBEZPECNE

  // Zobrat obrazok zo file fieldu a hodit do imagetype fieldu
  $word = ".png";
  foreach($page->subor_hry as $file) {
      if(strpos($file, $word) !== false){
        $png = new Pageimage($page->images_thumb, $file->filename());
      }
  }
  $word = ".jpg";
  foreach($page->subor_hry as $file) {
      if(strpos($file, $word) !== false){
        $jpg = new Pageimage($page->images_thumb, $file->filename());
      }
  }
  if(empty($png)){
    $page->images_thumb = $jpg;
  } else {
    $page->images_thumb = $png;
  }
}

  // Populate back return value, if you have modified it
  $event->return = $return;
});
?>

i have no idea too why it happens i tried putting the title rewrite out of if statements but remains same, i only have condiditon if body isnt empty dont rewrite it but that' it

Link to comment
Share on other sites

8 hours ago, bernhard said:

Remove all your hooks, start with mine, add your code pieces step by step and after each step save and check the title. Then you'll quickly find the culprit ? 

tried it maybe my processwire instalation broken? or it doesnt work when its in ready.php but this is all i have and it's not working

<?php
$this->addHookAfter('Pages::saveReady', function(HookEvent $event) {

  // Get the object the event occurred on, if needed
  $pages = $event->object;

    // An 'after' hook can retrieve and/or modify the return value
  $return = $event->return;

  // Get values of arguments sent to hook (if needed)
  $page = $event->arguments(0);

$page->title = "test?";
  // Populate back return value, if you have modified it
  $event->return = $return;
});
?>

 

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