Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active May 1, 2020 09:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hissy/aae644d870f2546b170e7236f5f620e7 to your computer and use it in GitHub Desktop.
Save hissy/aae644d870f2546b170e7236f5f620e7 to your computer and use it in GitHub Desktop.
[concrete5][V8][MigrationTool] Example of creating custom publisher
<?php
Events::addListener('on_before_dispatch', function($event) {
/** @var \PortlandLabs\Concrete5\MigrationTool\Publisher\Block\Manager $blockPublisherManager */
$blockPublisherManager = Core::make('migration/manager/publisher/block');
if (is_object($blockPublisherManager)) {
$originalBlockTypeHandle = 'manual_nav';
$blockPublisherManager->extend($originalBlockTypeHandle, function () {
return new \Application\Concrete\MigrationTool\Publisher\Block\ManualNavPublisher();
});
}
});
<?php
// application/src/Concrete/MigrationTool/Publisher/Block/ManualNavPublisher.php
namespace Application\Concrete\MigrationTool\Publisher\Block;
use Concrete\Core\Block\Block;
use Concrete\Core\Page\Page;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\Batch;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\BlockValue\BlockValue;
use PortlandLabs\Concrete5\MigrationTool\Publisher\Block\PublisherInterface;
class ManualNavPublisher implements PublisherInterface
{
public function publish(Batch $batch, $bt, Page $page, $area, BlockValue $value)
{
$records = $value->getRecords();
$inspector = \Core::make('migration/import/value_inspector', array($batch));
foreach ($records as $record) {
if (strcasecmp($record->getTable(), $bt->getController()->getBlockTypeDatabaseTable()) == 0) {
/** @var Block $b */
$b = $page->addBlock($bt, $area, []);
}
}
// Now we import the OTHER records.
if ($b) {
$data = [];
$i = 0;
foreach ($records as $record) {
if (strcasecmp($record->getTable(), $bt->getController()->getBlockTypeDatabaseTable()) != 0) {
$data['linkType'][$i] = 1;
$data['linkURL'][$i] = '';
foreach ($record->getData() as $key => $value) {
$result = $inspector->inspect($value);
if ($key == 'linkToCID') {
$data['internalLinkCID'][$i] = $result->getReplacedValue();
}
if ($key == 'linkText') {
$data['title'][$i] = $result->getReplacedValue();
}
if ($key == 'position') {
$data['sortOrder'][$i] = $result->getReplacedValue();
}
}
$i++;
}
}
$b->update($data);
return $b;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment