In the node--my-content-type.html.twig, I want to add the French indefinite article to the title, which depends on the first letter of the word to which the article is added.

  • Banane => de Banane
  • Orange => d'Orange

So based on the Twig documentation, I'm trying to use one of the following code lines.

{% if label|first|lower == o %} 
{% if label|slice(0, 1)|lower == o %}
{% if label[:1]|lower == o %}

None of them work.

How can I get the first character of the node title?


You could try this code:

 * Implements hook_preprocess_HOOK().
 * @param $variables array
function TEMPLATE_preprocess_node(&$variables)
  /** @var \Drupal\node\Entity\Node $node */
  $node = $variables['node'];
  $title = $node->getTitle();
    $variables['title_addition'] = "D'";
  }else {
    $variables['title_addition'] = "De ";

 * Check if the first character is a vowel
 * @param $string string
 * @return bool
function _isFirstCharIsVowel($string){
  $vowels =
    'aàáâãāăȧäảåǎȁąạḁẚầấẫẩằắẵẳǡǟǻậặæǽǣ' .
    'eèéêẽēĕėëẻěȅȇẹȩęḙḛềếễểḕḗệḝ' .
    'iìíîĩīĭıïỉǐịįȉȋḭḯ' .
    'oòóôõōŏȯöỏőǒȍȏơǫọøồốỗổȱȫȭṍṏṑṓờớỡởợǭộǿœ' .
  // set necessary encodings
  $first = mb_substr($string, 0, 1);
  $vow = mb_ereg_replace('[^'.$vowels.']','',  $first);
  return !empty($vow);

This can be used in your .theme or .module file.
This of course will act on all nodes, so you need to add your type checking.
The _isFirstCharIsVowel function can be in a service, I placed it here for simplicity

UPDATE Credits for vowwl detectioon goes to this https://stackoverflow.com/questions/14268970/how-to-detect-and-echo-the-last-vowel-in-a-word

  • WOW ! It works perfectly. Thank you SO MUCH. Following the advises of the previous comments, I was trying to figure out how to preprocess the regex function, and I was far from it... – Tritof Sep 14 '18 at 15:25

A preprocess function is better suited for this task, and I would use the following code. I am using custom-type as machine name for the content type; replace it with the one you are using, and for which you want to change the title.

function mymodule_preprocess_node(&$variables) {
  if ($node = $variables['node'] && $node->bundle() == 'custom-type') {
    $title = $variables['label'];
    $translitted_title = iconv("UTF-8", "ASCII//TRANSLIT", $title);
    $article = (in_array($translitted_title[0], ['a', 'e', 'i', 'o', 'u']) ? "D'" : 'De ');
    $variables['label'] = $article . $title;

In the template file, using {{ label }} you will get the changed title.

If you want to still be able to use {{ label }} for the original title, I would use the following code.

function mymodule_preprocess_node(&$variables) {
  if ($node = $variables['node'] && $node->bundle() == 'custom-type') {
    $title = $variables['label'];
    $translitted_title = iconv("UTF-8", "ASCII//TRANSLIT", $title);
    $article = (in_array($translitted_title[0], ['a', 'e', 'i', 'o', 'u']) ? "D'" : 'De ');
    $variables['changed_label'] = $article . $title;

In the template, you can now use {{ changed_label }} for the new title, and {{ label }} for the original title.

Replace mymodule with the machine name of the module, or theme, where the code is being added.

As said in the documentation for the iconv extension, the extension is enabled by default. If the code I shown doesn't work, the extension has been disabled on the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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