Uploaded image for project: 'eZ Publish / Platform'
  1. eZ Publish / Platform
  2. EZP-21328

[SignalSlot] Error when publishing a content on first version in draft mode trought slot

    XMLWordPrintable

Details

    Description

      Creating a slot to automatic publish draft content it will raise an error and however still publish the content.
      Error:

        [eZ\Publish\Core\Base\Exceptions\BadStateException]                                       
        Argument '$versionInfo' has a bad state: Only versions in draft status can be published. 
      
      Steps:
      ( Registering a slot )
      1 - create a slot (https://confluence.ez.no/display/EZP/How+to+listen+to+Core+events)
      2 - define it to listen to signal: ContentService\CreateContentSignal
      3 - on the receive function publish the created content
      
      ( Make a command with public API )
      1 - create a content (tested with "folder" type)
      2 - define name
      3 - create content with: $contentService->createContent( ... )
      
      ( Verify )
      1 - execute that command
      2 - verify the error mentioned above
      

      Code used:

      OnCreateContentSlot::receive()

          public function receive( Signal $signal )
          {
              if ( !$signal instanceof Signal\ContentService\CreateContentSignal )
              {
                  return;
              }
      
              // Load content
              $contentVersionInfo = $this->contentService->loadVersionInfo(
                  $this->contentService->loadContentInfo($signal->contentId), 1
              );
      
              // publish content
              $this->contentService->publishVersion( $contentVersionInfo );
          }
      

      CreateContentCommand::execute()

          protected function execute( InputInterface $input , OutputInterface $output )
          {
              $this->login();
      
              $name = $input->getArgument( "name" );
              $type = $input->getArgument( "type" );
              $location = $input->getArgument( "location" );
      
              if( array_search( $type , array( 'folder' , 'article' ) ) )
                  throw new \InvalidArgumentException( "'type' can't be defined as '$type'" );
      
              if( is_integer( $location ) )
                  throw new \InvalidArgumentException( "'location' can't be defined as '$location' must be an integer" );
      
              $repository = $this->getContainer()->get( 'ezpublish.api.repository' );
      
              $contentService = $repository->getContentService();
      
              $newDraft =  $contentService->newContentCreateStruct(
                  $repository->getContentTypeService()->loadContentTypeByIdentifier( $type ),
                  'eng-GB'
              );
      
              $newDraft->setField( "name" , $name );
      
              $draft = $contentService->createContent(
                  $newDraft,
                  array( $repository->getLocationService()->newLocationCreateStruct( $location ) )
              );
      
              $output->writeln("Draft was created.");
      
      //        $contentService->publishVersion( $draft->versionInfo );
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            marcos.loureiro-obsolete@ez.no Marcos Loureiro (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: