View source
<?php
namespace Drupal\opigno_dashboard\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AboutOpignoBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $account;
public function __construct(AccountInterface $account, ...$default) {
parent::__construct(...$default);
$this->account = $account;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('current_user'), $configuration, $plugin_id, $plugin_definition);
}
public function defaultConfiguration() : array {
return [
'label_display' => FALSE,
];
}
public function build() {
if ($this->account
->isAnonymous()) {
return [
'#cache' => [
'max-age' => -1,
],
];
}
$build = [
'#cache' => [
'tags' => $this
->getCacheTags(),
'contexts' => $this
->getCacheContexts(),
],
];
if (!$this->account
->hasPermission('access administration pages')) {
return $build;
}
$logo = theme_get_setting('logo_path_anonymous');
if (!is_file($logo)) {
$logo = drupal_get_path('theme', 'aristotle') . '/assets/' . $logo;
}
$options = [
'attributes' => [
'target' => '_blank',
],
];
$url = Url::fromUri('https://www.opigno.org', $options);
return [
'#theme' => 'opigno_about_block',
'#logo' => file_exists($logo) ? file_url_transform_relative(file_create_url($logo)) : '',
'#texts' => [
$this
->t('Opigno™ is a Trademark of Connect-i Sàrl, based in Préverenges, Switzerland.'),
$this
->t('For more information regarding Opigno™ please consult the website @link.', [
'@link' => Link::fromTextAndUrl('www.opigno.org', $url)
->toString(),
]),
],
'#version' => opigno_lms_get_current_opigno_lms_release() ?: '3.0',
] + $build;
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'user',
]);
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), [
'user' . (int) $this->account
->id(),
]);
}
}