Commit 12b09da4 authored by alexpott's avatar alexpott

Issue #1895266 by damiankloip, dawehner: Convert views to use the ModuleHandler.

parent 8f3468aa
......@@ -28,7 +28,7 @@ function field_views_data() {
if (empty($result)) {
$result = field_views_field_default_views_data($field);
}
drupal_alter('field_views_data', $result, $field, $module);
Drupal::moduleHandler()->alter('field_views_data', $result, $field, $module);
if (is_array($result)) {
$data = NestedArray::mergeDeep($result, $data);
......
......@@ -295,7 +295,8 @@ public function buildOptionsForm(&$form, &$form_state) {
'#weight' => 150,
);
// Allow to alter the default values brought into the form.
drupal_alter('views_handler_options', $this->options, $view);
// @todo Do we really want to keep this hook.
\Drupal::moduleHandler()->alter('views_handler_options', $this->options, $this->view);
}
/**
......
......@@ -1202,7 +1202,7 @@ public function optionsSummary(&$categories, &$options) {
'***DEFAULT_LANGUAGE***' => t("Default site language"),
LANGUAGE_NOT_SPECIFIED => t('Language neutral'),
);
if (module_exists('language')) {
if (\Drupal::moduleHandler()->moduleExists('language')) {
$languages = array_merge($languages, language_list());
}
$options['field_langcode'] = array(
......
......@@ -205,7 +205,7 @@ public function buildOptionsForm(&$form, &$form_state) {
);
// Only display the menu selector if menu module is enabled.
if (module_exists('menu')) {
if (\Drupal::moduleHandler()->moduleExists('menu')) {
$form['menu']['name'] = array(
'#title' => t('Menu'),
'#type' => 'select',
......@@ -322,7 +322,7 @@ public function buildOptionsForm(&$form, &$form_state) {
),
);
// Only display the menu selector if menu module is enabled.
if (module_exists('menu')) {
if (\Drupal::moduleHandler()->moduleExists('menu')) {
$form['tab_options']['name'] = array(
'#title' => t('Menu'),
'#type' => 'select',
......
......@@ -1379,7 +1379,7 @@ public function query($get_count = FALSE) {
}
// Add all query substitutions as metadata.
$query->addMetaData('views_substitutions', module_invoke_all('views_query_substitutions', $this));
$query->addMetaData('views_substitutions', \Drupal::moduleHandler()->invokeAll('views_query_substitutions', array($this->view)));
return $query;
}
......@@ -1402,10 +1402,7 @@ function get_where_args() {
* Let modules modify the query just prior to finalizing it.
*/
function alter(ViewExecutable $view) {
foreach (module_implements('views_query_alter') as $module) {
$function = $module . '_views_query_alter';
$function($view, $this);
}
\Drupal::moduleHandler()->invokeAll('views_query_alter', array($view, $this));
}
/**
......@@ -1455,7 +1452,7 @@ function execute(ViewExecutable $view) {
$items = array();
if ($query) {
$additional_arguments = module_invoke_all('views_query_substitutions', $view);
$additional_arguments = \Drupal::moduleHandler()->invokeAll('views_query_substitutions', array($view));
// Count queries must be run through the preExecute() method.
// If not, then hook_query_node_access_alter() may munge the count by
......
......@@ -290,7 +290,7 @@ function build_form(array $form, array &$form_state) {
'#prefix' => '<div id="edit-page-link-properties-wrapper">',
'#suffix' => '</div>',
);
if (module_exists('menu')) {
if (\Drupal::moduleHandler()->moduleExists('menu')) {
$menu_options = menu_get_menus();
}
else {
......@@ -348,7 +348,7 @@ function build_form(array $form, array &$form_state) {
);
}
if (!module_exists('block')) {
if (!\Drupal::moduleHandler()->moduleExists('block')) {
return $form;
}
......@@ -555,9 +555,7 @@ protected function row_style_options() {
* available).
*/
protected function build_filters(&$form, &$form_state) {
// Find all the fields we are allowed to filter by.
module_load_include('inc', 'views_ui', 'admin');
$fields = views_fetch_fields($this->base_table, 'filter');
$bundles = entity_get_bundles($this->entity_type);
// If the current base table support bundles and has more than one (like user).
......
......@@ -34,7 +34,7 @@ public static function getInfo() {
* Tests the views_ui_autocomplete_tag function.
*/
public function testViewsUiAutocompleteTag() {
module_load_include('inc', 'views_ui', 'admin');
\Drupal::moduleHandler()->loadInclude('views_ui', 'inc', 'admin');
// Save 15 views with a tag.
$tags = array();
......
......@@ -45,9 +45,11 @@ public static function importTestViews($class, $modules = array()) {
'create' => array(),
'change' => array(),
);
$module_handler = \Drupal::moduleHandler();
foreach ($modules as $module) {
$config_dir = drupal_get_path('module', $module) . '/test_views';
if (!is_dir($config_dir) || !module_exists($module)) {
if (!is_dir($config_dir) || !$module_handler->moduleExists($module)) {
continue;
}
......
......@@ -170,7 +170,8 @@ protected function viewsData() {
* Tests the views_fetch_fields function().
*/
public function testViewsFetchFields() {
module_load_include('inc', 'views_ui', 'admin');
$this->enableModules(array('views_ui'));
$this->container->get('module_handler')->loadInclude('views_ui', 'inc', 'admin');
$expected = array(
'field' => array(
......
......@@ -46,6 +46,8 @@ protected function setUp() {
$this->installSchema('language', 'language');
$this->installSchema('system', 'variable');
$this->enableModules(array('views_ui'));
$this->wizard = $this->container->get('plugin.manager.views.wizard')->createInstance('standard:views_test_data', array());
}
......
......@@ -987,10 +987,8 @@ public function build($display_id = NULL) {
}
// Let modules modify the view just prior to building it.
foreach (module_implements('views_pre_build') as $module) {
$function = $module . '_views_pre_build';
$function($this);
}
$module_handler = \Drupal::moduleHandler();
$module_handler->invokeAll('views_pre_build', array($this));
// Attempt to load from cache.
// @todo Load a build_info from cache.
......@@ -1111,10 +1109,7 @@ public function build($display_id = NULL) {
$this->attachDisplays();
// Let modules modify the view just after building it.
foreach (module_implements('views_post_build') as $module) {
$function = $module . '_views_post_build';
$function($this);
}
$module_handler->invokeAll('views_post_build', array($this));
return TRUE;
}
......@@ -1189,10 +1184,8 @@ public function execute($display_id = NULL) {
}
// Let modules modify the view just prior to executing it.
foreach (module_implements('views_pre_execute') as $module) {
$function = $module . '_views_pre_execute';
$function($this);
}
$module_handler = \Drupal::moduleHandler();
$module_handler->invokeAll('views_pre_execute', array($this));
// Check for already-cached results.
if (!empty($this->live_preview)) {
......@@ -1217,10 +1210,7 @@ public function execute($display_id = NULL) {
}
// Let modules modify the view just after executing it.
foreach (module_implements('views_post_execute') as $module) {
$function = $module . '_views_post_execute';
$function($this);
}
$module_handler->invokeAll('views_post_execute', array($this));
$this->executed = TRUE;
}
......@@ -1254,6 +1244,8 @@ public function render($display_id = NULL) {
$exposed_form = $this->display_handler->getPlugin('exposed_form');
$exposed_form->pre_render($this->result);
$module_handler = \Drupal::moduleHandler();
// Check for already-cached output.
if (!empty($this->live_preview)) {
$cache = FALSE;
......@@ -1261,6 +1253,7 @@ public function render($display_id = NULL) {
else {
$cache = $this->display_handler->getPlugin('cache');
}
if ($cache && $cache->cache_get('output')) {
}
else {
......@@ -1301,23 +1294,15 @@ public function render($display_id = NULL) {
}
// Let modules modify the view just prior to rendering it.
foreach (module_implements('views_pre_render') as $module) {
$function = $module . '_views_pre_render';
$function($this);
}
$module_handler->invokeAll('views_pre_render', array($this));
// Let the themes play too, because pre render is a very themey thing.
foreach ($GLOBALS['base_theme_info'] as $base) {
$function = $base->name . '_views_pre_render';
if (function_exists($function)) {
$function($this);
}
}
$function = $GLOBALS['theme'] . '_views_pre_render';
if (function_exists($function)) {
$function($this);
$module_handler->invoke($base, 'views_pre_render', array($this));
}
$module_handler->invoke($GLOBALS['theme'], 'views_pre_render', array($this));
$this->display_handler->output = $this->display_handler->render();
if ($cache) {
$cache->cache_set('output');
......@@ -1331,23 +1316,15 @@ public function render($display_id = NULL) {
}
// Let modules modify the view output after it is rendered.
foreach (module_implements('views_post_render') as $module) {
$function = $module . '_views_post_render';
$function($this, $this->display_handler->output, $cache);
}
$module_handler->invokeAll('views_post_render', array($this, $this->display_handler->output, $cache));
// Let the themes play too, because post render is a very themey thing.
foreach ($GLOBALS['base_theme_info'] as $base) {
$function = $base->name . '_views_post_render';
if (function_exists($function)) {
$function($this);
}
}
$function = $GLOBALS['theme'] . '_views_post_render';
if (function_exists($function)) {
$function($this, $this->display_handler->output, $cache);
$module_handler->invoke($base, 'views_post_render', array($this));
}
$module_handler->invoke($GLOBALS['theme'], 'views_post_render', array($this));
return $this->display_handler->output;
}
......@@ -1423,10 +1400,7 @@ public function preExecute($args = array()) {
}
// Let modules modify the view just prior to executing it.
foreach (module_implements('views_pre_view') as $module) {
$function = $module . '_views_pre_view';
$function($this, $display_id, $this->args);
}
\Drupal::moduleHandler()->invokeAll('views_pre_view', array($this, $display_id, $this->args));
// Allow hook_views_pre_view() to set the dom_id, then ensure it is set.
$this->dom_id = !empty($this->dom_id) ? $this->dom_id : hash('sha256', $this->storage->id() . REQUEST_TIME . mt_rand());
......
......@@ -23,7 +23,7 @@ public function load(array $ids = NULL) {
// Only return views for enabled modules.
return array_filter($entities, function ($entity) {
if (module_exists($entity->get('module'))) {
if (\Drupal::moduleHandler()->moduleExists($entity->get('module'))) {
return TRUE;
}
return FALSE;
......
......@@ -31,6 +31,7 @@ public function build(ContainerBuilder $container) {
$container->register('views.views_data', 'Drupal\views\ViewsDataCache')
->addArgument(new Reference('cache.views_info'))
->addArgument(new Reference('config.factory'))
->addArgument(new Reference('module_handler'))
->addTag('needs_destruction');
$container->register('views.executable', 'Drupal\views\ViewExecutableFactory');
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\DestructableInterface;
/**
......@@ -73,8 +74,26 @@ class ViewsDataCache implements DestructableInterface {
*/
protected $langcode;
public function __construct(CacheBackendInterface $cache_backend, ConfigFactory $config) {
/**
* Stores a module manager to invoke hooks.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs this ViewsDataCache object.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend to use.
* @param \Drupal\Core\Config\ConfigFactory $config
* The configuration factory object to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler class to use for invoking hooks.
*/
public function __construct(CacheBackendInterface $cache_backend, ConfigFactory $config, ModuleHandlerInterface $module_handler) {
$this->cacheBackend = $cache_backend;
$this->moduleHandler = $module_handler;
$this->langcode = language(LANGUAGE_TYPE_INTERFACE)->langcode;
$this->skipCache = $config->get('views.settings')->get('skip_cache');
......@@ -176,8 +195,8 @@ protected function getData() {
return $data->data;
}
else {
$data = module_invoke_all('views_data');
drupal_alter('views_data', $data);
$data = $this->moduleHandler->invokeAll('views_data');
$this->moduleHandler->alter('views_data', $data);
$this->processEntityTypes($data);
......
......@@ -79,7 +79,7 @@ function views_pre_render_view_element($element) {
* Implement hook_theme(). Register views theming functions.
*/
function views_theme($existing, $type, $theme, $path) {
module_load_include('inc', 'views', 'views.theme');
Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
// Some quasi clever array merging here.
$base = array(
......@@ -687,13 +687,15 @@ function views_invalidate_cache() {
// Set the menu as needed to be rebuilt.
state()->set('menu_rebuild_needed', TRUE);
$module_handler = Drupal::moduleHandler();
// Invalidate the block cache to update views block derivatives.
if (module_exists('block')) {
drupal_container()->get('plugin.manager.block')->clearCachedDefinitions();
if ($module_handler->moduleExists('block')) {
Drupal::service('plugin.manager.block')->clearCachedDefinitions();
}
// Allow modules to respond to the Views cache being cleared.
module_invoke_all('views_invalidate_cache');
$module_handler->invokeAll('views_invalidate_cache');
}
/**
......@@ -955,7 +957,7 @@ function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
}
// @todo While Views is providing on behalf of core modules, check to see
// if they are enabled or not.
if (isset($plugin['module']) && !module_exists($plugin['module'])) {
if (isset($plugin['module']) && !\Drupal::moduleHandler()->moduleExists($plugin['module'])) {
continue;
}
......@@ -980,10 +982,9 @@ function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
* An array of plugin definitions for all types.
*/
function views_get_plugin_definitions() {
$container = drupal_container();
$plugins = array();
foreach (ViewExecutable::getPluginTypes() as $plugin_type) {
$plugins[$plugin_type] = $container->get("plugin.manager.views.$plugin_type")->getDefinitions();
$plugins[$plugin_type] = Views::pluginManager($plugin_type)->getDefinitions();
}
return $plugins;
......@@ -1015,10 +1016,8 @@ function views_get_enabled_display_extenders() {
* @endcode
*/
function views_get_applicable_views($type) {
$container = drupal_container();
// Get all display plugins which provides the type.
$display_plugins = $container->get('plugin.manager.views.display')->getDefinitions();
$display_plugins = Views::pluginManager('display')->getDefinitions();
$ids = array();
foreach ($display_plugins as $id => $definition) {
if (!empty($definition[$type])) {
......@@ -1026,13 +1025,13 @@ function views_get_applicable_views($type) {
}
}
$entity_ids = $container->get('entity.query')->get('view')
$entity_ids = Drupal::service('entity.query')->get('view')
->condition('status', TRUE)
->condition("display.*.display_plugin", $ids, 'IN')
->execute();
$result = array();
foreach ($container->get('plugin.manager.entity')->getStorageController('view')->load($entity_ids) as $view) {
foreach (Drupal::service('plugin.manager.entity')->getStorageController('view')->load($entity_ids) as $view) {
// Check each display to see if it meets the criteria and is enabled.
$executable = $view->get('executable');
$executable->initDisplay();
......@@ -1560,7 +1559,7 @@ function views_exposed_form_cache($views_name, $display_name, $form_output = NUL
* Build a list of theme function names for use most everywhere.
*/
function views_theme_functions($hook, ViewExecutable $view, $display = NULL) {
module_load_include('inc', 'views', 'views.theme');
Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');
return _views_theme_functions($hook, $view, $display);
}
......
......@@ -1023,7 +1023,7 @@ function theme_views_form_views_form($variables) {
$replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
}
// Add in substitutions from hook_views_form_substitutions().
$substitutions = module_invoke_all('views_form_substitutions');
$substitutions = Drupal::moduleHandler()->invokeAll('views_form_substitutions');
foreach ($substitutions as $placeholder => $substitution) {
$search[] = $placeholder;
$replace[] = $substitution;
......
......@@ -87,8 +87,8 @@ public function getForm(ViewStorageInterface $view, $display_id, $js) {
$key = $form_state['form_key'];
// @todo Remove the need for this.
module_load_include('inc', 'views_ui', 'admin');
module_load_include('inc', 'views', 'includes/ajax');
\Drupal::moduleHandler()->loadInclude('views_ui', 'inc', 'admin');
\Drupal::moduleHandler()->loadInclude('views', 'inc', 'includes/ajax');
// Reset the cache of IDs. Drupal rather aggressively prevents ID
// duplication but this causes it to remember IDs that are no longer even
......
......@@ -100,7 +100,7 @@ public function form(array $form, array &$form_state, EntityInterface $view) {
'#options' => $options,
);
$show_form = &$form['displays']['show'];
$default_value = module_exists('node') ? 'node' : 'users';
$default_value = \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'users';
$show_form['wizard_key']['#default_value'] = WizardPluginBase::getSelected($form_state, array('show', 'wizard_key'), $default_value, $show_form['wizard_key']);
// Changing this dropdown updates the entire content of $form['displays'] via
// AJAX.
......
......@@ -326,7 +326,7 @@ public function getDisplayTab($view) {
}
// In AJAX context, ViewUI::rebuildCurrentTab() returns this outside of form
// context, so hook_form_views_ui_edit_form_alter() is insufficient.
drupal_alter('views_ui_display_tab', $build, $view, $display_id);
\Drupal::moduleHandler()->alter('views_ui_display_tab', $build, $view, $display_id);
return $build;
}
......@@ -667,7 +667,7 @@ public function renderDisplayTop(ViewUI $view) {
);
// Let other modules add additional links here.
drupal_alter('views_ui_display_top_links', $element['extra_actions']['#links'], $view, $display_id);
\Drupal::moduleHandler()->alter('views_ui_display_top_links', $element['extra_actions']['#links'], $view, $display_id);
if (isset($view->type) && $view->type != t('Default')) {
if ($view->type == t('Overridden')) {
......@@ -724,7 +724,7 @@ public function renderDisplayTop(ViewUI $view) {
* should not yet redirect to the destination.
*/
public function submitDelayDestination($form, &$form_state) {
$query = drupal_container()->get('request')->query;
$query = \Drupal::service('request')->query;
// @todo: Revisit this when http://drupal.org/node/1668866 is in.
$destination = $query->get('destination');
if (isset($destination) && $form_state['redirect'] !== FALSE) {
......
......@@ -82,7 +82,7 @@ public static function getAdminCSS() {
$list[$module_path . '/css/views-admin.css'] = array();
$list[$module_path . '/css/views-admin.theme.css'] = array();
if (module_exists('contextual')) {
if (\Drupal::moduleHandler()->moduleExists('contextual')) {
$list[$module_path . '/css/views-admin.contextual.css'] = array();
}
......
......@@ -651,7 +651,7 @@ public function renderPreview($display_id, $args = array()) {
$rows['statistics'][] = array('<strong>' . t('View render time') . '</strong>', t('@time ms', array('@time' => intval($this->executable->render_time * 100000) / 100)));
}
drupal_alter('views_preview_info', $rows, $this);
\Drupal::moduleHandler()->alter('views_preview_info', $rows, $this);
}
else {
// No query was run. Display that information in place of either the
......
......@@ -246,7 +246,7 @@ function views_ui_cache_set(ViewUI $view) {
*/
function views_ui_preprocess_views_view(&$vars) {
$view = $vars['view'];
if (!empty($view->live_preview) && module_exists('contextual')) {
if (!empty($view->live_preview) && Drupal::moduleHandler()->moduleExists('contextual')) {
$view->hide_admin_links = TRUE;
foreach (array('title', 'header', 'exposed', 'rows', 'pager', 'more', 'footer', 'empty', 'attachment_after', 'attachment_before') as $section) {
if (!empty($vars[$section])) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment