It might be a good idea to implement all views hooks in views_hook_info implementation of hook_hook_info. Then they all have the option of living in the MODULE.views.inc file.

This is also a good opportunity to expand the test coverage a bit and update the tests to use the module handler service. I think the drupal_static stuff in the current ViewsHookTest are broken slightly too.

I have also refactored views_hook_info so it's not as repetitive.

Comments

damiankloip’s picture

Project: Drupal core » Views (for Drupal 7)
Issue summary: View changes

Updated issue summary.

damiankloip’s picture

Issue summary: View changes

Updated issue summary.

tim.plunkett’s picture

Project: Views (for Drupal 7) » Drupal core
+++ b/core/modules/views/lib/Drupal/views/Tests/ViewsHooksTest.phpundefined
@@ -16,42 +16,87 @@
+   * The moduel handler to use for invoking hooks.

module is spelled wrong

+++ b/core/modules/views/lib/Drupal/views/Tests/ViewsHooksTest.phpundefined
@@ -16,42 +16,87 @@
+  protected $testView;
...
+    $this->testView = views_get_view('test_view');

Is this actually needed?

+++ b/core/modules/views/lib/Drupal/views/Tests/ViewsHooksTest.phpundefined
@@ -16,42 +16,87 @@
+  public function setUp() {

If you're rerolling, make this protected :)

+++ b/core/modules/views/lib/Drupal/views/Tests/ViewsHooksTest.phpundefined
@@ -16,42 +16,87 @@
+        break;
+        case 'alter':
+          $data = array();
+          $this->moduleHandler->invoke('views_test_data', $hook, array($data));
+        break;
+        default:

break should be indented two spaces, and have a blank line after

+++ b/core/modules/views/views.moduleundefined
@@ -821,26 +821,24 @@ function &views_get_current_view() {
+  $hooks = array (
+    'views_data',
+    'views_data_alter',
+    'views_query_substitutions',
+    'views_form_substitutions',
+    'views_analyze',
+    'views_pre_view',
+    'views_pre_build',
+    'views_post_build',
+    'views_pre_execute',
+    'views_post_execute',
+    'views_pre_render',
+    'views_post_render',
+    'views_query_alter',
+    'views_invalidate_cache',
   );
 
-  return $hooks;
+  return array_fill_keys($hooks, array('group' => 'views'));

Nice!

damiankloip’s picture

StatusFileSize
new1.97 KB
new10.71 KB

Thanks for the review! Fair points.

dawehner’s picture

Aweseome patch!

+++ b/core/modules/views/lib/Drupal/views/Tests/ViewsHooksTest.phpundefined
@@ -16,42 +16,83 @@
+          $this->moduleHandler->invoke('views_test_data', $hook);

The problem with invoke in general though is that it uses function_exists() first, so you actually can't be sure that the file in views.inc haven't been loaded, as once it's loaded it's loaded, but yeah no idea how to address the problem here.

+++ b/core/modules/views/lib/Drupal/views/Tests/ViewsHooksTest.phpundefined
@@ -16,42 +16,83 @@
+      $this->assertTrue(state()->get('views_hook_test_' . $hook), format_string('The %hook hook was invoked.', array('%hook' => $hook)));

state() that from the container ;)

damiankloip’s picture

Thanks!

Yeah, I think we are at the mercy of the module handler here.

Not sure what you mean about the state()?

dawehner’s picture

$this->container->get('state')

damiankloip’s picture

StatusFileSize
new899 bytes
new10.73 KB

Oh! yeah, sure.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

This is ready to go now (with green).

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 1914256-6.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new1.22 KB
new11.77 KB

Oh, there is a nice little bug there, when we are invoking hook_views_query_substitutions we are passing in the query class, but I think we want to pass in the view! Also we should add typing to our implementation in views.module

dawehner’s picture

Issue tags: -VDC

#6: 1914256-6.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +VDC

The last submitted patch, 1914256-9.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new11.68 KB

I think this hook should be removed, as that's used in views_ui and not views. The tests have intentionally not included assertions for hook_views_preview_info_alter and hook_views_ui_display_top_links_alter as these should be covered in views ui?

dawehner’s picture

StatusFileSize
new1.24 KB
new12.92 KB

Then let's also adapt the other instances of it, to be more constistent.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

This is RTBC now.

damiankloip’s picture

Yep, those last tweaks look good.

catch’s picture

Status: Reviewed & tested by the community » Needs work

This puts the cached/info hooks and the runtime hooks into the same group, which means the info/cached hooks will end up more likely to be loaded on every request rather than less (although sites without views enabled at all could skip all of them).

We don't have any standards for what goes in hook_hook_info() but it was originally for performance (sometimes I put .admin.inc and .install files in apc.filters to keep APC size down given those are extremely low hit-rate files, potentially 'info' hook groups could be added to that). Since it doesn't account for dynamic hooks at all that really seems the only decent use for it apart from some minimal code organization.

dawehner’s picture

What about using .views_runtime.inc additional to the already existing .views.inc, which should be the static one, which is not needed often.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new21.56 KB

Ok, I've added the views_runtime group to the hooks in views_hook_info(), and moved module implementations into this new file.

I haven't moved views_views_query_substitutions yet, Do we think it's best to move that into a views.views_runtime.inc file?

dawehner’s picture

Yeah that one is totally a runtime hook.

damiankloip’s picture

StatusFileSize
new22.5 KB

Yeah, let's add it to a views.views_runtime.inc file.

Status: Needs review » Needs work

The last submitted patch, 1914256-20.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new1.17 KB
new22.6 KB

Oh dear, using classes when needed is usually a good plan.

Status: Needs review » Needs work

The last submitted patch, 1914256-22.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new506 bytes
new22.6 KB

We need that back too.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new22.71 KB
damiankloip’s picture

StatusFileSize
new22.94 KB

Another reroll after the patch adding ViewsDataCache::clear() got in

xjm’s picture

Issue tags: -VDC

#26: 1914256-26.patch queued for re-testing.

xjm’s picture

#26: 1914256-26.patch queued for re-testing.

Status: Reviewed & tested by the community » Needs work
Issue tags: +VDC

The last submitted patch, 1914256-26.patch, failed testing.

webchick’s picture

+++ b/core/modules/views/views.moduleundefined
@@ -819,24 +819,27 @@ function &views_get_current_view() {
+  ), array('group' => 'views'));
...
+  ), array('group' => 'views_runtime'));

