View source
<?php
namespace Drupal\varbase_total_control\Plugin\Block;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class VarbaseDashboardUser extends BlockBase implements BlockPluginInterface, ContainerFactoryPluginInterface {
protected $stringTranslation;
protected $entityTypeManager;
protected $redirectDestination;
protected $currentUser;
protected $userStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $string_translation, RedirectDestinationInterface $redirect_destination, AccountInterface $current_user, EntityStorageInterface $user_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->stringTranslation = $string_translation;
$this->redirectDestination = $redirect_destination;
$this->currentUser = $current_user;
$this->userStorage = $user_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('string_translation'), $container
->get('redirect.destination'), $container
->get('current_user'), $container
->get('entity.manager')
->getStorage('user'));
}
public function build() {
$user = $this->userStorage
->load($this->currentUser
->id());
$destination = $this->redirectDestination
->getAsArray();
$options = [
$destination,
];
$name = Link::fromTextAndUrl($user
->getDisplayName(), new Url('entity.user.edit_form', [
'user' => $user
->id(),
], $options))
->toString();
$url = new Url('entity.user.edit_form', [
'user' => $user
->id(),
$options,
]);
$welcom_back_text = $this
->t('Welcome back');
$markup_data = '<div class="content"> <div class="welcome"><p class="welcome-back">' . $welcom_back_text . '</p><p class="name"> ' . $name . ' </p></div><div class="action-links"><a class="button button-action button--primary button--small" href="' . $url
->toString() . '">' . $this
->t('Edit Account') . '</a></div></div>';
return [
'#type' => 'markup',
'#markup' => $markup_data,
];
}
}