Score:1

How to use entityQuery with custom field type?

mx flag
awm

I am working on building a query for nodes that have a few custom fields (field types). The fields are custom build in a custom module with a custom schema. When I execute the query:

 $query = \Drupal::entityQuery('node')
    ->condition('type', 'place');
 $query->condition('my_custom_field', '', '<>');

It fails with a message indicating that: node__my_custom_field.my_custom_field_value does not exist (NOT found). This is correct the table has a custom schema for this field _value does not exist:

 drush sqlq "describe node__my_custom_field"

bundle  varchar(128)    NO  MUL     
deleted tinyint(4)  NO  PRI 0   
entity_id   int(10) unsigned    NO  PRI NULL    
revision_id int(10) unsigned    NO  MUL NULL    
langcode    varchar(32) NO  PRI     
delta   int(10) unsigned    NO  PRI NULL    
my_custom_field_title   text    YES     NULL    
my_custom_field_description text    YES     NULL

How do I make my entity field query work with this field custom schema?

Score:2
cn flag

It looks like your custom field type has title and description columns rather than value (the default Drupal assumes if you don't tell it otherwise).

You can specify the field column to use with . notation like this:

$query->condition('my_custom_field.title', '', '<>');
$query->condition('my_custom_field.description', '', '<>');

If you want the system to consider, for example, the title property as the default instead of value, you can implement FieldItemBase::mainPropertyName() in your field class:

public static function mainPropertyName() {
  return 'title';
}

That would make your original line work for the title column:

$query->condition('my_custom_field', '', '<>');
awm avatar
mx flag
awm
Thanks .. I should have figured this one ..
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.