Commit 03c4be74 authored by renatog's avatar renatog Committed by renatog
Browse files

Issue #3269359 by RenatoG: Store the block class in a config to be used in the auto-complete

parent a53110f8
Loading
Loading
Loading
Loading
+34 −0
Changes for src/Service/BlockClassHelperService.php: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ use Drupal\Core\Entity\EntityFormInterface;
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\Html;
use Drupal\block_class\Constants\BlockClassConstants;
use Drupal\Component\Serialization\Json;


/**
 * Block Class Service Class.
@@ -169,6 +171,38 @@ class BlockClassHelperService {
    // Set the Third Party Settings.
    $entity->setThirdPartySetting('block_class', 'classes', $block_classes);

    // Get the config object.
    $config = $this->configFactory->getEditable('block_class.settings');

    // Get the current classes stored.
    $block_classes_stored = $config->get('block_classes_stored');

    // Get the array from JSON.
    $block_classes_stored = Json::decode($block_classes_stored);

    // Verify if is empty.
    if (empty($block_classes_stored)) {
      $block_classes_stored = [];
    }

    // Get the current class and export to array.
    $current_block_classes = explode(' ', $block_classes);

    // Use the key the same as value.
    $current_block_classes = array_combine($current_block_classes, $current_block_classes);

    // Merge with the current one.
    $block_classes_to_store = array_merge($current_block_classes, $current_block_classes);

    // Get as JSON.
    $block_classes_to_store = Json::encode($block_classes_to_store);

    // Store in the config.
    $config->set('block_classes_stored', $block_classes_to_store);

    // Save.
    $config->save();

  }

  /**