UserRegisterBlock.php in Glazed Theme Helper 8
File
src/Plugin/Block/UserRegisterBlock.php
View source
<?php
namespace Drupal\glazed_helper\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
class UserRegisterBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityFormBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityFormBuilderInterface $entityFormBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
$this->entityFormBuilder = $entityFormBuilder;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity.form_builder'));
}
public function build() {
$build = [];
$account = $this->entityTypeManager
->getStorage('user')
->create([]);
$build['form'] = $this->entityFormBuilder
->getForm($account, 'register');
return $build;
}
public function blockAccess(AccountInterface $account) {
return AccessResult::allowedIf($account
->isAnonymous() && \Drupal::config('user.settings')
->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY)
->addCacheContexts([
'user.roles',
])
->addCacheTags(\Drupal::config('user.settings')
->getCacheTags());
}
}