Drupal7 Nodes and the Command Line

David G - DrupalAnother question I was fielded recently was how to view a Drupal node from the command line, simply how to inspect a Node’s state. Obviously something like drush ev “var_dump(node_load(3));” will produce something usable. But, I prefer to leverage the hard work of others! So as a sitewide install to my development box I have installed Drush Entity. It allows inspection of Nodes (and Entities) from the command line among other useful commands.

Drush Entity has the following commands:

$ drush help --filter=entity
Entity commands: (entity)
 entity-create (ec)    Create an entity from a json object                       
 entity-delete (ed)    Delete entities.                                          
 entity-list (el)      Get a list of entity type information in a summary table. 
 entity-read (er)      Print entity contents                                     
 entity-type-read      List details of entity types                              
 (etr)                                                                           
 entity-update (eu)    Update an entity as json in the default editor

As you can see it allows you to easily view the contents of a Node, as an example:

$drush er node 3

Array
(
    [3] => stdClass Object
        (
            [vid] => 3
            [uid] => 0
            [title] => Error
            [log] => 
            [status] => 1
            [comment] => 1
            [promote] => 0
            [sticky] => 0
            [vuuid] => e6d7880a-169f-41d2-90fa-447bcf8dc70f
            [ds_switch] => 
            [nid] => 3
            [type] => page
            [language] => und
            [created] => 1391019228
            [changed] => 1391019228
            [tnid] => 0
            [translate] => 0
            [uuid] => cc820884-f87b-4c43-afaf-dad9973b4863
            [revision_timestamp] => 1391019228
            [revision_uid] => 0
            [body] => Array
                (
                )

            [rdf_mapping] => Array
                (
                    [rdftype] => Array
                        (
                            [0] => foaf:Document
                        )

                    [title] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:title
                                )

                        )

                    [created] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:date
                                    [1] => dc:created
                                )

                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )

                    [changed] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => dc:modified
                                )

                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )

                    [body] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => content:encoded
                                )

                        )

                    [uid] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:has_creator
                                )

                            [type] => rel
                        )

                    [name] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => foaf:name
                                )

                        )

                    [comment_count] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:num_replies
                                )

                            [datatype] => xsd:integer
                        )

                    [last_activity] => Array
                        (
                            [predicates] => Array
                                (
                                    [0] => sioc:last_activity_date
                                )

                            [datatype] => xsd:dateTime
                            [callback] => date_iso8601
                        )

                )

            [webform] => Array
                (
                    [confirmation] => 
                    [confirmation_format] => 
                    [redirect_url] => <confirmation>
                    [block] => 0
                    [allow_draft] => 0
                    [auto_save] => 0
                    [submit_notice] => 1
                    [submit_text] => 
                    [next_serial] => 1
                    [submit_limit] => -1
                    [submit_interval] => -1
                    [total_submit_limit] => -1
                    [total_submit_interval] => -1
                    [progressbar_page_number] => 0
                    [progressbar_percent] => 0
                    [progressbar_bar] => 1
                    [progressbar_pagebreak_labels] => 1
                    [progressbar_include_confirmation] => 1
                    [progressbar_label_first] => Start
                    [progressbar_label_confirmation] => Complete
                    [preview] => 0
                    [preview_next_button_label] => 
                    [preview_prev_button_label] => 
                    [preview_title] => 
                    [preview_message] => 
                    [preview_message_format] => 
                    [preview_excluded_components] => Array
                        (
                        )

                    [status] => 1
                    [record_exists] => 
                    [roles] => Array
                        (
                            [0] => 1
                            [1] => 2
                        )

                    [emails] => Array
                        (
                        )

                    [components] => Array
                        (
                        )

                    [conditionals] => Array
                        (
                        )

                )

            [cid] => 0
            [last_comment_timestamp] => 1391019228
            [last_comment_name] => 
            [last_comment_uid] => 0
            [comment_count] => 0
            [name] => 
            [picture] => 0
            [data] => 
        )

)

This is output of a Node where the nid = 3 for a recent project. You can glean alot from this output (without having to open a browser and goto the page):

  • It is a Page content type.
  • The site uses the UUID module.
  • The Node is on it’s 3rd revision.
  • The site uses Display Suite.

Unfortunately the inquiry I received ended up being for Drupal6 which is not supported by this tool. But, Drush works with Drupal6 and a similar module called Node Export can export for instance a Json representation of a node to STDOUT and you could pipe it to Less if you wanted. So Drush and its community driven command sets can be very useful across the version(s) of Drupal you may be developing in.

Another reason I mention Drush Entity is that I just begun a project where a website is a mix of legacy database content and then a handful of static HTML pages. I could envision creating a small Drupal application that uses CuRL to scrape webpages as JSON objects which for example get imported into the new site using drush entity-create. You can think of this approach as a poor man’s quick and dirty Migrate implementation for a handful of HTML pages …. I’m really liking that idea! But, I’m so familiar with Migrate I may just use that, haha.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

 

Tags: , , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *