Drupal 8 New Content Type and Views

I have been doing Drupal sites since 2009. For specific sites, Drupal is really great. Obviously for other types of sites WordPress is really good. As an example I am writing this post in WordPress and not Drupal.

I created a new content type, Consumer Products, in D8, in order to track their sale price and location. This is so that I can more easily remember when, where and how much we bought a sale item. Retail has regular sale cycles, so if you can catch the product at right time in the cycle, it will be cheaper.

Enter D8 and its easily created content type. This has not really changed much since D6, and with views it was relatively easy to get this going. One unfortunate issue was that when you create a content type in D8, this content type must also be one of the fields you use in your form. For example I created a content type called “Consumer Product”. This must be a field in your form. Initially I deleted this field, as I wanted my field to be named “Product” and not “Consumer Product”. Little did I know that D8 would not like this. Once I finished the content type and tried to add data, I received a nasty error.

The website encountered an unexpected error. Please try again later.

Not too helpful. I turned on debugging (Admin > Config > Development > Logging and Errors) and received more info:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘title’ cannot be null: INSERT INTO {node_field_data} (nid, vid, type, langcode, title, uid, status, created, changed, promote, sticky, revision_translation_affected, default_langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12); Array ( [:db_insert_placeholder_0] => 17 [:db_insert_placeholder_1] => 39 [:db_insert_placeholder_2] => consumer_product [:db_insert_placeholder_3] => en [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => 2 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 1489179905 [:db_insert_placeholder_8] => 1489179905 [:db_insert_placeholder_9] => 1 [:db_insert_placeholder_10] => 0 [:db_insert_placeholder_11] => 1 [:db_insert_placeholder_12] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 770 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Huh? Something to do with having a field called “title” that was null, yet D8 tried to do an SQL Insert. It turns out that the field I deleted was called “title”, which D8 happens to use as the key to the database. Of course it would complain if the key was null and you tried to do an Insert! I deleted the data, content type and view, and redid it, but this time included the content type “Consumer Product”, after which I did not get this error again.

While I still prefer to write in WordPress, sometimes D8 is just the ticket. Secure, efficient and more capable, but much more confusing than WordPress.

Leave a Reply

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