Score:5

Add third party settings to a basefield definition

cn flag

Is there a way to add third party settings when defining a basefield for a custom Content Entity?

I am creating a new Entity type and want to keep the fields defined in code vs config as much as I can. I have an address field and a geofield field defined. I need to add third party settings to the geofield to allow it to geolocate from the Address field.

I was able to get the settings from the yaml config which I translated to a php array and have tried applying the settings to the field via the ->setSettings() method, however that doesn't seem to work.

Anyone got any ideas?

$fields['location'] = BaseFieldDefinition::create('geofield')
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setLabel(t('Location Coordinates'))
      ->setRequired(FALSE)
      ->setCardinality(1)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
        'type' => 'geofield_latlon',
        'weight' => 4,
        'settings' => [
          'html5_geolocation' => FALSE,
        ],
      ])
      ->setSettings(
        [
          'third_party_settings' => [
            'geocoder_field' => [
              'method' => 'geocode',
              'weight' => 0,
              'field' => 'address',
              'skip_not_empty_value' => false,
              'disabled' => false,
              'hidden' => false,
              'providers' => [
                0 => 'googlemaps',
              ],
              'dumper' => 'wkt',
              'delta_handling' => 'default',
              'failure' => [
                'handling' => 'preserve',
                'status_message' => true,
                'log' => true,
              ],
              'reverse_geocode' => [
                'field' => '',
              ],
            ],
          ],
        ]
      )
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('view', [
        'type' => 'geofield_latlon',
        'label' => 'hidden',
        'weight' => 10,
      ]);
4uk4 avatar
cn flag
You see some issues when modules try to use BaseFieldDefinition::getThirdPartySettings and get an error. They then check whether the field definition is implementing ThirdPartySettingsInterface, otherwise they ignore third party settings. So I think you need to extend BaseFieldDefinition adding an implementation of ThirdPartySettingsInterface and then such modules should work as intended.
I sit in a Tesla and translated this thread with Ai:

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.