tl;dr: Specify a default title, and don't allow the user to change it.
The D7→D8 change records state:
node_type->has_title removed in favor of entity form displays
Now that it is possible to configure the behavior of the node title
widget using Entity form displays, the special flag to hide the title
form element for nodes is no longer needed.
To hide the title for a certain node type, use the "Manage form
display" UI or hide it with a snippet like the following:
$form_display = entity_get_form_display('node', 'article', 'default');
$form_display->removeComponent('title');
$form_display->save();
After reading that, I tried a few ways of doing it. Only the last one works, so this answer should serve as a warning of what not to try:
Edit the entity_form_display config
If it is your own module, edit core.entity_form_display.node.my_content_type.default.yml
and remove the title
key from content
. This works for fields, but if I remove the title or any of the other default things like promoted, created, etc., they are still displayed on the form at /node/add/my_content_type
.
Modify the entity_form_display in code
Add this code to my_module_install
:
$form_display = entity_get_form_display('node', 'my_content_type', 'default');
$form_display->removeComponent('title');
$form_display->save();
This works visually, but there are three problems with this solution:
- It still causes the integrity constraint errors you refer to.
- It causes the config in the database to override the config in the file at install time, which is a bad idea, IMHO.
- entity_get_form_display
is deprecated
Hide it in the entity_form_display config
Hide it, but don't remove it. In core.entity_form_display.node.my_content_type.default.yml
:
hidden:
title: false
By trial and error, I found the logic to be reversed here. This, too, causes the integrity constraint errors you refer to.
Hide it using CSS
I tried this, and it seems that empty strings are also disallowed for the title. So you either have to create your own entity type, which reimplements most of the code from the core node module, or...
The only working solution I could find
Specify a default title, and don't allow the user to change it.
function my_module_form_node_my_content_type_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form['title']['#disabled'] = 'disabled';
$form['title']['widget'][0]['value']['#default_value'] = 'none';
}
You could change none
to my_module
, a timestamp, or your favourite brand of peanut butter.
Node::setTitle
?