How do I alter the title of a menu link? In my website, users can collect some 'points' over time. I have a top-bar menu with some links (e.g. "home", "profile", "logout"). I want to alter the menu item "profile" and add the number points owned by the current connected user. 

I tried several hook, and I almost succeeded in getting what I wanted with hook_link_alter(). 

function mycustommodule_link_alter(&amp;$variables) {
  if ($variables['text'] == "profile") {
    // Do some work.
    $variables['text'] = $variables['text'] . " (you have $nb_points points)";
  }
}


I cleared the cache (with drush cr), and the menu item shows the value I want. But, if the value changes for some reason, it shows the old value. I must clear the cache all the times to update its value. 

How can I avoid cleaning the cache to update the menu title all the times the points gained from the user change?
I suggest implementing a custom menu link plugin. The code below assumes your module name is example.

&lt;?php

namespace Drupal\example\Plugin\Menu;

use Drupal\Core\Database\Connection;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A menu link that displays number of points.
 */
class ExampleMenuLink extends MenuLinkDefault {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $dbConnection;

  /**
   * Constructs a new points menu link.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
   *   The static override storage.
   * @param \Drupal\Core\Database\Connection $db_connection
   *   The database connection.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, Connection $db_connection) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);
    $this-&gt;dbConnection = $db_connection;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container-&gt;get('menu_link.static.overrides'),
      $container-&gt;get('database')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    $count = $this-&gt;dbConnection-&gt;query('SELECT COUNT(*) FROM {example_points}')-&gt;fetchField();
    return $this-&gt;t('You have (@count) points', ['@count' =&gt; $count]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    // Invalidate these tags when number of points is changed.
    return ['example.points_count'];
  }

}


If you don't want to inject the database service the class would become much simpler.

&lt;?php

namespace Drupal\example\Plugin\Menu;

use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A menu link that displays number of points.
 */
class ExampleMenuLink extends MenuLinkDefault {

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    $count = \Drupal::database()-&gt;query('SELECT COUNT(*) FROM {example_points}')-&gt;fetchField();
    return $this-&gt;t('You have (@count) points', ['@count' =&gt; $count]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    // Invalidate these tags when number of points is changed.
    return ['example.points_count'];
  }

}


Next you need to put the link definition into example.links.menu.yml file.

example.user_points:
  route_name: &lt;front&gt;
  menu_name: main
  class: Drupal\example\Plugin\Menu\ExampleMenuLink
  weight: 30


The caching problem

Whenever the number of points is changed the menu link cache should be invalidated as follows. 

 \Drupal::service('cache_tags.invalidator')-&gt;invalidateTags(['example.points_count']);


You need to find out the right place for this. If the points managed by contributed module check the module API and pick up an appropriate hook (hook_points_insert(), hook_points_delete() and so on).

Since the number of points is calculated for each user account individually you may consider using per account cache tags (something like ['example.points_count.' . $uid]). Therefore the cache will be preserved  for users with unchanged points.



To generate code for the Menu link plugin I used Drupal Code Generator.