Skip to main content
add check for WebSiteTypeInterface
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

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->bundle->entity->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->getEntityType()->getKey('bundle');
if (!empty($bundle_field_name)) {
  $bundle_config = $entity->get($bundle_field_name)->entity;
  if ($bundle_config instanceof \Drupal\mymodule\Entity\WebsiteTypeInterface) {
    $url = $bundle_config->getServiceUrl();
  }
}

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->bundle->entity->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->getEntityType()->getKey('bundle');
if (!empty($bundle_field_name)) {
  $bundle_config = $entity->get($bundle_field_name)->entity;
  if ($bundle_config) {
    $url = $bundle_config->getServiceUrl();
  }
}

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->bundle->entity->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->getEntityType()->getKey('bundle');
if (!empty($bundle_field_name)) {
  $bundle_config = $entity->get($bundle_field_name)->entity;
  if ($bundle_config instanceof \Drupal\mymodule\Entity\WebsiteTypeInterface) {
    $url = $bundle_config->getServiceUrl();
  }
}
add generic way
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

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->bundle->entity->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->getEntityType()->getKey('bundle');
if (!empty($bundle_field_name)) {
  $bundle_config = $entity->get($bundle_field_name)->entity;
  if ($bundle_config) {
    $url = $bundle_config->getServiceUrl();
  }
}

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->bundle->entity->getServiceUrl();

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->bundle->entity->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->getEntityType()->getKey('bundle');
if (!empty($bundle_field_name)) {
  $bundle_config = $entity->get($bundle_field_name)->entity;
  if ($bundle_config) {
    $url = $bundle_config->getServiceUrl();
  }
}
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

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->bundle->entity->getServiceUrl();