src/AddToCampaignHandler.php
public function Form($object)
    {
        $inChangeSets = $this->getInChangeSets($object);
        $inChangeSetIDs = $inChangeSets->column('ID');

        // Get changesets that can be added to
        $candidateChangeSets = $this->getAvailableChangeSets();
        if ($inChangeSetIDs) {
            $candidateChangeSets = $candidateChangeSets->exclude('ID', $inChangeSetIDs);
        }

        $canCreate = ChangeSet::singleton()->canCreate();
        $message = $this->getFormAlert($inChangeSets, $candidateChangeSets, $canCreate);
        $fields = new FieldList(array_filter([
            $message ? LiteralField::create("AlertMessages", $message) : null,
            HiddenField::create('ID', null, $object->ID),
            HiddenField::create('ClassName', null, $object->baseClass())
        ]));

        // Add fields based on available options
        $showSelect = $candidateChangeSets->count() > 0;
        if ($showSelect) {
            $campaignDropdown = DropdownField::create(
                'Campaign',
                _t(__CLASS__ . '.AddToCampaignAvailableLabel', 'Available campaigns'),
                $candidateChangeSets
            )
                ->setEmptyString(_t(__CLASS__ . '.AddToCampaignFormFieldLabel', 'Select a Campaign'))
                ->addExtraClass('noborder')
                ->addExtraClass('no-chosen');
            $fields->push($campaignDropdown);

            // Show visibilty toggle of other create field
            if ($canCreate) {
                $addCampaignSelect = CheckboxField::create('AddNewSelect', _t(
                    __CLASS__ . '.ADD_TO_A_NEW_CAMPAIGN',
                    'Add to a new campaign'
                ))
                    ->setAttribute('data-shows', 'NewTitle')
                    ->setSchemaData(['data' => ['shows' => 'NewTitle']]);
                $fields->push($addCampaignSelect);
            }
        }
        if ($canCreate) {
            $placeholder = _t(__CLASS__ . '.CREATE_NEW_PLACEHOLDER', 'Enter campaign name');
            $createBox = TextField::create(
                'NewTitle',
                _t(__CLASS__ . '.CREATE_NEW', 'Create a new campaign')
            )
                ->setAttribute('placeholder', $placeholder)
                ->setSchemaData(['attributes' => ['placeholder' => $placeholder]]);
            $fields->push($createBox);
        }

        $actions = FieldList::create();
        if ($canCreate || $showSelect) {
            $actions->push(
                AddToCampaignHandler_FormAction::create()
                    ->setTitle(_t(__CLASS__ . '.AddToCampaignAddAction', 'Add'))
                    ->addExtraClass('add-to-campaign__action')
            );
        }

        $form = Form::create(
            $this->controller,
            $this->name,
            $fields,
            $actions,
            AddToCampaignValidator::create()
        );

        $form->setHTMLID('Form_EditForm_AddToCampaign');
        $form->addExtraClass('form--no-dividers add-to-campaign__form');

        return $form;
    }
public function Form($object)
    {
        $inChangeSets = $this->getInChangeSets($object);
        $inChangeSetIDs = $inChangeSets->column('ID');

        // Get changesets that can be added to
        $candidateChangeSets = $this->getAvailableChangeSets();
        if ($inChangeSetIDs) {
            $candidateChangeSets = $candidateChangeSets->exclude('ID', $inChangeSetIDs);
        }

        $canCreate = ChangeSet::singleton()->canCreate();
        $message = $this->getFormAlert($inChangeSets, $candidateChangeSets, $canCreate);
        $fields = new FieldList(array_filter([
            $message ? LiteralField::create("AlertMessages", $message) : null,
            HiddenField::create('ID', null, $object->ID),
            HiddenField::create('ClassName', null, $object->baseClass())
        ]));

        // Add fields based on available options
        $showSelect = $candidateChangeSets->count() > 0;
        if ($showSelect) {
            $campaignDropdown = DropdownField::create(
                'Campaign',
                _t(__CLASS__ . '.AddToCampaignAvailableLabel', 'Available campaigns'),
                $candidateChangeSets
            )
                ->setEmptyString(_t(__CLASS__ . '.AddToCampaignFormFieldLabel', 'Select a Campaign'))
                ->addExtraClass('noborder')
                ->addExtraClass('no-chosen');
            $fields->push($campaignDropdown);

            // Show visibilty toggle of other create field
            if ($canCreate) {
                $addCampaignSelect = CheckboxField::create('AddNewSelect', _t(
                    __CLASS__ . '.ADD_TO_A_NEW_CAMPAIGN',
                    'Add to a new campaign'
                ))
                    ->setAttribute('data-shows', 'NewTitle')
                    ->setSchemaData(['data' => ['shows' => 'NewTitle']]);
                $fields->push($addCampaignSelect);
            }
        }
        if ($canCreate) {
            $placeholder = _t(__CLASS__ . '.CREATE_NEW_PLACEHOLDER', 'Enter campaign name');
            $createBox = TextField::create(
                'NewTitle',
                _t(__CLASS__ . '.CREATE_NEW', 'Create a new campaign')
            )
                ->setAttribute('placeholder', $placeholder)
                ->setSchemaData(['attributes' => ['placeholder' => $placeholder]]);
            $fields->push($createBox);
        }

        $actions = FieldList::create();
        if ($canCreate || $showSelect) {
            $actions->push(
                AddToCampaignHandler_FormAction::create()
                    ->setTitle(_t(__CLASS__ . '.AddToCampaignAddAction', 'Add'))
                    ->addExtraClass('add-to-campaign__action')
            );
        }

        $form = Form::create(
            $this->controller,
            $this->name,
            $fields,
            $actions,
            AddToCampaignValidator::create()
        );

        $form->setHTMLID('Form_EditForm_AddToCampaign');
        $form->addExtraClass('form--no-dividers add-to-campaign__form');

        return $form;
    }
