Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

berliner/HPC 9932 #1230

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ form.ghi-glb-form.glb-canvas-form .glb-canvas-form__actions input[disabled],
}

#layout-builder-modal .confirmation {
padding-top: 2rem;
min-height: auto !important;
margin-left: 0;
margin-right: 0;
}
Expand Down
9 changes: 8 additions & 1 deletion html/modules/custom/ghi_blocks/ghi_blocks.module
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\ghi_blocks\Controller\BaseObjectReferenceController;
use Drupal\ghi_blocks\Controller\LayoutBuilderBlockController;
Expand Down Expand Up @@ -374,13 +375,18 @@ function ghi_blocks_link_alter(&$variables) {

$route_name = $url->getRouteName();

// @todo This could also be done via the route definition, at least for our
// custom ones.
$allowed_routes = [
'layout_builder.choose_block',
'layout_builder.remove_block',
'ghi_blocks.import_block',
'ghi_blocks.show_block_config',
'ghi_blocks.hide_block',
'ghi_blocks.unhide_block',
'ghi_blocks.hide_entity_block',
'ghi_blocks.unhide_entity_block',
'ghi_blocks.remove_entity_block',
];
if (!in_array($route_name, $allowed_routes)) {
// All other routes are already handled by the Layout Buidler Modal module.
Expand Down Expand Up @@ -434,7 +440,8 @@ function ghi_blocks_form_alter(array &$form, FormStateInterface $form_state, $fo

if ($form['#theme'] == 'confirm_form') {
$form['#after_build'][] = 'ghi_blocks_confirm_form_after_build';

$form['#prefix'] = Markup::create('<div class="gin-confirm-form">');
$form['#suffix'] = Markup::create('</div>');
}
}

Expand Down
9 changes: 6 additions & 3 deletions html/modules/custom/ghi_blocks/ghi_blocks.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ ghi_blocks.unhide_block:
ghi_blocks.hide_entity_block:
path: '/layout_builder/hide-entity-block/{entity_type}/{entity}/{uuid}'
defaults:
_controller: 'Drupal\ghi_blocks\Controller\LayoutBuilderBlockController::hideEntityBlock'
_form: '\Drupal\ghi_blocks\Form\PageElementsConfirmForm'
action: 'hide'
requirements:
_entity_access: 'entity.update'
options:
Expand All @@ -119,7 +120,8 @@ ghi_blocks.hide_entity_block:
ghi_blocks.unhide_entity_block:
path: '/layout_builder/unhide-entity-block/{entity_type}/{entity}/{uuid}'
defaults:
_controller: 'Drupal\ghi_blocks\Controller\LayoutBuilderBlockController::unhideEntityBlock'
_form: '\Drupal\ghi_blocks\Form\PageElementsConfirmForm'
action: 'unhide'
requirements:
_entity_access: 'entity.update'
options:
Expand All @@ -130,7 +132,8 @@ ghi_blocks.unhide_entity_block:
ghi_blocks.remove_entity_block:
path: '/layout_builder/remove-entity-block/{entity_type}/{entity}/{uuid}'
defaults:
_controller: 'Drupal\ghi_blocks\Controller\LayoutBuilderBlockController::removeEntityBlock'
_form: '\Drupal\ghi_blocks\Form\PageElementsConfirmForm'
action: 'remove'
requirements:
_entity_access: 'entity.update'
options:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\ghi_blocks\Plugin\Block\GHIBlockBase;
use Drupal\ghi_blocks\Traits\PageElementsTrait;
use Drupal\layout_builder\Controller\MoveBlockController;
use Drupal\layout_builder\LayoutEntityHelperTrait;
use Drupal\layout_builder\SectionStorageInterface;
Expand All @@ -28,7 +25,6 @@
class LayoutBuilderBlockController extends ControllerBase implements ContainerInjectionInterface {

use LayoutEntityHelperTrait;
use PageElementsTrait;

/**
* The container.
Expand Down Expand Up @@ -198,77 +194,4 @@ public function moveBlock(SectionStorageInterface $section_storage, int $delta_f
return $response;
}

/**
* Hide a block on an entity.
*
* This is done immediately without confirmation or passing by the tempstore.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to update.
* @param string $uuid
* The uuid of the component that should be hidden.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An ajax response object.
*/
public function hideEntityBlock(EntityInterface $entity, $uuid) {
$this->actionComponentOnEntity('hide', $entity, [$uuid]);
return $this->redirectEntityRouteResponse($entity, 'ghi_blocks.node.page_elements');
}

/**
* Unhide a block on an entity.
*
* This is done immediately without confirmation or passing by the tempstore.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to update.
* @param string $uuid
* The uuid of the component that should be hidden.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An ajax response object.
*/
public function unhideEntityBlock(EntityInterface $entity, $uuid) {
$this->actionComponentOnEntity('unhide', $entity, [$uuid]);
return $this->redirectEntityRouteResponse($entity, 'ghi_blocks.node.page_elements');
}

/**
* Remove a block from an entity.
*
* This is done immediately without confirmation or passing by the tempstore.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to update.
* @param string $uuid
* The uuid of the component that should be hidden.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An ajax response object.
*/
public function removeEntityBlock(EntityInterface $entity, $uuid) {
$this->actionComponentOnEntity('remove', $entity, [$uuid]);
return $this->redirectEntityRouteResponse($entity, 'ghi_blocks.node.page_elements');
}

/**
* Create a redirect to the current page.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to redirect to.
* @param string $route_name
* The route name.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An ajax response object.
*/
private function redirectEntityRouteResponse(EntityInterface $entity, $route_name) {
$response = new AjaxResponse();
$response->addCommand(new RedirectCommand(Url::fromRoute($route_name, [
$entity->getEntityTypeId() => $entity->id(),
])->toString()));
return $response;
}

}
173 changes: 173 additions & 0 deletions html/modules/custom/ghi_blocks/src/Form/PageElementsConfirmForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace Drupal\ghi_blocks\Form;

use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\ghi_blocks\Traits\GinLbModalTrait;
use Drupal\ghi_blocks\Traits\PageElementsTrait;
use Drupal\gin_lb\HookHandler\FormAlter;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a form to actions on the page elements listing.
*/
class PageElementsConfirmForm extends ConfirmFormBase {

use GinLbModalTrait;
use AjaxFormHelperTrait;
use PageElementsTrait;

/**
* The entity object.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;

/**
* The uuid of the page element to act on.
*
* @var string
*/
protected $uuid;

/**
* The action to execute.
*
* @var string
*/
protected $action;

/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->moduleHandler = $container->get('module_handler');
return $instance;
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ghi_blocks_page_elements_action_confirm';
}

/**
* {@inheritdoc}
*/
public function getDescription() {
if ($this->action == 'remove') {
return $this->t('This will permanently remove the page element from this page. This cannot be undone.');
}
return $this->t('This will @action the page element on this page without removing it permanently.', [
'@action' => $this->action,
]);
}

/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to @action the page element?', [
'@action' => $this->action,
]);
}

