DefaultTwigExtension.php in Opigno training catalog 3.x
File
src/TwigExtension/DefaultTwigExtension.php
View source
<?php
namespace Drupal\opigno_catalog\TwigExtension;
use Drupal\Core\Render\Markup;
use Drupal\group\Entity\Group;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class DefaultTwigExtension extends AbstractExtension {
public function getFunctions() {
return [
new TwigFunction('opigno_catalog_get_style', [
$this,
'get_row_style',
]),
new TwigFunction('opigno_catalog_is_member', [
$this,
'is_member',
]),
new TwigFunction('opigno_catalog_is_started', [
$this,
'is_started',
]),
new TwigFunction('opigno_catalog_get_default_image', [
$this,
'get_default_image',
]),
];
}
public function getName() {
return 'opigno_catalog.twig.extension';
}
public function get_row_style() {
$style = \Drupal::service('opigno_catalog.get_style')
->getStyle();
return $style == 'line' ? 'style-line' : 'style-block';
}
public function is_member($group_id) {
$group = Group::load($group_id);
$account = \Drupal::currentUser();
return (bool) $group
->getMember($account);
}
public function is_started($group_id) {
$group = Group::load($group_id);
$account = \Drupal::currentUser();
return (bool) opigno_learning_path_started($group, $account);
}
public function get_default_image($type, $title) {
$request = \Drupal::request();
$path = \Drupal::service('module_handler')
->getModule('opigno_catalog')
->getPath();
$title = t('Picture of') . ' ' . $title;
switch ($type) {
case 'course':
$img = '<img src="' . $request
->getBasePath() . '/' . $path . '/img/img_course.png" alt="' . $title . '">';
break;
case 'module':
$img = '<img src="' . $request
->getBasePath() . '/' . $path . '/img/img_module.png" alt="' . $title . '">';
break;
case 'learning_path':
$theme_path = drupal_get_path('theme', 'aristotle');
$img = '<img src="' . $request
->getBasePath() . '/' . $theme_path . '/src/images/content/training.svg" alt="' . $title . '">';
break;
case 'certificate_image':
$theme_path = drupal_get_path('theme', 'aristotle');
$img = '<img src="' . $request
->getBasePath() . '/' . $theme_path . '/src/images/design/certificate.svg" alt="' . $title . '">';
break;
default:
$img = NULL;
break;
}
return Markup::create($img);
}
}