Need clarification on layout published/draft status

Hi,

I’m trying to remove all the regions within a layout via the PHP API.

Here is the code for it:


$xRegion = (new \Xibo\OAuth2\Client\Entity\XiboRegion($this->EP));
$xLayout = (new \Xibo\OAuth2\Client\Entity\XiboLayout($this->EP))->getById($layout['layoutXiboId'], 'regions,playlists,widgets');
        
if($xLayout->publishedStatusId == XiboPublishedStatus::PUBLISHED)
  $xLayoutCheckout = $xLayout->checkout($xLayout->layoutId); 
else if($xLayout->publishedStatusId == XiboPublishedStatus::DRAFT){
  $xLayoutCheckout = $xLayout;
}
        
//Delete all the regions it contains
foreach($xLayoutCheckout->regions as $key => $region){
    $xRegion->delete($region->regionId);
}

Before the first if/else block, I printed the content of the “$xLayout” object and it showed me that the XiboPublishedId is 2 which is a draft.

However when it reaches the delete region function, it throws an error telling me the layout is not a draft.
I tried to checkout the layout before that function and it throws an error telling me the layout is already checked out…

Also, I don’t know if this could explain the bug, but when I print the checked out layout object, the parentId is equal to 0 (the layoutId is 5598)

Hello there.

It works as expected, let me clarify some details.

When Layout is in draft state, there are two Layouts in the database related to eachother.
The original Layout and the editable draft Layout, both will have publishedStatusId = 2, the actual draft however will have parentId set to the layoutId of the original Layout.

When Layout is published, then it’s fairly straightforward, you need to call checkout on it and get the returned draft Layout which you can edit.

When Layout is in draft state, you need to get the actual draft layout using the original layoutId in the query as a parentId parameter. I don’t think we have wrapper for that, as such you will want to use the get on XiboLayout Entity with something like:

['parentId' => $layout->layoutId, 'showDrafts' => 1, 'embed' => 'regions,playlists,widgets']
as an array of parameters - since it’s just get call it will return an array (with 1 element), so you will want to make sure to grab the first element of the returned array from this call.

Without doing this lookup, you’re basically attempting to edit the original Layout, which won’t be allowed, hence the error you’ve encountered.

Once you’re done with your edits and you want to publish the Layout, you can call publish with the original layoutId - depending on which CMS version you’re using, you may need to use the XiboEntityProvider put call to /layout/publish/{layoutId} with publishNow = 1 flag as parameter, as I don’t think the publish in XiboLayout entity was updated to include this flag.

You can also have a look at Layout Helper we use in the test suite, xibo-cms/LayoutHelperTrait.php at release30 · xibosignage/xibo-cms · GitHub , it uses some calls from wrapper library, some direct calls from XiboEntityProvider, might be helpful if you get stuck.

I hope that helps.

Hi, thank you for the clarifications

I added some tests to check if the parent id is equal to 0 when a layout is in draft and if so I can call the getById with the parentId parameter. Is seems to work fine now. The weird thing is it was working fine with everything else until this point.