Source of file GridFieldHasOneRelationHandler.php
Size: 1,791 Bytes - Last Modified: 2022-02-21T10:01:55+00:00
/var/www/docs.ssmods.com/process/src/gridfield/GridFieldHasOneRelationHandler.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 | <?php class GridFieldHasOneRelationHandler extends GridFieldRelationHandler { protected $onObject; protected $relationName; protected $targetObject; public function __construct(DataObject $onObject, $relationName, $targetFragment = 'before') { $this->onObject = $onObject; $this->relationName = $relationName; $hasOne = $onObject->has_one($relationName); if(!$hasOne) { user_error('Unable to find a has_one relation named ' . $relationName . ' on ' . $onObject->ClassName, E_USER_WARNING); } $this->targetObject = $hasOne; parent::__construct(false, $targetFragment); } protected function setupState($state, $extra = null) { parent::setupState($state, $extra); if($state->FirstTime) { $state->RelationVal = $this->onObject->{$this->relationName}()->ID; } } public function getColumnContent($gridField, $record, $columnName) { $class = $gridField->getModelClass(); if(!($class == $this->targetObject || is_subclass_of($class, $this->targetObject))) { user_error($class . ' is not a subclass of ' . $this->targetObject . '. Perhaps you wanted to use ' . $this->targetObject . '::get() as the list for this GridField?', E_USER_WARNING); } $state = $this->getState($gridField); $checked = $state->RelationVal == $record->ID; $field = new ArrayData(array('Checked' => $checked, 'Value' => $record->ID, 'Name' => $this->relationName . 'ID')); return $field->renderWith('GridFieldHasOneRelationHandlerItem'); } protected function saveGridRelation(GridField $gridField, $arguments, $data) { $field = $this->relationName . 'ID'; $state = $this->getState($gridField); $id = intval($state->RelationVal); $this->onObject->{$field} = $id; $this->onObject->write(); parent::saveGridRelation($gridField, $arguments, $data); } } |