Skip to content
Snippets Groups Projects
Commit ad3f72ca authored by renatog's avatar renatog Committed by renatog
Browse files

Issue #3269015 by RenatoG: Apply entityPresave with Uppercase or Lowercase according to selecion

parent 5ffeb63d
Branches
Tags
10 merge requests!62Issue #3272848 by RenatoG: When the error appear the field is hidden and the...,!59Issue #3497768: Click JavaScript to close details causes modals to scroll unnecessarily,!35Issue #3366420: \Drupal calls should be avoided in classes,!34Issue #3366420: \Drupal calls should be avoided in classes,!29Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!28Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!23Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!22Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!20Issue #3272632 by RenatoG: Add a logic to block and disable the remove item if...,!7Issue #3320529 by justcaldwell: block_classes_stored setting only retains classes from most recently saved block
...@@ -37,9 +37,10 @@ function block_class_help($route_name, RouteMatchInterface $route_match) { ...@@ -37,9 +37,10 @@ function block_class_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_ENTITY_TYPE_presave(). * Implements hook_ENTITY_TYPE_presave().
*/ */
function block_class_block_presave(BlockInterface $entity) { function block_class_block_presave(BlockInterface $entity) {
if (empty($entity->getThirdPartySetting('block_class', 'classes'))) {
$entity->unsetThirdPartySetting('block_class', 'classes'); // Call the helper class to implement the presave logic.
} \Drupal::service('block_class.helper')->blockClassPreSave($entity);
} }
/** /**
...@@ -56,7 +57,7 @@ function block_class_form_block_form_alter(&$form, FormStateInterface $form_stat ...@@ -56,7 +57,7 @@ function block_class_form_block_form_alter(&$form, FormStateInterface $form_stat
* Implements hook_preprocess_HOOK(). * Implements hook_preprocess_HOOK().
*/ */
function block_class_preprocess_block(&$variables) { function block_class_preprocess_block(&$variables) {
// return;
// Call the helper class to implement the preprocess block logic. // Call the helper class to implement the preprocess block logic.
\Drupal::service('block_class.helper')->blockClassPreprocessBlock($variables); \Drupal::service('block_class.helper')->blockClassPreprocessBlock($variables);
......
...@@ -122,6 +122,45 @@ class BlockClassHelperService { ...@@ -122,6 +122,45 @@ class BlockClassHelperService {
$this->entityRepository = $entityRepository; $this->entityRepository = $entityRepository;
} }
/**
* Method to do the presave block.
*/
public function blockClassPreSave(&$entity) {
// If there is no class, unset the Third Party Setting.
if (empty($entity->getThirdPartySetting('block_class', 'classes'))) {
$entity->unsetThirdPartySetting('block_class', 'classes');
}
// Get the config object.
$config = $this->configFactory->getEditable('block_class.settings');
// Get the default case on settings.
$default_case = $config->get('default_case', 'standard');
// Get the block class.
$block_classes = $entity->getThirdPartySetting('block_class', 'classes');
switch ($default_case) {
case 'uppercase':
$block_classes = strtoupper($block_classes);
break;
case 'lowercase':
$block_classes = strtolower($block_classes);
break;
}
$entity->setThirdPartySetting('block_class', 'classes', $block_classes);
}
/** /**
* Method to redirect to Bulk Operations. * Method to redirect to Bulk Operations.
*/ */
...@@ -139,6 +178,33 @@ class BlockClassHelperService { ...@@ -139,6 +178,33 @@ class BlockClassHelperService {
} }
/**
* Method to do the preprocess block.
*/
public function blockClassPreprocessBlock(&$variables) {
// Blocks coming from page manager widget does not have id. If there is no
// Block ID, skip that.
if (empty($variables['elements']['#id'])) {
return;
}
// Load the block by ID.
$block = Block::load($variables['elements']['#id']);
// Verify if the current block has Third Party Settings with classes.
if ($block && $classes = $block->getThirdPartySetting('block_class', 'classes')) {
// Get all classes if exists.
$classes_array = explode(' ', $classes);
// Add all classes.
foreach ($classes_array as $class) {
$variables['attributes']['class'][] = Html::cleanCssIdentifier($class, []);
}
}
}
/** /**
* Method to do a form alter. * Method to do a form alter.
*/ */
...@@ -190,31 +256,4 @@ class BlockClassHelperService { ...@@ -190,31 +256,4 @@ class BlockClassHelperService {
} }
} }
/**
* Method to do the preprocess block.
*/
public function blockClassPreprocessBlock(&$variables) {
// Blocks coming from page manager widget does not have id. If there is no
// Block ID, skip that.
if (empty($variables['elements']['#id'])) {
return;
}
// Load the block by ID.
$block = Block::load($variables['elements']['#id']);
// Verify if the current block has Third Party Settings with classes.
if ($block && $classes = $block->getThirdPartySetting('block_class', 'classes')) {
// Get all classes if exists.
$classes_array = explode(' ', $classes);
// Add all classes.
foreach ($classes_array as $class) {
$variables['attributes']['class'][] = Html::cleanCssIdentifier($class, []);
}
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment