I have a custom entity with a field which is defined as follows.
$fields['file'] = BaseFieldDefinition::create('file')
->setDescription('Reference to the built-in core File entity type.')
->setLabel('File')
->setRequired(TRUE)
->setSetting('file_extensions', 'pdf rtf doc docx')
->setSetting('max_filesize', '20MB')
->setSetting('description_field', TRUE)
->setDisplayOptions('form', ['type' => 'file'])
->setDisplayOptions('view', ['type' => 'file']);
I have no problem getting to the fields of the referenced File
entity from instances of my own entity type. So for example, these lines work as expected.
$filename = $my_entity->file->entity->filename->value;
$username = $my_entity->file->entity->uid->entity->name->value;
When I try to use the fields of the referenced File
entity with a TableSort
, which I want to plug into an entity query on my custom entity type, the code throws an exception.
$header['name'] = [
'data' => 'File Name',
'field' => 'file.entity.filename',
'specifier' => 'file.entity.filename',
];
$header['user'] = [
'data' => 'Uploaded By',
'field' => 'file.entity.uid.entity.name',
'specifier' => 'file.entity.uid.entity.name',
];
// …
$query->tableSort($header);
Uncaught PHP Exception Drupal\Core\Entity\Query\QueryException: "'file' not found"
For the syntax of the $header
array I'm largely relying on clues I got from Tablesort with EntityQuery, in which Berdir recommends that we look at the load()
and buildHeader()
methods of the UserListBuilder
class. Unfortunately, that example appears to be using field values which are stored directly in the base table, so I'm falling back on the assumption that, since we're in entity query land, the same syntax described in QueryInterface::condition()
would be expected.
I've looked at a number of the open issues, such as Table sort ignores "field", always adds header title as order query parameter and Tablesort cleanup, but I didn't see anything which shed any light on the problem.