refactored user blocks using ajax loaded drupal's html as template for vue
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\materio_user\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Drupal\block\Entity\Block;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
// use Drupal\Core\Cache\CacheableJsonResponse;
|
||||
// use Drupal\Core\Cache\CacheableMetadata;
|
||||
// use Drupal\core\render\RenderContext;
|
||||
|
||||
|
||||
/**
|
||||
* Defines a route controller.
|
||||
*/
|
||||
class AjaxLoginBlock extends ControllerBase {
|
||||
|
||||
private function getBlockDefinition(){
|
||||
// $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
// \Drupal::logger('materio_user')->notice($language);
|
||||
$this->bid = "userlogin";
|
||||
$this->block = Block::load($this->bid);
|
||||
$this->block_builded = \Drupal::entityManager()->getViewBuilder('block')->view($this->block);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for getBlock request.
|
||||
*/
|
||||
public function getBlock(Request $request) {
|
||||
|
||||
$this->getBlockDefinition();
|
||||
|
||||
$rendered = \Drupal::service('renderer')->renderRoot($this->block_builded);
|
||||
$data = [
|
||||
'rendered' => $rendered,
|
||||
// '#cache' => [
|
||||
// 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
|
||||
// 'tags' => [
|
||||
// 'materio_sapi-search_form-cache',
|
||||
// ]
|
||||
// ]
|
||||
];
|
||||
|
||||
$response = new JsonResponse();
|
||||
$response->setData($data);
|
||||
// $response = new CacheableJsonResponse($data);
|
||||
// $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
// https://www.qed42.com/blog/autocomplete-drupal-8
|
||||
// https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code
|
||||
|
||||
namespace Drupal\materio_user\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Form\FormBuilder;
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
// use Drupal\Core\Cache\CacheableJsonResponse;
|
||||
// use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\core\render\RenderContext;
|
||||
|
||||
|
||||
/**
|
||||
* Defines a route controller for entity autocomplete form elements.
|
||||
*/
|
||||
class AjaxLoginForm extends ControllerBase {
|
||||
|
||||
/**
|
||||
* The form builder.
|
||||
*
|
||||
* @var \Drupal\Core\Form\FormBuilder
|
||||
*/
|
||||
protected $formBuilder;
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*/
|
||||
public function __construct(FormBuilder $formBuilder) {
|
||||
$this->formBuilder = $formBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('form_builder')
|
||||
);
|
||||
}
|
||||
|
||||
private function getFormDefinition(){
|
||||
// $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
// \Drupal::logger('materio_user')->notice($language);
|
||||
$this->form_builded = $this->formBuilder->getForm('Drupal\user\Form\UserLoginForm');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for getform request.
|
||||
*/
|
||||
public function getForm(Request $request) {
|
||||
|
||||
$this->getFormDefinition();
|
||||
|
||||
$rendered = render($this->form_builded);
|
||||
// $form_builded = $this->form_builded;
|
||||
// $rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($form_builded) {
|
||||
// return render($form_builded);
|
||||
// });
|
||||
$data = [
|
||||
'rendered' => $rendered,
|
||||
// '#cache' => [
|
||||
// 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
|
||||
// 'tags' => [
|
||||
// 'materio_sapi-search_form-cache',
|
||||
// ]
|
||||
// ]
|
||||
];
|
||||
|
||||
$response = new JsonResponse();
|
||||
$response->setData($data);
|
||||
// $response = new CacheableJsonResponse($data);
|
||||
// $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\materio_user\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Session\AccountProxy;
|
||||
use Drupal\Core\Session\AccountProxyInterface;
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Provides a 'UserBlock' block.
|
||||
*
|
||||
* @Block(
|
||||
* id = "user_block",
|
||||
* admin_label = @Translation("User block"),
|
||||
* )
|
||||
*/
|
||||
class UserBlock extends BlockBase implements ContainerFactoryPluginInterface{
|
||||
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
// Instantiates this form class.
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('current_user')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $configuration
|
||||
* @param string $plugin_id
|
||||
* @param mixed $plugin_definition
|
||||
* @param \Drupal\Core\Session\AccountProxyInterface $account
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->user = $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
$build = [];
|
||||
// dpm($this->user);
|
||||
if($this->user->id()){
|
||||
$user_url = Url::fromRoute('entity.user.canonical', ['user' => $this->user->id()]);
|
||||
$build['user-link'] = array(
|
||||
'#title' => $this->user->getEmail(),
|
||||
'#type' => 'link',
|
||||
'#url' => $user_url,
|
||||
'#options'=>array(
|
||||
'attributes' => array(
|
||||
'data-drupal-link-system-path' => $user_url->getInternalPath(),
|
||||
'alt' => t('User account'),
|
||||
)
|
||||
)
|
||||
);
|
||||
$logout_url = Url::fromRoute('user.logout');
|
||||
$build['user-logout'] = array(
|
||||
'#title' => t('Logout'),
|
||||
'#type' => 'link',
|
||||
'#url' => $logout_url,
|
||||
'#options'=>array(
|
||||
'attributes' => array(
|
||||
'data-drupal-link-system-path' => $logout_url->getInternalPath(),
|
||||
'alt' => t('Logout'),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user