This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

While developing an Extreme Clean job I needed large numbers of page versions to test it with. They didn't need to be anything specific, just identical new versions of existing pages.

A quick search led me to a forum post by ijessup about creating new page versions programmatically. The code to create a new page version can be summarised as:

$currentCollectionVersion = Collection::getByID($cID)->getVersionObject();
$newCollectionVersion = $currentCollectionVersion->createNew('My version comment');

Then later in the thread, if you want to approve the version created:

$newCollectionVersion->approve();

This was a great starting point that could easily be extended into a simple job to create new page versions in large batches.

class JlPageVersionFillerJob extends Job {

  public function getJobName() {
    return t('Page version filler job');
  }

  public function getJobDescription() {
    return t('Creates a whole load of new page versions for testing. 
    Configure by adjusting numbers in the code.');
  }

  public function run() {

    // Hack this to the number of versions to add.
    $versions = 25;

    try{

      // Hack this to list cID of the pages you want to add more versions for.
      $pages = array(1, 173,181,184);

      foreach ($pages as $cID){
        $page = Page:: getByID($cID);

      /*
      (You may need to imagine a 'less than or equal to', a left angle bracket preceding '='. 
      where I have inserted 'le'. At the time of the current update, the markdown editor 
      will not accept 'less than' or translate the escape code for it)
      */ 
      for ($ix=1; $ix 'le' $versions; $ix++){
          $cv = $page->getVersionObject();
          $new_cv = $cv->createNew(t('Page filler job created a new version %s of %s', $ix, $versions));
       }

        /*
        Approve the last one for each page. If you uncomment this, the last version will            
        become the approved version. You probably don't want that because it will be 
        empty!
        */
        //$new_cv->approve();        
      }
      $result = t('%s new versions created for cID %s', $versions, implode(', ',$pages));

    } catch (Exception $e) {
      $result = t('cID %s, Exception: %s. ',  $cID, $e->getMessage());
    }
    return $result;
  }

}

If you find yourself needing to create lots of page versions to test something, you can copy the above code into the root jobs directory as jl_page_version_filler_job.php and then install and run it from the dashboard jobs page.

In the code you will need to edit the array of page cIDs to match the cIDs of the pages you want to create versions for.

$pages = array(1, 173,181,184);

You may also want to adjust the actual number of versions created by each run of the job. Don't make this combined list of pages and versions too big or the job could timeout.

Read more How-tos by JohntheFish.

Loading Conversation