Dynamic date in the text block

Thanks for the hint.
I need to extend the query in the plugin as currently it only checks if the value is not empty if ($option->key()->isNotEmpty()) I hope I can manage this with my level of knowledge :man_facepalming:

Works, but does not appear to be an efficient solution.

'fieldMethods' => [
    'toOptions' => function ($field) {
        $result = [];
        foreach ($field->toStructure() as $option) {
            if ($option->key()->isNotEmpty()) {
                $datatype = $option->datatype()->value();
                if ($datatype === 'date' && $option->date()->isNotEmpty()) {
                    $result[$option->key()->value()] = $option->date()->toDateDiff();
                } elseif ($datatype === 'text' && $option->text()->isNotEmpty()) {
                    $result[$option->key()->value()] = $option->text()->value();
                }
            }
        }
        return $result;
	},

Not really…

Why are you checking if the fields are empty? What do you expect as output if the value is empty? Leave the placeholder intact? Show or throw an error?

This logical thought is convincing! Thanks for the hint.

I have now added ‘required’ to the ‘datatype’ field in the blueprint to avoid an empty selection here at least. The code in the plug-in now looks like this:

'fieldMethods' => [
    'toOptions' => function($field) {
        $result = [];
        foreach ($field->toStructure() as $option) {
            if ($option->key()->isNotEmpty()) {
                $dataType = $option->datatype()->value();

                if ($dataType === 'date') {
                    $result[$option->key()->value()] = $option->date()->toDateDiff();
                } else {
                    $result[$option->key()->value()] = $option->text()->value();
                }
            }
        }
        return $result;
  	},

I am grateful for suggestions for improvement, not only to optimize the code, but also my :brain: knowledge.