eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\UpdateSortKeysCommand::updateField PHP Method

updateField() protected method

For given $contentId in $versionNo updates fields of $fieldTypeIdentifier type.
protected updateField ( Symfony\Component\Console\Output\OutputInterface $output, ProgressBar $progress, integer | string $contentId, integer $versionNo, string $fieldTypeIdentifier, boolean $dryRun )
$output Symfony\Component\Console\Output\OutputInterface
$progress Symfony\Component\Console\Helper\ProgressBar
$contentId integer | string
$versionNo integer
$fieldTypeIdentifier string
$dryRun boolean
    protected function updateField(OutputInterface $output, ProgressBar $progress, $contentId, $versionNo, $fieldTypeIdentifier, $dryRun)
    {
        $container = $this->getContainer();
        /** @var \eZ\Publish\SPI\FieldType\FieldType $fieldType */
        /* @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter $converter */
        /* @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $gateway */
        /* @var \eZ\Publish\SPI\Persistence\Content\Handler $contentHandler */
        /* @var \eZ\Publish\API\Repository\ContentService $contentService */
        /* @var \eZ\Publish\API\Repository\ContentTypeService $contentTypeService */
        $fieldType = $container->get("ezpublish.fieldType.{$fieldTypeIdentifier}");
        $converter = $container->get("ezpublish.fieldType.{$fieldTypeIdentifier}.converter");
        $gateway = $container->get('ezpublish.persistence.legacy.content.gateway');
        $contentHandler = $container->get('ezpublish.spi.persistence.legacy.content.handler');
        $contentService = $container->get('ezpublish.api.service.content');
        $contentTypeService = $container->get('ezpublish.api.service.content_type');
        $content = $contentService->loadContent($contentId, null, $versionNo);
        $spiContent = $contentHandler->load($contentId, $versionNo);
        $contentType = $contentTypeService->loadContentType($content->contentInfo->contentTypeId);
        foreach ($content->getFields() as $field) {
            $fieldDefinition = $contentType->getFieldDefinition($field->fieldDefIdentifier);
            // Match API field by field type
            if ($fieldDefinition->fieldTypeIdentifier != $fieldTypeIdentifier) {
                continue;
            }
            foreach ($spiContent->fields as $spiField) {
                // Match SPI field with API field by ID
                if ($spiField->id != $field->id) {
                    continue;
                }
                // Cast API field value to persistence value
                $persistenceValue = $fieldType->toPersistenceValue($field->value);
                if ($persistenceValue->sortKey == $spiField->value->sortKey) {
                    // Assume stored value is correct
                    break;
                }
                if (!$dryRun) {
                    // Create and fill Legacy Storage field value
                    $storageValue = new StorageFieldValue();
                    $converter->toStorageValue($persistenceValue, $storageValue);
                    $gateway->updateField($spiField, $storageValue);
                }
                $progress->clear();
                $output->write("\r");
                $output->writeln('Updated Content ' . $content->id . ', version ' . $content->versionInfo->versionNo . ', field ' . $spiField->id . ": '" . $spiField->value->sortKey . "' => '" . $persistenceValue->sortKey . "'");
                $output->write("\r");
                $progress->display();
                // Continue outer loop
                break;
            }
        }
    }