thepearson / silverstripe-hasonefield

Edit a has_one relationship using the gridfield editor, triggered by clicking a button.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SilverStripe has_one field

Allows you to create a CMS button for creating and editing a single related object. It is actually a grid field, but just looks like a button.

demo

Usage

In Warehouse.php context:

    use SilverShop\HasOneField\HasOneButtonField;

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		if($this->Address()->exists()){
			$fields->addFieldsToTab("Root.Main", array(
				ReadonlyField::create("add", "Address", $this->Address()->toString())
			));
		}
		$fields->removeByName("AddressID");
		$fields->addFieldToTab("Root.Main",
            // $dataObject, 'RelationName', 'Optional title - would default to Relation Name'
			HasOneButtonField::create($this, "Address") //here!
		);

		return $fields;
	}

You must pass through the parent context ($this), so that the has_one relationship can be set by the GridFieldDetailForm.

About

Edit a has_one relationship using the gridfield editor, triggered by clicking a button.


Languages

Language:PHP 94.6%Language:Scheme 2.3%Language:JavaScript 2.2%Language:CSS 1.0%