You are here

function ds_extras_block_submit in Display Suite 8.3

Same name and namespace in other branches
  1. 8.2 modules/ds_extras/ds_extras.module \ds_extras_block_submit()
  2. 7.2 modules/ds_extras/includes/ds_extras.admin.inc \ds_extras_block_submit()
  3. 7 modules/ds_extras/ds_extras.admin.inc \ds_extras_block_submit()

Submit callback: manage block regions.

1 string reference to 'ds_extras_block_submit'
ds_extras_form_entity_view_display_edit_form_alter in modules/ds_extras/ds_extras.module
Implements hook_form_FORM_ID_alter().

File

modules/ds_extras/ds_extras.module, line 132
Display Suite extras main functions.

Code

function ds_extras_block_submit($form, FormStateInterface $form_state) {

  // Create new region.
  if ($new_block_region = $form_state
    ->getValue('new_block_region')) {

    // Get the entity_type, bundle and view mode.
    $entity_type = $form['#entity_type'];
    $bundle = $form['#bundle'];
    $view_mode = $form_state
      ->getFormObject()
      ->getEntity()
      ->getMode();
    $block = [
      'title' => $new_block_region,
      'info' => "{$entity_type}_{$bundle}_{$view_mode}",
    ];
    $block_key = $form_state
      ->getValue('new_block_region_key');
    $region_blocks = \Drupal::config('ds_extras.settings')
      ->get('region_blocks');
    if (empty($region_blocks)) {
      $region_blocks = [];
    }
    $region_blocks[$block_key] = $block;
    \Drupal::configFactory()
      ->getEditable('ds_extras.settings')
      ->set('region_blocks', $region_blocks)
      ->save();
  }

  // Remove a region.
  if ($form_state
    ->hasValue('remove_block_region')) {
    $region_blocks = \Drupal::config('ds_extras.settings')
      ->get('region_blocks');
    $region_removal_status = $form_state
      ->getValue('remove_block_region');
    foreach ($region_removal_status as $key => $value) {
      if ($value !== 0 && $key == $value) {
        unset($region_blocks[$key]);

        // Make sure there is no active block instance for this ds block region.
        if (\Drupal::moduleHandler()
          ->moduleExists('block')) {
          $ids = \Drupal::entityQuery('block')
            ->condition('plugin', 'ds_region_block:' . $key)
            ->execute();
          $block_storage = \Drupal::service('entity_type.manager')
            ->getStorage('block');
          foreach ($block_storage
            ->loadMultiple($ids) as $block) {
            $block
              ->delete();
          }
        }
      }
    }

    // Clear cached block and ds plugin defintions.
    \Drupal::service('plugin.manager.block')
      ->clearCachedDefinitions();
    \Drupal::service('plugin.manager.ds')
      ->clearCachedDefinitions();

    // Remove the region from ds settings.
    \Drupal::configFactory()
      ->getEditable('ds_extras.settings')
      ->set('region_blocks', $region_blocks)
      ->save();
  }
}