I’ve got a fieldMethod where I need to check for a bool ‘headers’ prop in order to arrange my array accordingly.
'toTable' => function ($field) {
if (!is_string($field->value()) || empty($field->value())) {
return ['headers' => [], 'rows' => []];
} else {
$array = Str::split($field, "\n");
$newRow = $rows = [];
foreach ($array as $value) {
$value = trim($value);
if ($value === '-') {
if (!empty($newRow)) {
$rows[] = $newRow;
$newRow = [];
}
} else {
$value = trim($value, '- ');
$value = trim($value, '"\'');
$newRow[] = $value;
}
}
if (!empty($newRow)) {
$rows[] = $newRow;
}
$blueprint = $field->parent()->blueprint()->field($field->key());
$hasHeaders = $blueprint['headers'] ?? true;
$headers = $hasHeaders ? array_shift($rows) : [];
return ['headers' => $headers, 'rows' => $rows];
}
}
Now I’ve managed to do so by accessing the field blueprint and looking for the ‘headers’ prop:
$blueprint = $field->parent()->blueprint()->field($field->key());
$hasHeaders = $blueprint['headers'] ?? true;
$headers = $hasHeaders ? array_shift($rows) : [];
Though it works I don’t really like this solution as the $field->key() always returns lowercase and in scenarios where you got ‘tableSecond’ for example it cannot find the field blueprint.
I’m wondering what other solutions might be in this case to get a field prop in a field method? Or even if I’m looking at this problem in the right way…