Account.php in Dashboards with Layout Builder 8
File
src/Plugin/Dashboard/Account.php
View source
<?php
namespace Drupal\dashboards\Plugin\Dashboard;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\dashboards\Plugin\DashboardBase;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Account extends DashboardBase {
protected $account;
protected $entityDisplayRepository;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache, AccountInterface $account, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $cache);
$this->account = $account;
$this->entityDisplayRepository = $entity_display_repository;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('dashboards.cache'), $container
->get('current_user'), $container
->get('entity_display.repository'), $container
->get('entity_type.manager'));
}
public function buildRenderArray($configuration) : array {
$user = User::load($this->account
->id());
return $this->entityTypeManager
->getViewBuilder($user
->getEntityTypeId())
->view($user, $configuration['view_mode']);
}
public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration) : array {
$form['view_mode'] = [
'#type' => 'select',
'#options' => $this->entityDisplayRepository
->getViewModeOptions('user'),
'#default_value' => isset($configuration['view_mode']) ? $configuration['view_mode'] : FALSE,
];
return $form;
}
}