private function getModelFields(ModelInterface $model, $properties = null)
    {
        if ($properties === null) {
            // No custom properties; use all (from model metadata)
            $properties = array_keys($model->metadata()->properties());
        } else {
            // Ensure the key is always in the required fields.
            $properties = array_unique(array_merge([ $model->key() ], $properties));
        }

        $fields = [];
        foreach ($properties as $propertyIdent) {
            $prop = $model->property($propertyIdent);
            if (!$prop || !$prop->active() || !$prop->storable()) {
                continue;
            }

            $val = $model->propertyValue($propertyIdent);
            foreach ($prop->fields($val) as $fieldIdent => $field) {
                $fields[$field->ident()] = $field;
            }
        }

        return $fields;
    }