User login

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.

Searched words: 
Drupal 8 command line shell interface for Drupal

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.