我建议实现自定义菜单链接插件。下面的代码假定您的模块名称为example。
<?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->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->get('menu_link.static.overrides'),
$container->get('database')
);
}
/**
* {@inheritdoc}
*/
public function getTitle() {
$count = $this->dbConnection->query('SELECT COUNT(*) FROM {example_points}')->fetchField();
return $this->t('You have (@count) points', ['@count' => $count]);
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
// Invalidate these tags when number of points is changed.
return ['example.points_count'];
}
}
如果您不想注入数据库服务,则该类将变得更加简单。
<?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()->query('SELECT COUNT(*) FROM {example_points}')->fetchField();
return $this->t('You have (@count) points', ['@count' => $count]);
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
// Invalidate these tags when number of points is changed.
return ['example.points_count'];
}
}
接下来,您需要将链接定义放入example.links.menu.yml文件。
example.user_points:
route_name: <front>
menu_name: main
class: Drupal\example\Plugin\Menu\ExampleMenuLink
weight: 30
缓存问题
无论何时更改点数,菜单链接缓存都应按以下方式失效。
\Drupal::service('cache_tags.invalidator')->invalidateTags(['example.points_count']);
您需要为此找到合适的位置。如果由贡献模块管理的点检查模块API并选择适当的钩子(hook_points_insert(),hook_points_delete()等)。
由于积分是针对每个用户帐户分别计算的,因此您可以考虑使用每个帐户缓存标签(例如['example.points_count.' . $uid]
)。因此,将为点数不变的用户保留缓存。
为了生成Menu链接插件的代码,我使用了Drupal Code Generator。