You are here

function global_filter_remove_default_filter_from_views in Views Global Filter 8

Same name and namespace in other branches
  1. 7 global_filter.module \global_filter_remove_default_filter_from_views()

Remove a deleted global filter from any view using it as a contextual filter.

Note: because the same field/view may be used in more than one block and passed to the same view as a contextual filter (e.g top and bottom of the same page), it is not entirely correct to always remove it as a contextual filter. Should really check if the same argument is used in more than one global filter.... @todo

Parameters

string $name: name of the filter

3 calls to global_filter_remove_default_filter_from_views()
global_filter_block_info in ./global_filter.blocks.inc
Implements hook_block_info().
global_filter_block_save in ./global_filter.blocks.inc
Implements hook_block_save().
global_filter_uninstall in ./global_filter.install
Implements hook_uninstall().

File

./global_filter.module, line 374
global_filter.module

Code

function global_filter_remove_default_filter_from_views($name) {
  if (!module_exists('views')) {
    return;
  }
  $views = views_get_all_views();
  views_load_display_records($views);

  // Go through all Views and delete the default global filter if it exists.
  foreach ($views as $view) {
    foreach ($view->display as $display_name => $display) {
      if (!empty($display->display_options['arguments'])) {
        $arguments = $display->display_options['arguments'];
        if (isset($arguments[$name])) {
          $full_name = $name;
        }
        elseif (isset($arguments[$name . '_value'])) {
          $full_name = $name . '_value';
        }
        elseif (isset($arguments[$name . '_tid'])) {
          $full_name = $name . '_tid';
        }
        if (!empty($full_name) && !empty($arguments[$full_name]['default_argument_type']) && strpos($arguments[$full_name]['default_argument_type'], 'global_filter') !== FALSE) {
          unset($view->display[$display_name]->display_options['arguments'][$full_name]);
          drupal_set_message(t('As the global filter %filter was deleted, it was removed as the contextual default from the view %view.', array(
            '%filter' => $name,
            '%view' => empty($view->human_name) ? $view->name : $view->human_name,
          )));
          views_save_view($view);
        }
      }
    }
  }
}