/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('@action the page element', [
'@action' => ucfirst($this->action),
]);
}

/**
* {@inheritdoc}
*/
public function processAction() {
$this->actionComponentOnEntity($this->action, $this->entity, [$this->uuid]);
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ?EntityInterface $entity = NULL, $uuid = NULL, $action = NULL) {
$this->entity = $entity;
$this->uuid = $uuid;
$this->action = $action;

$form = parent::buildForm($form, $form_state);

if ($this->isAjax()) {
$form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
// We overwrite the class on purpose so that this is not a button anymore.
$form['actions']['cancel']['#attributes']['class'] = ['dialog-cancel'];

// The AJAX system automatically moves focus to the first tabbable
// element after closing a dialog, sometimes scrolling to a page top.
// Disable refocus on the button.
$form['actions']['submit']['#ajax']['disable-refocus'] = TRUE;
}

// Mark this as an administrative page for JavaScript ("Back to site" link).
$form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;

if ($this->moduleHandler->moduleExists('gin_lb')) {
$form['#after_build'][] = [FormAlter::class, 'afterBuildAttachGinLbForm'];
$form['#gin_lb_form'] = TRUE;
$form['#attributes']['class'][] = 'glb-form';
}

$form['#attached']['library'][] = 'system/admin';
$form['#attached']['library'][] = 'ghi_blocks/layout_builder_gin';
$form['#attached']['library'][] = 'ghi_blocks/layout_builder_modal_admin';

$this->makeGinLbForm($form, $form_state);
return $form;
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return Url::fromRoute('ghi_blocks.node.page_elements', [
'node' => $this->entity->id(),
]);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->processAction();
$form_state->setRedirectUrl($this->getCancelUrl());
}

/**
* {@inheritdoc}
*/
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response->addCommand(new RedirectCommand($this->getCancelUrl()->toString()));
$response->addCommand(new CloseDialogCommand('#layout-builder-modal'));
return $response;
}

}
10 changes: 5 additions & 5 deletions html/modules/custom/ghi_blocks/src/Form/PageElementsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function buildForm(array $form, FormStateInterface $form_state, ?NodeInte
'#empty' => $this->t('No elements found in this region'),
];
}
$this->buildBulkForm($form, $this->getBulkFormActions());
$this->buildBulkForm($form, $form_state, !empty($rows) ? $this->getBulkFormActions() : []);
return $form;
}

Expand All @@ -142,13 +142,13 @@ public function buildForm(array $form, FormStateInterface $form_state, ?NodeInte
* A link array to be used by dropbutton.
*/
private function getBlockActionLink($label, BlockPluginInterface $plugin, $route_name, array $route_args = []) {
if (!$plugin instanceof GHIBlockBase) {
if ($route_name == 'ghi_blocks.hide_entity_block' && (!$plugin instanceof GHIBlockBase || $plugin->isHidden())) {
return NULL;
}
if ($route_name == 'ghi_blocks.hide_entity_block' && $plugin->isHidden()) {
if ($route_name == 'ghi_blocks.unhide_entity_block' && (!$plugin instanceof GHIBlockBase || !$plugin->isHidden())) {
return NULL;
}
if ($route_name == 'ghi_blocks.unhide_entity_block' && !$plugin->isHidden()) {
if ($route_name == 'ghi_blocks.show_block_config' && !$plugin instanceof GHIBlockBase) {
return NULL;
}
return [
Expand All @@ -173,7 +173,7 @@ private function getBulkFormActions() {
return [
'unhide' => $this->t('Unhide'),
'hide' => $this->t('Hide'),
'remove' => $this->t('Remove'),
// 'remove' => $this->t('Remove'),
];
}

Expand Down
Loading
Loading