Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d8 / plugin / menu-link.twig
1 <?php
2
3 namespace Drupal\{{ machine_name }}\Plugin\Menu;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Menu\MenuLinkDefault;
7 use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * A menu link that displays count of messages.
12  */
13 class {{ class }} extends MenuLinkDefault {
14
15   /**
16    * The database connection.
17    *
18    * @var \Drupal\Core\Database\Connection
19    */
20   protected $dbConnection;
21
22   /**
23    * Constructs the plugin object.
24    *
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.
35    */
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;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
45     return new static(
46       $configuration,
47       $plugin_id,
48       $plugin_definition,
49       $container->get('menu_link.static.overrides'),
50       $container->get('database')
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getTitle() {
58     $count = $this->dbConnection->query('SELECT COUNT(*) FROM {messages}')->fetchField();
59     return $this->t('Messages (@count)', ['@count' => $count]);
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getRouteName() {
66     return '{{ machine_name }}.messages';
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getCacheTags() {
73     // @DCG Invalidate this tags when messages are created or removed.
74     return ['{{ machine_name }}.messages_count'];
75   }
76
77 }