Accessing Field Value before Field::resolve()
I have a custom Nova field and I need to add additional metadata that is accessible to Vue based on the value of the field.
The field is a file preview field for the detail page only. It takes in the path of a file (similar to the File field), and from there, if the file type is previewable in the browser (like the PDF viewer), it will display it in a panel.
In order to determine if a given file is previewable, I need to compare its file type to a list of known file types. But any time I try to access $this->value it is always null, even though this.field.value shows the value just fine in Vue.
Here is the FilePreview.php Nova field:
<?php
namespace Ecsc\FilePreview;
use App\Statuses\FileType;
use Illuminate\Support\Facades\Storage;
use Laravel\Nova\Fields\Field;
use Illuminate\Support\Facades\File as FileFacade;
use Laravel\Nova\Http\Requests\NovaRequest;
class FilePreview extends Field
{
/**
* The field's component.
*
* @var string
*/
public $component = 'file-preview';
/**
* The disk the file uses. Default is public
*
* @var string
*/
public string $disk = 'public';
public function __construct($name, $attribute = null, callable $resolveCallback = null)
{
parent::__construct($name, $attribute, $resolveCallback);
}
public static function make(...$arguments): self
{
return (new FilePreview(...$arguments))
->previewTypeConstruction();
}
public function previewTypeConstruction(): self
{
$genericType = match(FileType::getExtension($this->value)) {
'pdf' => 'document',
'txt', 'tex', 'rtf', 'md' => 'text',
'mp3', 'wav', 'ogg' => 'audio',
'mp4', 'mpg', 'mpeg', 'mov' => 'video',
'jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg' => 'image',
default => 'unknown',
};
return $this->withMeta(['fileGenericType' => $genericType]);
}
}
So here, I am trying to get the file type / extension from $this->value, where $this->value should be the file path stored in the database. The path stores properly, and displays on Vue properly, but when accessing it here, it is ALWAYS null.
I'm not sure how to access this. I have tried overriding the Field class's resolve() and resolveForDisplay() methods, but that doesn't seem to work either.
Please or to participate in this conversation.