Jump to content

samberry

Members
  • Posts

    1
  • Joined

  • Last visited

Posts posted by samberry

  1. On 3/28/2017 at 2:48 AM, diogo said:

    And here goes some code. Place this on site/ready.php (or create a module from it) and edit the first lines to your liking:

     

    
    /*************************/
    /******* EDIT HERE *******/
    /*************************/
    
    // ID of the page where the custom content is
    $this->pageID = 1;
    
    // name of the field where the custom content is
    $this->fieldName = 'body';
    
    // name of html tag and id from the parent of the custom content on the 505 file
    $this->elementTag = 'div';
    $this->elementID = 'message';
    
    /*************************/
    /******* END EDIT ********/
    /*************************/
    
    
    // regex to get the contents of parent element
    $this->regex = '/(<'.$this->elementTag.'\sid=("|\')?'.$this->elementID.'("|\')?>)([^<]|<.+>.*<\/.+>)+(<\/'.$this->elementTag.'>)/i';
    
    // On save the content of the field on the 505.html file
    $this->addHookAfter('Pages::saved', function(HookEvent $event) {
      
        $page = $event->arguments[0];
    
        if($page->id !== $this->pageID) return;
    
        $file = wire('config')->paths->templates . '/errors/500.html';
    
        $content = file_get_contents($file);
        $field = $this->fieldName;
    
        $content = preg_replace($this->regex, '$1'.$page->$field.'$3', $content);
    
        file_put_contents($file, $content);
    
    });
    
    // On render the edit page, save the content of 505.html file on the field to show it
    $this->addHookBefore("ProcessPageEdit::buildFormContent", function(HookEvent $event) {
      
        $page = $event->object->getPage();
    
        if($page->id !== $this->pageID) return;
    
        $file = wire('config')->paths->templates . '/errors/500.html';
    
        $content = file_get_contents($file);
    
        preg_match($this->regex, $content, $matches);
    
        if($matches) {
    
            $page->of(false);
            $field = $this->fieldName;
            $page->$field = $matches[0];
            $page->save($this->fieldName);
    
        }
    
    });

     

    The above code assumes that your "505.html" file has a <div> with the id "message" in it, you just have to adapt it to yours.

    Thanks @diogo  for this.

    ___________________________________
    Get Assignment help.

    • Like 1
×
×
  • Create New...