Source of file ElementalGridFieldAddExistingAutocompleter.php
Size: 2,243 Bytes - Last Modified: 2021-12-24T06:44:58+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/ElementalGridFieldAddExistingAutocompleter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 | <?php namespace DNADesign\ElementalVirtual\Forms; use DNADesign\ElementalVirtual\Model\ElementVirtual; use SilverStripe\Core\Convert; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\SS_List; /** * This class is is responsible for adding objects to another object's has_many * and many_many relation, as defined by the {@link RelationList} passed to the * {@link GridField} constructor. * * Objects can be searched through an input field (partially matching one or * more fields). * * Selecting from the results will add the object to the relation. * * Often used alongside {@link GridFieldDeleteAction} for detaching existing * records from a relationship. * * For easier setup, have a look at a sample configuration in * {@link GridFieldConfig_RelationEditor}. * * @package forms * @subpackage fields-gridfield */ class ElementalGridFieldAddExistingAutocompleter extends GridFieldAddExistingAutocompleter { /** * If an object ID is set, add the object to the list * * @param GridField $gridField * @param SS_List $dataList * @return SS_List */ public function getManipulatedData(GridField $gridField, SS_List $dataList) { if (!$gridField->State->GridFieldAddRelation) { return $dataList; } $objectID = Convert::raw2sql($gridField->State->GridFieldAddRelation); if ($objectID) { $object = DataObject::get_by_id($gridField->getModelClass(), $objectID); if ($object) { // if the object is currently not linked to either a page or another list then we want to link to // the original, otherwise link to a clone if (!$object->ParentID) { $dataList->add($object); } else { $virtual = ElementVirtual::create(); $virtual->LinkedElementID = $object->ID; $virtual->write(); $dataList->add($virtual); } } } $gridField->State->GridFieldAddRelation = null; return $dataList; } } |