Getting a node (or other entity) and playing with it on the command line
Here's one way to get a node so that you can play with it on the command line and figure out what different methods actually do:
array_pop(\Drupal::service('entity_type.manager')->getStorage('node')->loadByProperties(['title' => 'Sample node']))
There's surely faster ways to do it with an ID, and maybe even better would be to use devel generate here. Anyway, this gives a node that can be messed with, like so, putting it all together:
drush ev "print array_pop(\Drupal::service('entity_type.manager')->getStorage('node')->loadByProperties(['title' => 'Sample poster']))->toUrl()->setAbsolute()->toString();"
That's how i got from toUrl() failing because it returns an object, to both returning a string and an absolute path.
The methods to try i'm getting from the API documentation, in this case https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.2.x
A better command line shell interface for Drupal must be coming (right Console?) with autocompletion and such but for now we still have Drush eval and the docs.
Comments
Post new comment