0

I am not a great POO specialist, maybe my ask will seems stupid x)

When I create new field from the user UI administration, I would like find quickly this field when I work from my IDE in customs modules.

So, I think I need add a custom method for the User Class, like :

public function mycustomfield_value($user){
    return $user->mycustomfield->value;
}

From my IDE, if I type "$user->" the IDE auto-complete must show me "$user->mycustomfield_value(..." method.

My ask is : how can I add this method ? I must create a new Class implement/extends User Drupal 8 Class ? I don't know how can I do that..

2
  • What exactly are you trying to achieve with your code? get the value of a user field?
    – No Sssweat
    Oct 16, 2016 at 10:47
  • Yes, I just want create a custom method 'somewhere' for access quickly to the custom User field value when I type '$user->' (with the IDE autocompletion). I'm looking for a 'proper' way (override drupal User Class if its possible?) Oct 16, 2016 at 10:58

1 Answer 1

1

Don't. The field system doesn't work like that, there is currently no useful way to get autocomplete.

You could replace the entity class with your own and add methods for your fields, but it's not worth the trouble IMHO as you might get into trouble of some other module wants to replace the class as well.

You could add helper method or functions somewhere else and pass in the entity.

Your Answer

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

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