Skip to main content

OK PhpStorm, what methods does my entity have?

Published on

Working in object-oriented programming is great. You can define interfaces which specify a contract that implementers need to fulfill. You can then extend these to define your end requirement and provide the implementation. This also means you can guarantee an object that implements that interface will have a guaranteed set of known methods. In Drupal, all entities implement EntityInterface, this is how we know that an entity can return its identifier, label, language, and other common methods. You then have ConfigEntityInterface and ContentEntityInterface which extend this base interface for configuration and content entities.

When working with an entity, like a Product in Drupal Commerce, knowing the available methods you can work with off-hand would be great. Luckily, PhpStorm has a “Structure” window which shows you methods, properties, and functions within a file. As shown below, when viewing the Structure window we see all available methods defined on our interface.

There’s just one problem. We only see the methods defined on the current interface. The ProductInterface interface extends the following interfaces (including ones through inheritance).

  • ContentEntityInterface
  • FieldableEntityInterface
  • TranslatableRevisionableInterface
  • TranslatableInterface
  • RevisionableInterface
  • EntityChangedInterface
  • EntityOwnerInterface
  • EntityPublishedInterface
  • EntityStoresInterface

It would be much more efficient to see all of the inherited methods at once. We also cannot just go view the implementation class as it has inherited interface implementations via base classes. Luckily, there’s a little button that we can toggle to show inherited methods. In the Structure window, click on the icon which looks like three connected squares.

Once the “Show Inherited” option is enabled, you will see all the methods available to your current class or interface! Methods defined in the current interface are displayed in white and those inherited are greyed out.

When you click on a method it will open the source file it is defined in. For instance, getStoreIds is from the EntityStoresInterface class and getOwner is from EntityOwnerInterface.

I highly recommend taking this approach as the code for interfaces contains good documentation.

I'm available for one-on-one consulting calls – click here to book a meeting with me 🗓️