You are here

function select2_bef_update_8001 in Select2 Boxes 8

Provide upgrade path for better_exposed_filters from 8.x-3.x to 8.x-4.x.

File

modules/select2_bef/select2_bef.install, line 11
Contains select2_bef.install.

Code

function select2_bef_update_8001() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory
    ->listAll('views.view.') as $config_name) {
    $config = $config_factory
      ->getEditable($config_name);

    // Go through each display on each view.
    $displays = $config
      ->get('display');
    foreach ($displays as $display_name => $display) {
      if (!empty($display['display_options']['exposed_form'])) {
        $exposed_form = $display['display_options']['exposed_form'];

        // Find BEF exposed forms.
        if (isset($exposed_form['type']) && $exposed_form['type'] === 'bef' && isset($exposed_form['options']['bef']['filter'])) {
          foreach ($exposed_form['options']['bef']['filter'] as $field_name => $configuration) {
            if ($configuration['plugin_id'] === 'select2boxes_autocomplete_list' || $configuration['plugin_id'] === 'select2boxes_autocomplete_multi' || $configuration['plugin_id'] === 'select2boxes_autocomplete_single') {
              $configuration['plugin_id'] = 'select2boxes';
              $exposed_form['options']['bef']['filter'][$field_name] = $configuration;
            }
          }

          // Update BEF settings.
          $config
            ->set("display.{$display_name}.display_options.exposed_form.options.bef", $exposed_form['options']['bef']);
        }
      }
    }
    $config
      ->save(TRUE);
  }
}