Skip to content
Snippets Groups Projects
Commit e9b2839a authored by Daniel Cothran's avatar Daniel Cothran
Browse files

Issue #3460720: Module enabled but new blocks might need better labelling

parent 8b990561
No related branches found
No related tags found
1 merge request!2Update 2 files
Pipeline #246327 passed with warnings
......@@ -2,17 +2,18 @@
namespace Drupal\vallb\Plugin\Block;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Plugin\Block\ViewsBlock;
/**
* Provides a views lazy built block.
*
* @Block(
* id = "vallb_block",
* admin_label = @Translation("Views lazy built block"),
* deriver = "Drupal\views\Plugin\Derivative\ViewsBlock"
* )
*/
#[Block(
id: 'vallb_block',
admin_label: new TranslatableMarkup('Views Ajax Lazy Load Blocks'),
deriver: 'Drupal\vallb\Plugin\Derivative\ViewsLazyBuiltBlock'
)]
class ViewsLazyBuiltBlock extends ViewsBlock {
/**
......
<?php
namespace Drupal\vallb\Plugin\Derivative;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Plugin\Derivative\ViewsBlock;
/**
* Provides lazy load block plugin definitions for all Views block displays.
*
* @see \Drupal\views\Plugin\Block\ViewsBlock
*/
class ViewsLazyBuiltBlock extends ViewsBlock {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$derivatives = parent::getDerivativeDefinitions($base_plugin_definition);
foreach ($derivatives as &$derivative) {
// Change the category for blocks using this module.
$derivative['category'] .= ' ' . new TranslatableMarkup('[Lazy Loaded]');
}
return $derivatives;
}
}
......@@ -8,19 +8,31 @@ use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\views\Element\View;
use Drupal\views\Entity\View as ViewConfigEntity;
use Drupal\views\ViewEntityInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\ViewExecutableFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The Views Ajax Lazy Load Blocks renderer.
*/
final class Renderer extends ControllerBase implements TrustedCallbackInterface {
protected EntityStorageInterface $viewStorage;
/**
* The view storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $viewStorage;
/**
* The view executable.
*
* @var \Drupal\views\ViewExecutable
*/
protected $view;
/**
......@@ -36,7 +48,7 @@ final class Renderer extends ControllerBase implements TrustedCallbackInterface
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
protected ViewExecutableFactory $viewExecutableFactory
protected ViewExecutableFactory $viewExecutableFactory,
) {
$this->viewStorage = $entity_type_manager->getStorage('view');
}
......@@ -61,17 +73,19 @@ final class Renderer extends ControllerBase implements TrustedCallbackInterface
/**
* Outputs the render array of a view block.
*
* @param $view
* @param \Drupal\views\ViewEntityInterface $view
* The view id.
* @param string $display_id
* The display id.
* @param string $nojs
* The nojs parameter.
*
* @return array|\Drupal\Core\Ajax\AjaxResponse The render array of the view.
* @return array|\Drupal\Core\Ajax\AjaxResponse
* The render array of the view.
*
* @throws \Drupal\views\Exception\ViewRenderElementException
*/
public function output($view, string $display_id, string $nojs = 'nojs'): array|AjaxResponse {
public function output(ViewEntityInterface $view, string $display_id, string $nojs = 'nojs'): array|AjaxResponse {
$query = \Drupal::requestStack()->getCurrentRequest()->query->all();
$views_arguments = [];
......@@ -115,7 +129,7 @@ final class Renderer extends ControllerBase implements TrustedCallbackInterface
/**
* Checks the access control of the view.
*
* @param string|\Drupal\views\Entity\View $view
* @param \Drupal\views\ViewEntityInterface $view
* The view id or the view config entity.
* @param string $display_id
* The view display id.
......@@ -123,7 +137,7 @@ final class Renderer extends ControllerBase implements TrustedCallbackInterface
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function checkOutputAccess(string|ViewConfigEntity $view, string $display_id): AccessResultInterface {
public function checkOutputAccess(ViewEntityInterface $view, string $display_id): AccessResultInterface {
$view = $this->viewExecutable($view);
return $view->access($display_id) ? AccessResult::allowed() : AccessResult::forbidden();
}
......@@ -168,16 +182,15 @@ final class Renderer extends ControllerBase implements TrustedCallbackInterface
/**
* Initialize the view executable property.
*
* @param $view_id
* @param \Drupal\views\ViewEntityInterface $view
* The view id.
*
* @return \Drupal\views\ViewExecutable
* The executable.
*/
protected function viewExecutable($view_id): ViewExecutable {
protected function viewExecutable(ViewEntityInterface $view): ViewExecutable {
if (!$this->view) {
$view_config = is_string($view_id) ? $this->viewStorage->load($view_id) : $view_id;
$this->view = $this->viewExecutableFactory->get($view_config);
$this->view = $this->viewExecutableFactory->get($view);
}
return $this->view;
}
......
......@@ -21,5 +21,6 @@ function vallb_theme($existing, $type, $theme, $path): array {
'loading_message' => t('Loading...'),
],
],
'file' => 'vallb.theme.inc',
];
}
......@@ -5,3 +5,7 @@ vallb.lazy_builder:
_title: 'Views lazy builder ajax callback'
requirements:
_custom_access: vallb.renderer:checkOutputAccess
options:
parameters:
view:
type: entity:view
......@@ -9,6 +9,9 @@ use Drupal\Component\Utility\Crypt;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
/**
* Implements hook_preprocess_HOOK().
*/
function template_preprocess_vallb(array &$variables) {
$variables['endpoint'] = Url::fromRoute($variables['route_name'], $variables['route_parameters'], $variables['route_options']);
if (!empty($variables['route_options']['query']['vallb_views_arguments'])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment