I'm finding that the biggest problem in working with Drupal 8 is I can't get the data I need. Drupal 8 wants me to use public methods rather than manually drilling down thru an object. The problem is, I can't figure out a consistent way for getting a list of available methods! (they magically exist, and I feel like I'm just supposed to know about them).=
For this example, let's say I have a content type with a video field. I need to get the raw URL of the video file in that field.
So I'd start with a node id ($nid), and somehow I have to figure out how to load the node. This isn't too bad because there's lots of examples. So I do something like $node = \Drupal\node\Entity\Node::load($nid);
.
So far so good. Then I need to get the value of my video field (field_main_video). This took me FOREVER to figure out because there's conflicting documentation around the 'net. Finally I figured out I'd have to do something like this (because it is a multivalue item):
$video = \Drupal\node\Entity\Node::load($nid)->field_main_video->getValue();
...then loop thru the array etc. Using kint didn't help me find this, either. Because, for example, if I kint($node)
and look under methods, I don't see getValue() as an item there. Still not terrible, because there were enough examples around to figure it out.
As I go deeper, though, what I didn't know (this is the important part) was that rather than getting the video field entity id, then loading the entity, then finding the "uri" field in the entity, etc (like I would in D7): There was a method that lets me get the URI all in this same line of code!
$url = \Drupal\node\Entity\Node::load($nid)->field_main_video->entity->getFileUri();
But how could I possibly have known this getFileUri() existed? I happened to stumble upon it in a blog post. This truly DOES make getting a URI easier than in D7...but only if you know (magically) what methods exist for each 'level' of an object.
In the end, with this example I'm asking: How do you find all public methods for each level of an object in a way that's easy to read and understand? Note that it seems there should be a drupal-centric (ie. devel module) way of doing this rather than manually searching api.drupal.org or using something IDE specific?