Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Code snippet that can be used to programmatically generate a title for node in Drupal 8.

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MODULE_form_node_CONTENT_TYPE_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  //Hide the title
  $form['title']['#access'] = FALSE;
}
function MODULE_entity_presave(EntityInterface $entity)
{
  if ($entity->getEntityTypeId() == 'node') {
    if ($entity->getType() == 'article') {
      $name = $entity->get('field_name')->value;
      $lastname = $entity->get('field_lastname')->value;
      $entity->set('title', $name . ' - ' . $lastname);
    }
  }
}

Comments

Submitted by Gaetano Leoni (not verified) on Tue, 04/07/2020 - 08:21 Permalink

I tried your code and I ran into problems. The writing remmata refers to FORM_ID_ALTER while the func refers to MODULE_form_node_CONTENT_TYPE_form_alter I changed the name of the module from MODULE to personal (it's the module I wrote). in the C.T. article I entered the name and lastname fields. When I insert a new article, the mandatory insertion of the title remains. The code $ form ['title'] ['# access'] = FALSE; does not work. I tried to change the code in $ form ['title'] ['widget'] ['0'] ['value'] ['# access'] = FALSE; but it doesn't work the same. Am I wrong?
Submitted by Gaetano Leoni (not verified) on Tue, 04/07/2020 - 11:04 Permalink

The code works, I'm the one who hasn't understood many things yet Thanks for the code
Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon