7

I know the Automatic Nodetitles module exists, but it is only for Drupal 7.

Is there a method of automatically assigning the title with the NID upon clicking submit? I know that the NID doesn't exist until the submit button is hit. How would one embed the NID in the title?

What hook would one use?

2
  • Have you tried Auto EntityLabel for D8? A dev branch exists. Have you also tried Rules?
    – Kevin
    Commented Aug 27, 2016 at 3:04
  • @Kevin unfortunately, Rules 8 is MIA.
    – No Sssweat
    Commented Aug 28, 2016 at 13:17

3 Answers 3

5

You need to do two step:

Step one: auto generate the title and hidden form. use hook_field_widget_form_alter() with code

function MY_MODULE_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  $field_definition = $context['items']->getFieldDefinition();
  $bundle = $form_state->getFormObject()->getEntity()->bundle();
  // You can check with other bundle
  if ($field_definition->getName() == 'title' && $bundle == 'page' && $element['value']['#default_value'] == '') {
    $element['#required'] = FALSE;
    //hidden title  
    $element['#type'] = 'hidden';
    $element['value']['#required'] = FALSE;
    // Set default value
    $element['value']['#default_value'] = 'Default Title';
  }
}

with step one you can cross submit form.

Step two: Override new title with default value. Use hook_node_insert()

function MY_MODULE_node_insert(Drupal\node\NodeInterface $node) {
  if ($node->bundle() == 'page') {
    $new_title = 'New title ' . $node->id();
    $node->setTitle($new_title);
    $node->setNewRevision(FALSE);
    $node->save();  
  }
}
1
  • 2
    If it has the the nid, which is the only information only available after saving, then this is the right way. Anything else, like the date or something like that would be possible from presave hook, which would be faster as you wouldn't have to resave twice. If you really need this, you should also add a check to make sure that title is not the default.
    – Berdir
    Commented Aug 27, 2016 at 12:03
1

You can use the Hook Post Action module

So this module introduces several new Drupal hooks to overcome this limitation

  • hook_entity_postsave
  • hook_entity_postinsert
  • hook_entity_postupdate
  • hook_entity_postdelete
  • hook_node_postsave
  • hook_node_postinsert
  • hook_node_postupdate
  • hook_node_postdelete

and one you have it installed you can use the hook_node_postsave() hook

In the Automatic Nodetitles module page says:

Be advised that there is a better maintained alternative available: https://www.drupal.org/project/auto_entitylabel

0

This may be an old issue, but here's my take. Let's say you need this for node add article.

<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;

function MYMODULE_form_node_article_form_alter(&$form, FormStateInterface $form_state, $form_id) {

    // Hide title field to avoid confusion
    $form['title']['#access'] = FALSE;
    // Set a temporary title because it's always required on node save.
    $form['title']['widget'][0]['value']['#default_value'] = 'Dummy title';

}

function MYMODULE_node_insert(NodeInterface $node) {
  if ($node->bundle() == 'article') {
    $new_title = 'Title ' . $node->id();
    $node->setTitle($new_title);
    $node->setNewRevision(FALSE);
    $node->save();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.