public function Form ( $ object ) { $ inChangeSets = $ this -> getInChangeSets ( $ object ) ; $ inChangeSetIDs = $ inChangeSets -> column ( 'ID' ) ; // Get changesets that can be added to $ candidateChangeSets = $ this -> getAvailableChangeSets ( ) ; if ( $ inChangeSetIDs ) { $ candidateChangeSets = $ candidateChangeSets -> exclude ( 'ID' , $ inChangeSetIDs ) ; } $ canCreate = ChangeSet :: singleton ( ) -> canCreate ( ) ; $ message = $ this -> getFormAlert ( $ inChangeSets , $ candidateChangeSets , $ canCreate ) ; $ fields = new FieldList ( array_filter ( [ $ message ? LiteralField :: create ( "AlertMessages" , $ message ) : null , HiddenField :: create ( 'ID' , null , $ object -> ID ) , HiddenField :: create ( 'ClassName' , null , $ object -> baseClass ( ) ) ] ) ) ; // Add fields based on available options $ showSelect = $ candidateChangeSets -> count ( ) > 0 ; if ( $ showSelect ) { $ campaignDropdown = DropdownField :: create ( 'Campaign' , _t ( __CLASS__ . '.AddToCampaignAvailableLabel' , 'Available campaigns' ) , $ candidateChangeSets ) -> setEmptyString ( _t ( __CLASS__ . '.AddToCampaignFormFieldLabel' , 'Select a Campaign' ) ) -> addExtraClass ( 'noborder' ) -> addExtraClass ( 'no-chosen' ) ; $ fields -> push ( $ campaignDropdown ) ; // Show visibilty toggle of other create field if ( $ canCreate ) { $ addCampaignSelect = CheckboxField :: create ( 'AddNewSelect' , _t ( __CLASS__ . '.ADD_TO_A_NEW_CAMPAIGN' , 'Add to a new campaign' ) ) -> setAttribute ( 'data-shows' , 'NewTitle' ) -> setSchemaData ( [ 'data' => [ 'shows' => 'NewTitle' ] ] ) ; $ fields -> push ( $ addCampaignSelect ) ; } } if ( $ canCreate ) { $ placeholder = _t ( __CLASS__ . '.CREATE_NEW_PLACEHOLDER' , 'Enter campaign name' ) ; $ createBox = TextField :: create ( 'NewTitle' , _t ( __CLASS__ . '.CREATE_NEW' , 'Create a new campaign' ) ) -> setAttribute ( 'placeholder' , $ placeholder ) -> setSchemaData ( [ 'attributes' => [ 'placeholder' => $ placeholder ] ] ) ; $ fields -> push ( $ createBox ) ; } $ actions = FieldList :: create ( ) ; if ( $ canCreate || $ showSelect ) { $ actions -> push ( AddToCampaignHandler_FormAction :: create ( ) -> setTitle ( _t ( __CLASS__ . '.AddToCampaignAddAction' , 'Add' ) ) -> addExtraClass ( 'add-to-campaign__action' ) ) ; } $ form = Form :: create ( $ this -> controller , $ this -> name , $ fields , $ actions , AddToCampaignValidator :: create ( ) ) ; $ form -> setHTMLID ( 'Form_EditForm_AddToCampaign' ) ; $ form -> addExtraClass ( 'form--no-dividers add-to-campaign__form' ) ; return $ form ; }
Builds a Form that mirrors the parent editForm, but with an extra field to collect the ChangeSet ID

@param DataObject $object The object we're going to be adding to whichever ChangeSet is chosen
@return Form
Builds a Form that mirrors the parent editForm but with an extra field to collect the ChangeSet ID