Skip to content

pub_date and unpub_date handling #7

@ttyiav

Description

@ttyiav

Hello,
Tried to implement the auto-unpublish with CacheMaster enabled, but unfortunately it does not work as expected :(
Here is my bug report and my local corrections.

Related commits for reference:
9c45274
0ea93d8

  1. When $modx->cacheManager->refresh(...) is called on OnBeforeDocFormSave event, MODX takes the values of pub_date and unpub_date from the database, not from event's $resource object.
    Since we are in OnBeforeDocFormSave handler, the new values (pub or unpub) are not yet saved to the database.
    Therefore, $modx->cacheManager->refresh will update the auto_publish.cache.php not for current resource, but for other resources with non-zero pub_date and unpub_date values.
    If the submitted (but not saved yet) resource is the only with the unpub_date, the auto_publish.cache.php will not be updated.

  2. With the current approach, the auto_publish.cache.php not updating when the user unset either pub_date or unpub_date, which was previously set. Inside OnBeforeDocFormSave unset values are empty and the condition is not satisfied.

My solution:

  • bind CacheMaster to OnDocFormSave event.
  • relocate $modx->cacheManager->refresh(...) without surrounding condition from OnBeforeFormSave to the first switch($event):
/* Bail out if we're not doing this object */
switch($event) {
    case 'OnDocFormSave':
        if (!$doResources) {
            return;
        }
        
        $ctx = $resource->get('context_key');
        
        // If pub_date or unpub_date are set, update the auto_publish cache 
        // if ($resource->get('pub_date') || $resource->get('unpub_date')) {
        $providers = array(
            'auto_publish' => array('contexts' => $ctx),
        );
        $providers_result = array();
        $modx->cacheManager->refresh($providers, $providers_result); // Cache refreshes from database
            
        if ($doDebug) {
            my_debug($providers_result);
        }
            
        //}
        return;        
        
        break;
    case 'OnDocFormPrerender':
    case 'OnBeforeDocFormSave':
        if (!$doResources) {
            return;
        }
        break;


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions