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

As a package developer, if you add a block type or single page to a package, you can use upgrade() to install them without any need to uninstall the package first.

Package Version 1.0

First, to set the background, the package controller for version 1.0 of a package, installing a block type and a single page.

protected $pkgVersion = '1.0';

public function install() {
  $pkg = parent::install();
  // install block
  BlockType::installBlockTypeFromPackage('my_block', $pkg); 
  // install single page
  Loader::model('single_page');
  SinglePage::add('/path/for/page', $pkg);
}

Package Version 1.0.1

In version 1.0.1, the example package provides another block type and another single page. These are installed normally in the install() method to cater for any new installation of the package.

To cater for updates from a previous version of the package, the upgrade() method also installs the block type and single page that have been added since version 1.0.

protected $pkgVersion = '1.0.1';

// Install everything for a new installation
public function install() {
  $pkg = parent::install();
  // install block type
  BlockType::installBlockTypeFromPackage('my_block', $pkg); 
  // install single page
  Loader::model('single_page');
  SinglePage::add('/path/for/page', $pkg);

  // install another block type
  BlockType::installBlockTypeFromPackage('my_other_block', $pkg); 
  // install another single page
  SinglePage::add('/path/for/another_page', $pkg);
}

// Update any existing installation
public function upgrade() {
  parent::upgrade();

  // add another block type
  $bt = BlockType::getByHandle('my_other_block');
  if (!is_object($bt)) {
    BlockType::installBlockTypeFromPackage('my_other_block', $this); 
  }

  // add another single page
  $p = Page::getByPath('/path/for/another_page');
  if ($p->isError() || (!is_object($p))) {
    SinglePage::add('/path/for/another_page', $this);
  }
}

The tests about the block type and single page in the upgrade() method are only really needed if there are further upgrades - they allow for the consequence of users of the package skipping a version before upgrading and prevent failure through erroneous repetition.

Further info:

Read more How-tos by JohntheFish

Loading Conversation