refactored user blocks using ajax loaded drupal's html as template for vue
This commit is contained in:
5
web/modules/custom/materio_user/materio_user.info.yml
Normal file
5
web/modules/custom/materio_user/materio_user.info.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
name: 'materio_user'
|
||||
type: module
|
||||
description: ''
|
||||
core: 8.x
|
||||
package: 'Materio'
|
57
web/modules/custom/materio_user/materio_user.module
Normal file
57
web/modules/custom/materio_user/materio_user.module
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains materio_user.module.
|
||||
*/
|
||||
|
||||
use \Drupal\Core\Form\FormStateInterface;
|
||||
use \Drupal\Core\Block\BlockPluginInterface;
|
||||
|
||||
/**
|
||||
* implements hook_form_FORM_ID_alter()
|
||||
*
|
||||
*/
|
||||
function materio_user_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
|
||||
// Drupal::logger('materio_user')->notice(print_r($form, true));
|
||||
$form['name']['#attributes'] += array(
|
||||
"v-model" => "mail",
|
||||
"@keyup.enter" => "login"
|
||||
);
|
||||
|
||||
$form['pass']['#attributes'] = array(
|
||||
"v-model" => "password",
|
||||
"@keyup.enter" => "login"
|
||||
);
|
||||
|
||||
$form['actions']['submit']['#attributes'] = array(
|
||||
"@click.prevent" => "login"
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* implements hook_block_view_BASE_BLOCK_ID_alter()
|
||||
*
|
||||
* https://www.drupal.org/project/drupal/issues/2626224
|
||||
*/
|
||||
function materio_user_block_view_user_login_block_alter(array &$build, BlockPluginInterface $block) {
|
||||
$build['#pre_render'][] = '_materio_user_user_login_block_pre_render';
|
||||
}
|
||||
|
||||
function _materio_user_user_login_block_pre_render(array $build){
|
||||
$user_links = &$build['content']['user_links'];
|
||||
$items = &$user_links['#items'];
|
||||
// ksm($items);
|
||||
$items['create_account']['#url']->mergeOptions(array(
|
||||
"attributes" => array(
|
||||
"@click.prevent" => "create_account"
|
||||
)
|
||||
));
|
||||
$items['request_password']['#url']->mergeOptions(array(
|
||||
'attributes' => array(
|
||||
"@click.prevent" => "request_password"
|
||||
)
|
||||
));
|
||||
return $build;
|
||||
}
|
15
web/modules/custom/materio_user/materio_user.routing.yml
Normal file
15
web/modules/custom/materio_user/materio_user.routing.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
materio_user.login_form:
|
||||
path: '/materio_user/login_form'
|
||||
defaults:
|
||||
_controller: '\Drupal\materio_user\Controller\AjaxLoginForm::getForm'
|
||||
_format: json
|
||||
requirements:
|
||||
_access: 'TRUE'
|
||||
|
||||
materio_user.login_block:
|
||||
path: '/materio_user/login_block'
|
||||
defaults:
|
||||
_controller: '\Drupal\materio_user\Controller\AjaxLoginBlock::getBlock'
|
||||
_format: json
|
||||
requirements:
|
||||
_access: 'TRUE'
|
@@ -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