Skip to content

Commit

Permalink
Move schema data into getSchemaDataDefaults() from Field()
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Nov 15, 2018
1 parent c766cbc commit dbc519e
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/TagField.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,30 @@ public function Field($properties = [])
Requirements::css('silverstripe/tagfield:client/dist/styles/bundle.css');
Requirements::javascript('silverstripe/tagfield:client/dist/js/bundle.js');

$schema = [
'name' => $this->getName() . '[]',
'lazyLoad' => $this->getShouldLazyLoad(),
'creatable' => $this->getCanCreate(),
'multi' => $this->getIsMultiple(),
'value' => $this->Value(),
'disabled' => $this->isDisabled() || $this->isReadonly(),
];
$this->addExtraClass('ss-tag-field');

return $this->customise($properties)->renderWith(self::class);
}

/**
* Provide TagField data to the JSON schema for the frontend component
*
* @return array
*/
public function getSchemaDataDefaults()
{
$schema = array_merge(
parent::getSchemaDataDefaults(),
[
'name' => $this->getName() . '[]',
'lazyLoad' => $this->getShouldLazyLoad(),
'creatable' => $this->getCanCreate(),
'multi' => $this->getIsMultiple(),
'value' => $this->Value(),
'disabled' => $this->isDisabled() || $this->isReadonly(),
]
);

if (!$this->getShouldLazyLoad()) {
$schema['options'] = array_values($this->getOptions()->toNestedArray());
} else {
Expand All @@ -220,13 +236,20 @@ public function Field($properties = [])
$schema['optionUrl'] = $this->getSuggestURL();
}

$this->setAttribute('data-schema', json_encode($schema));

$this->addExtraClass('ss-tag-field');
return $schema;
}

return $this
->customise($properties)
->renderWith(self::class);
/**
* When not used in a React form factory context, this adds the schema data to SilverStripe template
* rendered attributes lists
*
* @return array
*/
public function getAttributes()
{
$attributes = parent::getAttributes();
$attributes['data-schema'] = json_encode($this->getSchemaData());
return $attributes;
}

/**
Expand Down

0 comments on commit dbc519e

Please sign in to comment.