Source of file HasOneButtonField.php
Size: 3,787 Bytes - Last Modified: 2021-12-24T06:34:48+00:00
/var/www/docs.ssmods.com/process/src/src/HasOneButtonField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 | <?php namespace SilverShop\HasOneField; use SilverStripe\ORM\DataObject; use SilverStripe\View\Requirements; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig; use SilverShop\HasOneField\GridFieldHasOneButtonRow; use SilverStripe\Forms\GridField\GridFieldDetailForm; use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; /** * Class HasOneButtonField */ class HasOneButtonField extends GridField { /** * The related object to the parent * * @var DataObject */ protected $record; /** * The current parent of the relationship (the base object we are editing) * * @var DataObject */ protected $parent; /** * The name of the relation this field is managing * * @var string */ protected $relation; /** * HasOneButtonField constructor. * @param \SilverStripe\ORM\DataObject $parent * @param string $relationName * @param string|null $fieldName * @param string|null $title * @param GridFieldConfig|null $customConfig * @param boolean|null $useAutocompleter */ public function __construct(DataObject $parent, $relationName, $fieldName = null, $title = null, GridFieldConfig $customConfig = null, $useAutocompleter = true) { $record = $parent->{$relationName}(); $this->setRecord($record); $this->parent = $parent; $this->relation = $relationName; Requirements::css("silvershop/silverstripe-hasonefield:client/dist/styles/bundle.css"); Requirements::javascript("silvershop/silverstripe-hasonefield:client/dist/js/bundle.js"); $config = GridFieldConfig::create() ->addComponent(new GridFieldHasOneButtonRow()) ->addComponent(new GridFieldSummaryField($relationName)) ->addComponent(new GridFieldDetailForm()) ->addComponent(new GridFieldHasOneUnlinkButton($parent, 'buttons-before-right')) ->addComponent(new GridFieldHasOneEditButton('buttons-before-right')); if ($useAutocompleter) { $config->addComponent(new HasOneAddExistingAutoCompleter('buttons-before-right')); } $list = HasOneButtonRelationList::create($parent, $this->record, $relationName); // Limit the existing list so that autocomplete will find results $list = $list->filter("ID", $this->record->ID); parent::__construct($fieldName ?: $relationName, $title, $list, ($customConfig) ?: $config); $this->setModelClass($record->ClassName); } /** * @return \SilverStripe\ORM\DataObject */ public function getRecord() { return $this->record; } /** * @param DataObject|null $record */ public function setRecord($record) { $this->record = $record ?: singleton(get_class($this->record)); } /** * Get the current parent * * @return DataObject */ public function getParent() { return $this->parent; } /** * Set the current parent * * @param DataObject $parent parent of the relationship * * @return self */ public function setParent(DataObject $parent) { $this->parent = $parent; return $this; } /** * Get the name of the relation this field is managing * * @return string */ public function getRelation() { return $this->relation; } /** * Set the name of the relation this field is managing * * @param string $relation The relation name * * @return self */ public function setRelation(string $relation) { $this->relation = $relation; return $this; } } |