0

I am creating a custom content entity and in the BaseFieldDefinitions, I am trying to create a field for assigning a user as the designated assigner and will follow up with a assignee. So far, the code has been pretty easy to follow and makes sense, except for setSetting.

Basically the field below works, but its copy and pasted from drupal console as boiler plate code. What I don't understand is the setSetting part. If I comment out ->setSetting('target_type', 'user'), the field displays but no longer works.

So my question is, how am I suppose to know, or figure out, that I should be typing in 'target_type' and value 'user'? I went through the API but haven't managed to find any references to this. And I don't understand the setSetting below it either, 'handler' = 'default. I can comment that out and it works fine. So what gives?

How am I suppose to know what to set in the setSettings part? As I also noticed, not every field definition even has this.

$fields['assigner'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Assigner'))
  ->setDescription('The person who will issue this Task.')
  ->setRevisionable(TRUE)
  ->setSetting('target_type', 'user')
  ->setSetting('handler', 'default')
  ->setTranslatable(TRUE)
  ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'author',
      'weight' => 0,
  ])
  ->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 5,
      'settings' => [
          'match_operator' => 'CONTAINS',
          'size' => '60',
          'autocomplete_type' => 'tags',
          'placeholder' => '',
      ],
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);
  • target_type is the name of the database column which stores that data for an entity reference field. You find it by looking at the class which defines the field type, something, to be honest, you just need to know to do. In my experience unless you stumble across these concepts by chance in tutorials or generally reading through the core code, you're not going to know them. Trying to document something like this is difficult, as it's extremely flexible, and involves a lot of moving parts – Clive Apr 26 at 12:40
  • You can also try the options available when configuring the field type in UI and check the exported yaml files. – 4k4 Apr 26 at 14:49
0

You can look at the source code of the corresponding field type class to get an idea of the available settings.

target_type is something specific for an ER field

Base classes for field types provided in Drupal core

Almost all field types are derived from one of the above base classes, e.g. there are several number field types deriving from NumericItemBase

Custom field types are usually located in module_name/src/Plugin/Field/FieldType

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.