You are here

class DefaultTwigExtension in Opigno training catalog 8

Same name and namespace in other branches
  1. 3.x src/TwigExtension/DefaultTwigExtension.php \Drupal\opigno_catalog\TwigExtension\DefaultTwigExtension

Class DefaultTwigExtension.

Hierarchy

  • class \Drupal\opigno_catalog\TwigExtension\DefaultTwigExtension extends \Drupal\opigno_catalog\TwigExtension\Twig_Extension

Expanded class hierarchy of DefaultTwigExtension

1 string reference to 'DefaultTwigExtension'
opigno_catalog.services.yml in ./opigno_catalog.services.yml
opigno_catalog.services.yml
1 service uses DefaultTwigExtension
opigno_catalog.twig.extension in ./opigno_catalog.services.yml
Drupal\opigno_catalog\TwigExtension\DefaultTwigExtension

File

src/TwigExtension/DefaultTwigExtension.php, line 11

Namespace

Drupal\opigno_catalog\TwigExtension
View source
class DefaultTwigExtension extends \Twig_Extension {

  /**
   * {@inheritdoc}
   */
  public function getFunctions() {
    return [
      new \Twig_SimpleFunction('opigno_catalog_get_style', [
        $this,
        'get_row_style',
      ]),
      new \Twig_SimpleFunction('opigno_catalog_is_member', [
        $this,
        'is_member',
      ]),
      new \Twig_SimpleFunction('opigno_catalog_is_started', [
        $this,
        'is_started',
      ]),
      new \Twig_SimpleFunction('opigno_catalog_get_default_image', [
        $this,
        'get_default_image',
      ]),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'opigno_catalog.twig.extension';
  }

  /**
   * Gets row style.
   */
  public function get_row_style() {
    $style = \Drupal::service('opigno_catalog.get_style')
      ->getStyle();
    return $style == 'line' ? 'style-line' : 'style-block';
  }

  /**
   * Checks if user is a member of group.
   */
  public function is_member($group_id) {
    $group = Group::load($group_id);
    $account = \Drupal::currentUser();
    return (bool) $group
      ->getMember($account);
  }

  /**
   * Checks if training started.
   */
  public function is_started($group_id) {
    $group = Group::load($group_id);
    $account = \Drupal::currentUser();
    return (bool) opigno_learning_path_started($group, $account);
  }

  /**
   * Returns default image.
   */
  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':
        $img = '<img src="' . $request
          ->getBasePath() . '/' . $path . '/img/img_training.png" alt="' . $title . '">';
        break;
      default:
        $img = NULL;
        break;
    }
    return Markup::create($img);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultTwigExtension::getFunctions public function
DefaultTwigExtension::getName public function
DefaultTwigExtension::get_default_image public function Returns default image.
DefaultTwigExtension::get_row_style public function Gets row style.
DefaultTwigExtension::is_member public function Checks if user is a member of group.
DefaultTwigExtension::is_started public function Checks if training started.