Commit f8f02625 authored by renatog's avatar renatog Committed by renatog
Browse files

Issue #3273852 by RenatoG: Create a hook update to load all attribute values...

Issue #3273852 by RenatoG: Create a hook update to load all attribute values and store in the settings to be used in the auto-complete
parent ed56365c
Loading
Loading
Loading
Loading
+70 −0
Original line number Diff line number Diff line
@@ -326,3 +326,73 @@ function block_class_update_20013() {
    $config->save();
  }
}

/**
 * Save the attribute value in the settings to be used in the auto-complete.
 */
function block_class_update_20014() {

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

  // Load blocks.
  $blocks = Block::loadMultiple();

  foreach ($blocks as $block) {

    // If there is no classes on ThirdPartySettings, skip.
    if (empty($block->getThirdPartySetting('block_class', 'attributes'))) {
      continue;
    }

    // Get the attributes configured.
    $attributes = $block->getThirdPartySetting('block_class', 'attributes');

    // Get the keys stored.
    $attribute_value_stored = $config->get('attribute_value_stored');

    // Decode this to get an array with those values.
    $attribute_value_stored = Json::decode($attribute_value_stored);

    // Verify if it's empty and set a default array on this.
    if (empty($attribute_value_stored)) {
      $attribute_value_stored = [];
    }

    // Get the array with the values.
    $attributes = explode(PHP_EOL, $attributes);

    // Do a foreach to get all items.
    foreach ($attributes as $attribute) {

      // Get by pipe to be able to get the key value.
      $attribute_array = explode('|', $attribute);

      // Get the attribute value.
      $attribute_value = $attribute_array[1];

      if (empty($attribute_value)) {
        continue;
      }

      // Put that on array.
      $attribute_value_stored[] = trim($attribute_value);

    }

    // Combine to use the id and value.
    $attribute_value_stored = array_combine($attribute_value_stored, $attribute_value_stored);

    // Merge the values.
    $attribute_value_stored = array_merge($attribute_value_stored, $attribute_value_stored);

    // Encode that to store in JSON.
    $attribute_value_stored = Json::encode($attribute_value_stored);

    // Set in the object.
    $config->set('attribute_value_stored', $attribute_value_stored);

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