I would flip these... most other modules have their run-time equivalents in just "module.views.inc", and their admin stuff in "module.admin.inc". (Maybe "module.views_admin.inc" in this case?) "views_runtime.inc" is very strange, and passes a bunch of implementation details down the stack to "normal" developers.

+++ b/core/modules/views/views.moduleundefined
@@ -819,24 +819,27 @@ function &views_get_current_view() {
+    'views_analyze',
...
+    'views_invalidate_cache',

While the rest of these hooks make sense to run on every view that's shown on the front-end of the site, these two seem like oddball ones out. Shouldn't invalidate_cache be included with data/data_alter?

Also, does analyze belong here? It seems like it's only called from views_ui.module.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new3.32 KB
new22.25 KB

I would flip these...

I'm not sure about the flipping, people are used to their data being stored in module.views.inc, it's not really admin data, as it's for the views_data that is used when executing views.

Catch suggested putting the runtime hooks into another file, but really this is optionally too. We can look at changing the name of this, but I think that is quite fitting for it.

Good point about the analyze and invalidate_cache hooks, they could definitely move into the .views.inc file too. hook_views_analyze is invoked in views_ui and node modules in core.

Here is a reroll and moving those 2 hooks. Oh, and sorry, the interdiff is missing the views_invalidate_cache move - Got carried away making changes half way through a rebase :)

xjm’s picture

