Modifying page attribute on page update/add
Permalink
I've hooked into the on_page_update/on_page_add event to check the value of one the page attributes. If a certain condition is met, I want to clear said attribute.
I've tested that I've successfully hooked into the event, but I can't seem to clear the attribute (event though the exact same command works elsewhere).
site_events.php
customhooks.php
Thanks!
EDIT:
I tried putting a die() command right after attempting to clear the page attribute and it worked, but there has to be a better way than stopping the script...
I've tested that I've successfully hooked into the event, but I can't seem to clear the attribute (event though the exact same command works elsewhere).
site_events.php
customhooks.php
<?php defined('C5_EXECUTE') or die("Access Denied."); class CustomHooks{ public function checkStaleDate($objPage){ $tsPage = strtotime($objPage->getAttribute('stale_date')); // Remove the stale date if set to the current day if(date('Y-m-d', $tsPage) == date('Y-m-d', time())){ $objPage->clearAttribute(CollectionAttributeKey::getByHandle('stale_date')); } } } ?>
Thanks!
EDIT:
I tried putting a die() command right after attempting to clear the page attribute and it worked, but there has to be a better way than stopping the script...
maybe you need to load the collection attribute model or some such? see what the file that works is doing that this isn't.
Thanks for the reply, but I don't think it's a matter of a particular model not being loaded. When I put a die() right after the clearAttribute like so:
...the command is fired and the attribute is cleared. I haven't looked at the guts of how C5 does custom event methods, but I'm guessing it doesn't expect the page to be modified and overrides anything you try to do.
EDIT:
This may be all a moot point as any page attributes you assign when making a new page are not set yet when the on_page_add event passes its Page object. It looks like I'll have to find another way to handle this.
$objPage->clearAttribute(CollectionAttributeKey::getByHandle('stale_date')); die();
...the command is fired and the attribute is cleared. I haven't looked at the guts of how C5 does custom event methods, but I'm guessing it doesn't expect the page to be modified and overrides anything you try to do.
EDIT:
This may be all a moot point as any page attributes you assign when making a new page are not set yet when the on_page_add event passes its Page object. It looks like I'll have to find another way to handle this.
Did you manage to solve this? I'm looking to do something similar.
No, I did not find an answer to this specific problem. :(
The client had wanted to mark certain pages as "fresh" or "stale" so I set up a date picker page attribute that would allow them to set a date when a page's content expires.
I had wanted to tap into the add/update event in case they accidently set a date in the past, but for the reasons already explained I could not achieve it this way.
In retrospect, I would have simply created a new page attribute that did that verification right in its controller and ignored page add/update events altogether.
The client later changed how they wanted this to work so I did not explore this route any further.
The client had wanted to mark certain pages as "fresh" or "stale" so I set up a date picker page attribute that would allow them to set a date when a page's content expires.
I had wanted to tap into the add/update event in case they accidently set a date in the past, but for the reasons already explained I could not achieve it this way.
In retrospect, I would have simply created a new page attribute that did that verification right in its controller and ignored page add/update events altogether.
The client later changed how they wanted this to work so I did not explore this route any further.
I'm also trying to accomplish something with Concrete's "events" and am also having trouble with the "on_page_update" event. You said you think concrete is overriding your actions on the update event. I think however that the update-event is never fired. A die() should always be executed, there's no way around die(). That's why it's called "die". I experimented with the same command on various "events" and in all cases except the "update-event" the code is executed which leaves me to believe there is an error in Concrete causing the "on_update_event" not to be fired.