/**
* Try to load a workflow stage for newly created items
* which does not have a workflow assigned yet. If the category is not the
* carrier, overwrite it on your model and deliver your own carrier.
*
* @param Form $form A Form object.
* @param mixed $data The data expected for the form.
*
* @return boolean|integer An integer, holding the stage ID or false
* @since 4.0.0
*/
protected function getStageForNewItem(Form $form, $data)
{
$table = $this->getTable();
$hasKey = $table->hasField('catid');
if (!$hasKey) {
return false;
}
$catKey = $table->getColumnAlias('catid');
$field = $form->getField($catKey);
if (!$field) {
return false;
}
$catId = isset(((object) $data)->{$catKey}) ? ((object) $data)->{$catKey} : $form->getValue($catKey);
// Try to get the category from the html code of the field
if (empty($catId)) {
$catId = $field->getAttribute('default', null);
if (!$catId) {
// Choose the first category available
$catOptions = $field->options;
if ($catOptions && !empty($catOptions[0]->value)) {
$catId = (int) $catOptions[0]->value;
}
}
}
if (empty($catId)) {
return false;
}
return $this->workflow->getDefaultStageByCategory($catId);
}