hook_views_analyze() and hook_views_invalidate_cache() are only invoked during Views administrative operations, so they are not needed as frequently as the Views runtime hooks. They'd be better grouped with the other hooks.

No strong opinion on the labels for the groups.

Edit: Sorry, crossposted. I tend to accumulate a ton of half-written comments on Friday mornings. ;)

xjm’s picture

Filed #1962704: Document hook_views_analyze().

Trying to think of a better name for views_runtime. These are the Views execution hooks (#1810590: Improve naming and documentation to clarify hook execution order for exciting details). Maybe we could call the group view_render or view_execution or something?

dawehner’s picture

I really like the view_execution name, because it refers to the ViewExecutable.

damiankloip’s picture

Component: configuration entity system » views.module
StatusFileSize
new420 bytes
new22.28 KB

Ok, let's go for views_execution.

effulgentsia’s picture

Status: Needs review » Needs work

+1 to views_execution (plural).

effulgentsia’s picture

Status: Needs work » Reviewed & tested by the community

Lol. xpost.

dawehner’s picture

+1.00000000000001

webchick’s picture

Title: Add all views hooks to hook_hook_info() » Change notice: Add all views hooks to hook_hook_info()
Project: Drupal core » Views (for Drupal 7)
Version: 8.x-dev » 8.x-3.x-dev
Component: views.module » Documentation
Status: Reviewed & tested by the community » Active
Issue tags: +Needs change record

Ok, this looks good.

Alex explained that although hook_views_data() and their ilk are called less frequently, they're definitely defined much more frequently than any of the executable hooks, so it makes sense for them to take up the primary "views" namespace.

I also struggled a bit with "executable" as the adjective here, since that can also mean "something that can be killed" ;) whereas "runtime" is more explicit what's meant. But xjm pointed out that the underlying API name is "ViewsExecutable," so keeping these in sync is useful. Makes sense.

Committed and pushed to 8.x. Thanks!

Moving to the Views queue for the change notice there.

dave reid’s picture

This seems really odd. As a module developer, I have a hook that does not have 'views_executable' in the name, but now I have to figure out that I cannot just place it in my modulename.views.inc file. This breaks the current pattern of core by just putting everything into the one namespace for consistency and DX. Both hook_token_info() and hook_tokens() are placed in modulename.tokens.inc even though one is the registry of information, and one is the 'executable'. I would hate it as a module developer if I had to remember each time as a best practice to have both pants.token_info.inc and pants.token_executable.inc.

dave reid’s picture

tl;dr: It's a lot easier for a module developer to remember 'This is a Views hook, it should go in modulename.views.inc' rather than 'This should either go in modulename.views_execution.inc or modulename.views.inc.'

I feel very -1 on this change even though it's been committed.

webchick’s picture

I don't disagree on the DX impact, but this split was specifically asked for by catch in #16, for performance.

dave reid’s picture

:/

dawehner’s picture

Don't nail be down on that, but afaik you couldn't place the hook_views_FOO_BAR hooks into .views.inc, so at least that's not worse then in D7.

I agree that it's bad for DX, but seriously, we included probably more then 2000 lines of code or even more, without ever have the need for it.

xjm’s picture

Issue tags: +Needs followup

Actually, what we should do is document the appropriate file on each hook in views.api.php.

xjm’s picture

Issue summary: View changes

Updated issue summary.

damienmckenna’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.5.x-dev
Component: Documentation » views.module
Issue summary: View changes

Moving to the core issue queue.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lendude’s picture

Status: Active » Fixed
Issue tags: -Needs followup

Filed the requested follow up from #45, #3375847: Document in views.api.php for each hook in which include file they can be placed .

Discussed with @xjm and @longwave, issuing a CR now would serve no purpose, so closing this as fixed.

xjm’s picture

Title: Change notice: Add all views hooks to hook_hook_info() » Add all views hooks to hook_hook_info()
Issue tags: -Needs change record

Restoring original title and removing the now-irrelevant tag. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.