Adding a new Page Type in a Package

Permalink
In a package I'm working on (I'm helping with the development; I didn't create this code), I have this code to add a new Page Type:
PageType::add(
    array(
        'handle' => 'new_page_type',
        'name' => 'New Page Type',
        'defaultTemplate' => $template,
        'allowedTemplates' => 'C',
        'templates' => array($template),
        'ptLaunchInComposer' => 0,
        'ptIsFrequentlyAdded' => 0,
        'ptPublishTargetTypeID' => 3
    ),
    $pkg
);
The package installs fine, but when I go to the sitemap and try to add a new subpage, I get this error:
{"error":
    {"type":"Whoops\\Exception\\ErrorException",
    "message":"Call to a member function canPublishPageTypeBeneathTarget() on boolean",
    "file":"\/Users\/skybluesofa\/Sites\/concrete5.7.2\/sub\/updates\/concrete5.7.3.1\/concrete\/src\/Page\/Type\/Type.php",
    "line":993,
    "trace":[{
        "file":"\/Users\/skybluesofa\/Sites\/concrete5.7.2\/sub\/updates\/concrete5.7.3.1\/concrete\/src\/Page\/Type\/Type.php",
        "line":993,
        "function":null,
        "class":null,
        "args":[]
    }]},
    "errors":["Call to a member function canPublishPageTypeBeneathTarget() on boolean"]
}
It seems like the 'ptPublishTargetTypeID' and 'ptPublishTargetObject' never get set in the code. In the database, both these columns are null.

In the PageType::duplicate method, this code gets run:
// duplicate the target object.
$target = $this->getPageTypePublishTargetObject();
$new->setConfiguredPageTypePublishTargetObject($target);
I was wondering if this was an oversight? I'd be happy to do a pull request if it is. Otherwise, can you direct me in fixing the issue?

SkyBlueSofa
 
MichaelG replied on at Permalink Reply
MichaelG
the publish path isn't set. For example, you'll notice when you go to that page type in the dashboard, the option for "publish beneath" followed by three radios, no radios are checked. If you check one of them (i.e. ask when publishing) it won't give you that error.
SkyBlueSofa replied on at Permalink Reply
SkyBlueSofa
If it's a set of radio buttons, then UX would say that one option is necessary. It needs to be set when it gets installed. It seems like there is an oversight in the API. I would tend to think that, by default, the PublishTargetType should be '3' aka 'all'.
pvernaglia replied on at Permalink Reply
pvernaglia
HI, I just ran into this too. Did you find a solution? I'm just starting to look at what to do. thanks
razorcommerce replied on at Permalink Reply
razorcommerce
After you install PageType use or adapt the following. Notice the method is similar to the "get publish target" you mentioned from the duplicate method, the difference is at this there is no existing target object for the new PT so you use configure instead configurePageTypePublishTargetObject().

use Concrete\Core\Page\Type\PublishTarget\Type\Type as PublishTargetType;
// configured publishing target
$publishTarget = PublishTargetType::getByHandle('page_type')->configurePageTypePublishTarget( $pt, array(
  'ptID' => $pt->getPageTypeID()
));
$pt->setConfiguredPageTypePublishTargetObject( $publishTarget );