WebformAccessGroupEntityBlock.php in Webform 8.5
File
modules/webform_access/src/Plugin/Block/WebformAccessGroupEntityBlock.php
View source
<?php
namespace Drupal\webform_access\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformAccessGroupEntityBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $currentUser;
protected $webformAccessGroupStorage;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
$this->webformAccessGroupStorage = $entity_type_manager
->getStorage('webform_access_group');
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('language_manager'));
}
public function build() {
$nodes = $this->webformAccessGroupStorage
->getUserEntities($this->currentUser, 'node');
if (empty($nodes)) {
return NULL;
}
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
$items = [];
foreach ($nodes as $node) {
if ($node
->access()) {
if ($node
->hasTranslation($langcode)) {
$node = $node
->getTranslation($langcode);
}
$items[] = $node
->toLink()
->toRenderable();
}
}
if (empty($items)) {
return NULL;
}
return [
'#theme' => 'item_list',
'#items' => $items,
];
}
public function getCacheMaxAge() {
return 0;
}
}