0

Drupal 8 with custom config entity.

I have a custom config entity that has various custom properties such as 'product_id'. I am loading the populated config entity like this:

$config = $this->entityTypeManager->getStorage('role_subscribe')->loadByProperties(['product' => $product_id]);

This gives me an array containing all configurations matching my property. I use foreach to get to the individual config entities, but I can't find a way to load the properties so that I can check their values.

In my role_subscribe entity interface I have getter methods for each property, but they don't work with the entity loaded by entityTypeManager.

How can I access the values of the individual properties in my config entity?

As suggested below, I made my properties public and it now works, but I feel like I should be able to use my own getters which have been defined in my custom interface.

Here's the foreach loop that I'm using to act on each config entity that's loaded with entityTypeManger, but this only works because I made the properties public.

$config = $this->entityTypeManager->getStorage('role_subscribe')->loadByProperties(['product' => $product_id]);
  foreach ($config as $subscribe) {
    $role = $subscribe->role;
    $fixed_start = $subscribe->fixed_start;
    $term = strtotime($subscribe->term);
    $enrollment = $subscribe->enrollment;
  • mmm this should work like accessing any other entity value, are your properties private or protected ? Perhaps try changing one to public and dump ($config->something) – Taggart Jensen Dec 12 '18 at 22:13
  • Thanks for the quick response. The properties were set to protected. I changed them to public and I'm now able to access them with $config->property_name. – user3166999 Dec 12 '18 at 22:54
  • Properties should be protected - it sounds like your getters aren't implemented correctly – Clive Dec 13 '18 at 12:52
  • I was wondering about that. But I'm not sure how to access my getters. It seems I'm not using my interface when I load the config entity with entityTypeManager. I'm just learning this part of Drupal 8 so I'm following tutorials, but they don't show how to load the custom entity anywhere but in EntityForm, where it's available as $this->entity. – user3166999 Dec 13 '18 at 19:56
  • Updated my original question to show the exact issue. – user3166999 Dec 13 '18 at 21:15
0

there is a school of thought that properties should be protected how ever you can add getter and setter methods: Such as

<?php
 public function getRoles() {
   return $this->role
 }
/* 
or easy ... public function get(key) {
 return $this->{key};
}*/

 //then you can do when load the entity 
$role = $subscribe->getRole();

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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