Open SalvatorePollaci opened 5 years ago
@stevecohenfr Please see ezsystems/LegacyBridge#168
@SalvatorePollaci In the pre-publish process you don't have access to the location in symfony context. I tried to fetch node in the pre-publish function in the legacy context (works fine) and fetch the location with this node id (fail: location does not exists).
PrePublish should be used to change the attributes of the object on the fly. If you want to create a sub-object, please use AfterPublish function:
<?php
namespace AppBundle\Handler;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
use eZ\Publish\Core\SignalSlot\Repository;
use SteveCohenFr\LegacyPublishHandlerBundle\Classes\LegacyPublishHandlerInterface;
class OnPublishHandler implements LegacyPublishHandlerInterface
{
private $repository;
public function __construct(Repository $repository)
{
$this->repository = $repository;
}
/**
* This function is called from legacy part before an object publication (called by workflow)
* You must link the "before publish" trigger to the custom workflow
*
* @param Content $content The legacy object
* @param int $version The object version
*
*/
function beforePublish(Content $content, $version)
{
}
/**
* This function is called from legacy part after an object publication (called by workflow)
* You must link the "after publish" trigger to the custom workflow
*
* @param Content $content The legacy object
* @param int $version The object version
*
*/
function afterPublish(Content $content, $version)
{
$contentType = $this->repository->getContentTypeService()->loadContentType($content->getVersionInfo()->getContentInfo()->contentTypeId);
if ($version === 1 && $contentType->identifier === "article") {
$this->createContent("folder", [$content->getVersionInfo()->getContentInfo()->mainLocationId], array(
"name" => "My after folder",
), 'eng-GB');
}
}
public function createContent($contentTypeidentifier, array $parentLocationIds, $fieldValues = array(), $lang = null)
{
if ($lang == null) {
$lang = $this->repository->getContentLanguageService()->getDefaultLanguageCode();
}
$contentType = $this->repository->getContentTypeService()->loadContentTypeByIdentifier($contentTypeidentifier);
$contentCreateStruct = $this->repository->getContentService()->newContentCreateStruct($contentType, $lang);
$providedFields = array_keys($fieldValues);
$fields = $contentType->getFieldDefinitions();
foreach ($fields as $field) {
$fieldIdentifier = $field->identifier;
if (in_array($fieldIdentifier, $providedFields)) {
$contentCreateStruct->setField($fieldIdentifier, $fieldValues[$fieldIdentifier]);
}
}
$locationCreateStructs = array();
foreach ($parentLocationIds as $parentLocationId) {
$locationCreateStructs[] = $this->repository->getLocationService()->newLocationCreateStruct($parentLocationId);
}
$draft = $this->repository->getContentService()->createContent($contentCreateStruct, $locationCreateStructs);
$content = $this->repository->getContentService()->publishVersion($draft->versionInfo);
return $content;
}
}
@stevecohenfr Please see https://github.com/ezsystems/LegacyBridge/issues/168