Skip to content

Commit

Permalink
#168 adds enabled: attribute to matrix mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Dec 4, 2018
1 parent f3503d2 commit f4455ff
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Listeners/GetMatrixFieldSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function handle($event) {
foreach ($blockTypes as $blockType) {
$type = $union->addType(ucfirst($field->handle).ucfirst($blockType->handle), $blockType);
$type->addStringField('id'); // ideally this would be an `int`, but draft matrix blocks have an id of `new1`
$type->addBooleanField('enabled');
$type->addFieldsByLayoutId($blockType->fieldLayoutId);

if (empty($type->getFields())) {
Expand All @@ -75,6 +76,7 @@ function handle($event) {
if (!empty($blockTypes)) {
$inputType = $event->mutation->createInputObjectType(ucfirst($event->sender->handle) . 'Input');
$inputType->addStringArgument('id');
$inputType->addBooleanArgument('enabled');

foreach ($blockTypes as $blockType) {
$blockInputType = $event->mutation->createInputObjectType(ucfirst($event->sender->handle) . ucfirst($blockType->handle) . 'Input');
Expand All @@ -96,13 +98,24 @@ function handle($event) {

foreach ($values as $index => $value) {
$id = @$value['id'] ? $value['id'] : "new{$index}";
$enabled = @$value['enabled'] ? $value['enabled']: 0;
unset($value['id']);
if (isset($value['type'])) {
$type = $value['type'];
$fields = $value['fields'];
}
else {
$type = array_keys($value)[0];
// get the keys
$keys = array_keys($value);
// remove known keys
$keys = array_flip($keys);
unset($keys['id']);
unset($keys['enabled']);
unset($keys['type']);
unset($keys['fields']);
$keys = array_merge(array_flip($keys));
// type is the only remaining key
$type = $keys[0];
$fields = $value[$type];
}

Expand All @@ -119,7 +132,7 @@ function handle($event) {

$newValues[$id] = [
'type' => $type,
'enabled' => 1,
'enabled' => $enabled,
'fields' => $fields,
];
}
Expand Down

0 comments on commit f4455ff

Please sign in to comment.