Skip to main content
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);
    }
  }
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook