3 namespace Drupal\{{ machine_name }}\Plugin\Menu;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Menu\MenuLinkDefault;
7 use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * A menu link that displays count of messages.
13 class {{ class }} extends MenuLinkDefault {
16 * The database connection.
18 * @var \Drupal\Core\Database\Connection
20 protected $dbConnection;
23 * Constructs the plugin object.
25 * @param array $configuration
26 * A configuration array containing information about the plugin instance.
27 * @param string $plugin_id
28 * The plugin_id for the plugin instance.
29 * @param mixed $plugin_definition
30 * The plugin implementation definition.
31 * @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
32 * The static override storage.
33 * @param \Drupal\Core\Database\Connection $db_connection
34 * The database connection.
36 public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, Connection $db_connection) {
37 parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);
38 $this->dbConnection = $db_connection;
44 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49 $container->get('menu_link.static.overrides'),
50 $container->get('database')
57 public function getTitle() {
58 $count = $this->dbConnection->query('SELECT COUNT(*) FROM {messages}')->fetchField();
59 return $this->t('Messages (@count)', ['@count' => $count]);
65 public function getRouteName() {
66 return '{{ machine_name }}.messages';
72 public function getCacheTags() {
73 // @DCG Invalidate this tags when messages are created or removed.
74 return ['{{ machine_name }}.messages_count'];