Jump to content

How to hook into PageTable field on save of modal


alexcapes
 Share

Recommended Posts

Hi,

I've created a simple module that does the following:

  • Checks if a checkbox is ticked ('media_wall_quote_hero') on a page using the template 'media_wall_quote'.
  • If that checkbox is ticked, it adds that page to a PageTable field ('quotes_hero') located on the 'home' template.
  • If it's not checked and the page is in the PageTable field it is removed.

It all works correctly when checking/unchecking the box on pages using the 'media_wall_quote' template in admin.

However, if I edit the pages in the PageTable field itself on the homepage in admin, when I save the modal, the PageTable field is not updated. Strangely if I uncheck it (ie. when it should be removed) the PageTable field says 'item added'.

Is there a way I can hook into the PageTable field when the modal is saved and update it so if I check/uncheck the box it does the correct action? 

public function init() {
    $this->pages->addHookAfter('save', $this, 'AddRemoveHeroQuotes');
  }

  public function AddRemoveHeroQuotes($event) {
    $page = $event->arguments(0);

    if($page->template == 'media_wall_quote') {

      $home = wire('pages')->get(1);
      $home->of(false);

      $work_page = wire('pages')->get("template=work, media_wall_work=$page");

      // If this quote is a (published) hero quote add it to the pagetable field
      if($page->media_wall_quote_hero == 1 && !$page->is(Page::statusUnpublished)) {

        if(!$home->quotes_hero->has($page)) {
          $home->quotes_hero->add($page);
        }
      }
      // If this quote is a not a hero quote or is unpublished remove if from the pagetable field
      elseif($page->template == 'media_wall_quote' && $page->media_wall_quote_hero == 0 || $page->template == 'media_wall_quote' && $page->is(Page::statusUnpublished)) {

        if($home->quotes_hero->has($page)) {
          $home->quotes_hero->remove($page);
          $this->message('removed');
        }
      }
      $home->save('quotes_hero');
    }
  }

 

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

×
×
  • Create New...