Source of file HasOneAddExistingAutoCompleter.php
Size: 2,434 Bytes - Last Modified: 2021-12-24T06:50:39+00:00
/var/www/docs.ssmods.com/process/src/src/HasOneAddExistingAutoCompleter.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 | <?php namespace SilverShop\HasOneField; use SilverStripe\Control\Controller; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField_HTMLProvider; use SilverStripe\Forms\GridField\GridFieldAddNewButton; use SilverStripe\View\ArrayData; use SilverStripe\View\SSViewer; use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; use SilverStripe\ORM\DataObject; /** * Class GridFieldHasOneEditButton */ class HasOneAddExistingAutoCompleter extends GridFieldAddExistingAutocompleter { /** * Check if a record has been set, if so, don't load the fields * * @param GridField $gridField * * @return array */ public function getHTMLFragments($gridField) { if (!$gridField->getRecord()->exists()) { return parent::getHTMLFragments($gridField); } return [ $this->targetFragment => "" ]; } /** * Overwrite default add to and inlude redirect * * @param GridField $gridField * @param string $actionName Action identifier, see {@link getActions()}. * @param array $arguments Arguments relevant for this * @param array $data All form data */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if ($actionName == 'addto' && isset($data['relationID']) && $data['relationID']) { $parent = $gridField->getParent(); $relation = $gridField->getRelation() . "ID"; $item = DataObject::get($gridField->getModelClass()) ->byID($data['relationID']); if (empty($parent)) { throw new ValidationException( _t(__CLASS__ . '.ParentNotFound', "Parent record not found") ); } if (empty($item)) { throw new ValidationException( _t(__CLASS__ . '.ItemNotFound', "Related record not found") ); } // Save this relation to the DB $parent->{$relation} = $data['relationID']; $parent->write(); $gridField->State->GridFieldAddRelation = $data['relationID']; $gridField->getList()->add($item); Controller::curr()->getResponse()->setStatusCode( 200, _t(__CLASS__ . '.Linked', "Linked") ); } } } |