Skip to content

Commit

Permalink
Update RelationInput with recent changes to SelectizeInput
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiclord committed Aug 28, 2017
1 parent 30f3b35 commit 326f0ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
3 changes: 0 additions & 3 deletions metadata/charcoal/relation/pivot-interface.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@

"multiple": false,

"choice_obj_map": {
"label": "title"
},
"selectize_options": {
"create": false,
"plugins": {
Expand Down
40 changes: 40 additions & 0 deletions src/Charcoal/Admin/Property/Input/Selectize/RelationInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class RelationInput extends SelectizeInput
*/
private $targetObjectType;

/**
* Check used to parse multi Choice map against the obj properties.
*
* @var boolean
*/
protected $isChoiceObjMapFinalized = false;

/**
* Retrieve the target object type to build the choices from.
*
Expand Down Expand Up @@ -65,4 +72,37 @@ private function resolveTargetObjectType()

return $resolved;
}

/**
* Retrieve the object-to-choice data map.
*
* @return array Returns a data map to abide.
*/
public function choiceObjMap()
{
if ($this->choiceObjMap === null) {
$map = $this->defaultChoiceObjMap();

$model = $this->modelFactory()->get($this->targetObjectType());
$objProperties = $model->properties();

if ($objProperties instanceof \Iterator) {
$objProperties = iterator_to_array($objProperties);
}

foreach ($map as &$mapProp) {
$props = explode(':', $mapProp);
foreach ($props as $p) {
if (isset($objProperties[$p])) {
$mapProp = $p;
break;
}
}
}

$this->choiceObjMap = $map;
}

return $this->choiceObjMap;
}
}

0 comments on commit 326f0ea

Please sign in to comment.