0

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->service_endpoint_url;
  }

WebsiteTypeForm:

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

  $website_type = $this->entity;

  $form['endpoint_settings']['service_endpoint_url'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Argir Endpoint URL'),
    '#maxlength' => 255,
    '#description' => $this->t("The location of your service."),
    '#required' => TRUE,
    '#default_value' => $website_type->getServiceUrl(),
    '#attributes' => [
      'placeholder' => $this->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.

1 Answer 1

2

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();
  }
}
7
  • Thanks 4k4 for the fast response. I tried it but it didn't work Error: Call to a member function getServiceUrl() on null in aegir_websites_manager_entity_presave() here is the Hook: function MY_MODULE_entity_presave(EntityInterface $entity){ $url = $entity->bundle->entity->getServiceUrl(); } What do you think? Commented Apr 13, 2020 at 9:13
  • You have to check the entity type or use hook_ENTITY_TYPE_presave. Not all entity types have bundles.
    – 4uk4
    Commented Apr 13, 2020 at 9:18
  • I tried the hook_ENTITY_TYPE_presave and still getting the same error. I am sure the entity has bundles. Is there anything else I can try? Thanks for the support 4k4 Commented Apr 13, 2020 at 9:33
  • I've suggested a shortcut, if you know the bundle field. I add a more generic way to the answer.
    – 4uk4
    Commented Apr 13, 2020 at 9:35
  • Thanks again. the bundle field name "service_endpoint_url" Commented Apr 13, 2020 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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