You are here

BadgeController.php in User Badges 8

File

src/Controller/BadgeController.php
View source
<?php

/**
 * @file
 * Contains \Drupal\user_badges\Controller\BadgeController.
 */
namespace Drupal\user_badges\Controller;

use Drupal\acquia_connector\Client;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\user_badges\BadgeInterface;
use Drupal\user_badges\BadgeTypeInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class BadgeController.
 *
 * @package Drupal\user_badges\Controller
 */
class BadgeController extends ControllerBase {

  /**
   * The Link Generator service.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $linkGenerator;

  /**
   * Constructs a BadgeController object.
   *
   * @param \Drupal\Core\Utility\LinkGeneratorInterface linkGenerator
   *   Link generator service.
   */
  public function __construct(LinkGeneratorInterface $linkGenerator) {
    $this->linkGenerator = $linkGenerator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('link_generator'));
  }

  /**
   * Addpage.
   *
   * @return []
   *   Return links for badge creation.
   */
  public function addPage() {
    $build = [
      '#theme' => 'item_list',
    ];
    $content = [];
    $types = [];

    // Only use badge types the user has access to.
    foreach ($this
      ->entityTypeManager()
      ->getStorage('badge_type')
      ->loadMultiple() as $type) {
      $access = $this
        ->entityTypeManager()
        ->getAccessControlHandler('badge')
        ->createAccess($type
        ->id(), NULL, [], TRUE);
      if ($access
        ->isAllowed()) {
        $content[] = $this->linkGenerator
          ->generate($type
          ->label(), new Url('user_badges.badge_controller_add', [
          'badge_type' => $type
            ->id(),
        ]));
        $types[] = $type;
      }
    }

    // Bypass the /admin/structure/badge/add listing if only one content type is available.
    if (count($types) == 1) {
      $type = array_shift($types);
      return $this
        ->redirect('user_badges.badge_controller_add', [
        'badge_type' => $type
          ->id(),
      ]);
    }
    $build['#items'] = $content;
    return $build;
  }

  /**
   * Add.
   *
   * @return array
   *   Returns badge creation form.
   */
  public function add(BadgeTypeInterface $badge_type) {
    $badge = $this
      ->entityTypeManager()
      ->getStorage('badge')
      ->create([
      'type' => $badge_type
        ->id(),
    ]);
    return $this
      ->entityFormBuilder()
      ->getForm($badge);
  }

  /**
   * The _title_callback for the user_badges.badge_controller_add route.
   *
   * @param \Drupal\user_badges\BadgeTypeInterface $badge_type
   *   The current node.
   *
   * @return string
   *   The page title.
   */
  public function addBadgeTitle(BadgeTypeInterface $badge_type) {
    return $this
      ->t('Create @name', [
      '@name' => $badge_type
        ->label(),
    ]);
  }

}

Classes

Namesort descending Description
BadgeController Class BadgeController.