How to get the "field" value of config entity, with bundles, that is defined in a bundle while creating content I am creating a custom entity (WebsiteType) with bundles (Website), where I want to define a URL, among other configurations, that all content in a given bundle should inherit.

I created the fields in the config entity type and managed to show and save these fields while creating a new bundle. Nevertheless, I couldn't manage to get these values in HOOK_entity_presave.

here is a sample of the code:

src/Entity/WebsiteType

Notation:

 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid",
 *     "service_endpoint_url" = "service_endpoint_url",


Class:

class WebsiteType extends ConfigEntityBundleBase implements WebsiteTypeInterface {
  /**
   * The Website type Service Endpoint URL.
   *
   * @var string
   */
  protected $service_endpoint_url;

}

 /**
   * {@inheritdoc}
   */
  public function getServiceUrl() {
    return $this-&gt;service_endpoint_url;
  }


WebsiteTypeForm:

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  $website_type = $this-&gt;entity;

  $form['endpoint_settings']['service_endpoint_url'] = [
    '#type' =&gt; 'textfield',
    '#title' =&gt; $this-&gt;t('Argir Endpoint URL'),
    '#maxlength' =&gt; 255,
    '#description' =&gt; $this-&gt;t("The location of your service."),
    '#required' =&gt; TRUE,
    '#default_value' =&gt; $website_type-&gt;getServiceUrl(),
    '#attributes' =&gt; [
      'placeholder' =&gt; $this-&gt;t('https://XXXXX.com'),
    ],
  ];
}


WebsiteTypeInterface:

interface WebsiteTypeInterface extends ConfigEntityInterface {

  public function getServiceUrl();
}


I followed the documentation of Drupal of Entity API

Checked the answer here: enter link description here

also went through this outstanding tutorial: enter link description here

Tried to work with EntityTypeManager

All of that with no luck. Any advice is highly appreciated.
While creating content, HOOK_entity_presave() is called for the content entity and you can access only content fields. One of these content entity fields is a reference field to the bundle config entity, usually named bundle. Use this field to get the config entity via the magic property entity and get the config field value via the method you have defined in WebsiteTypeInterface:

$url = $entity-&gt;bundle-&gt;entity-&gt;getServiceUrl();


A more generic way when you don't know the bundle field name or the code should work for all entity types:

$bundle_field_name = $entity-&gt;getEntityType()-&gt;getKey('bundle');
if (!empty($bundle_field_name)) {
  $bundle_config = $entity-&gt;get($bundle_field_name)-&gt;entity;
  if ($bundle_config instanceof \Drupal\mymodule\Entity\WebsiteTypeInterface) {
    $url = $bundle_config-&gt;getServiceUrl();
  }
}