public function beginWorkflow(WorkflowDefinition $definition, DataObject $for = null)
    {
        if (!$this->ID) {
            $this->write();
        }

        if ($for
            && ($for->hasExtension(WorkflowApplicable::class)
                || $for->hasExtension(FileWorkflowApplicable::class))
        ) {
            $this->TargetClass = DataObject::getSchema()->baseDataClass($for);
            $this->TargetID = $for->ID;
        }

        // lets create the first WorkflowActionInstance.
        $action = $definition->getInitialAction()->getInstanceForWorkflow();
        $action->WorkflowID = $this->ID;
        $action->write();

        $title = $for && $for->hasField('Title')
            ? sprintf(_t('WorkflowInstance.TITLE_FOR_DO', '%s - %s'), $definition->Title, $for->Title)
            : sprintf(_t('WorkflowInstance.TITLE_STUB', 'Instance #%s of %s'), $this->ID, $definition->Title);

        $this->Title           = $title;
        $this->DefinitionID    = $definition->ID;
        $this->CurrentActionID = $action->ID;
        $this->InitiatorID     = Security::getCurrentUser()->ID;
        $this->write();

        $this->Users()->addMany($definition->Users());
        $this->Groups()->addMany($definition->Groups());
    }
public function beginWorkflow(WorkflowDefinition $definition, DataObject $for = null)
    {
        if (!$this->ID) {
            $this->write();
        }

        if ($for
            && ($for->hasExtension(WorkflowApplicable::class)
                || $for->hasExtension(FileWorkflowApplicable::class))
        ) {
            $this->TargetClass = DataObject::getSchema()->baseDataClass($for);
            $this->TargetID = $for->ID;
        }

        // lets create the first WorkflowActionInstance.
        $action = $definition->getInitialAction()->getInstanceForWorkflow();
        $action->WorkflowID = $this->ID;
        $action->write();

        $title = $for && $for->hasField('Title')
            ? sprintf(_t('WorkflowInstance.TITLE_FOR_DO', '%s - %s'), $definition->Title, $for->Title)
            : sprintf(_t('WorkflowInstance.TITLE_STUB', 'Instance #%s of %s'), $this->ID, $definition->Title);

        $this->Title           = $title;
        $this->DefinitionID    = $definition->ID;
        $this->CurrentActionID = $action->ID;
        $this->InitiatorID     = Security::getCurrentUser()->ID;
        $this->write();

        $this->Users()->addMany($definition->Users());
        $this->Groups()->addMany($definition->Groups());
    }
public function beginWorkflow ( WorkflowDefinition $ definition , DataObject $ for = null ) { if ( ! $ this -> ID ) { $ this -> write ( ) ; } if ( $ for && ( $ for -> hasExtension ( WorkflowApplicable :: class ) || $ for -> hasExtension ( FileWorkflowApplicable :: class ) ) ) { $ this -> TargetClass = DataObject :: getSchema ( ) -> baseDataClass ( $ for ) ; $ this -> TargetID = $ for -> ID ; } // lets create the first WorkflowActionInstance. $ action = $ definition -> getInitialAction ( ) -> getInstanceForWorkflow ( ) ; $ action -> WorkflowID = $ this -> ID ; $ action -> write ( ) ; $ title = $ for && $ for -> hasField ( 'Title' ) ? sprintf ( _t ( 'WorkflowInstance.TITLE_FOR_DO' , '%s - %s' ) , $ definition -> Title , $ for -> Title ) : sprintf ( _t ( 'WorkflowInstance.TITLE_STUB' , 'Instance #%s of %s' ) , $ this -> ID , $ definition -> Title ) ; $ this -> Title = $ title ; $ this -> DefinitionID = $ definition -> ID ; $ this -> CurrentActionID = $ action -> ID ; $ this -> InitiatorID = Security :: getCurrentUser ( ) -> ID ; $ this -> write ( ) ; $ this -> Users ( ) -> addMany ( $ definition -> Users ( ) ) ; $ this -> Groups ( ) -> addMany ( $ definition -> Groups ( ) ) ; }
Start a workflow based on a particular definition for a particular object.

The object is optional; if not specified, it is assumed that this workflow
is simply a task based checklist type of workflow.

@param WorkflowDefinition $definition
@param DataObject $for
Start a workflow based on a particular definition for a particular object .