mutual_credit-5.0.x-dev/src/Controller/WalletController.php
src/Controller/WalletController.php
<?php namespace Drupal\mcapi\Controller; use Drupal\mcapi\Entity\WalletInterface; use Drupal\mcapi\Entity\Wallet; use Drupal\mcapi\Entity\Storage\WalletStorage; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Returns all the wallets held by the given entity, in summary view mode. */ class WalletController extends ControllerBase { /** * @var ContentEntityType */ private $holderEntity; /** * @param RouteMatchInterface $route_match */ public function __construct(RouteMatchInterface $route_match) { $params = $route_match->getParameters()->all(); $this->holderEntity = $params['mcapi_wallet'] instanceof WalletInterface ? $params['mcapi_wallet'] : Wallet::load($params['mcapi_wallet']); } /** * @param ContainerInterface $container * @return \static */ public static function create(ContainerInterface $container) { return new static( $container->get('current_route_match') ); } /** * Get the name of the wallet(s) holder. * * @return TranslatableMarkup * The entity label of the wallet holder. */ public function entityWalletsTitle() : TranslatableMarkup { return $this->t('Wallet: @name', ['@name' => $this->holderEntity->label()]); } /** * Router callback. * * Show all an entities wallets in summary mode. * this is rather tricky because we don't know what arguments would be passed * from the url, so we have to load them from scratch. * * @return array * A renderable array. */ public function entityWallets() { $wallets = WalletStorage::walletsOf($this->holderEntity, TRUE); return $this->entityTypeManager() ->getViewBuilder('mcapi_wallet') ->viewMultiple($wallets); } }