Problem/Motivation

From #1851086: Replace admin/people with a View.

We need to be able to have optional handlers, such and language fields etc., that can ship with a default view but aren't necessarily needed

At the moment this is considered a broken handler, as it can't be found if the module is not available. This means that (e.g.) the admin/people view displays a warning when the language module is not available.

 users translation_link field

Proposed resolution

This could be resolved by adding an $optional parameter for views_get_handler() that determines whether a handler is 'optional'. Calling code can then use this parameter. For example, DisplayPluginBase::getHandlers() can check if an 'optional' key is set in the handler configuration, and pass this to views_get_handler() when it's called. This allows us to ignore missing handlers when they are considered optional.

Remaining tasks

If it is acceptable to have an optional handler, we should not show broken handler messages, and maybe not even load a broken handler stub instance - A point that needs to be discussed.

Here are two initial patches, one just suppressing the debug message if an optional flag is found, and the other actually not returning a handler instance.

Comments

damiankloip’s picture

StatusFileSize
new46.57 KB
new63.11 KB

Here is a screen grab from before/after using the admin/people patch referenced in the summary.

damiankloip’s picture

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

Issue summary formatting.

xjm’s picture

Issue summary: View changes

Updated issue summary.

tim.plunkett’s picture

Project: Views (for Drupal 7) » Drupal core
Issue tags: +Needs tests
StatusFileSize
new13.76 KB
new13.5 KB

Not using the broken handler results in an even more wonky UI. We should definitely go in the direction of the first patch.

However, we can go one step further in simplifying the DX for callers. The tests look a bit more awkward, but that's not who we're optimizing for.

dawehner’s picture

+++ b/core/modules/views/lib/Drupal/views/Plugin/views/exposed_form/InputRequired.phpundefined
@@ -82,8 +82,9 @@ function pre_render($values) {
+        'optional' => FALSE,

There is no need to have this marked as not optional.

+++ b/core/modules/views/views.moduleundefined
@@ -877,21 +877,25 @@ function views_library_info() {
+ *   - optional: (optional) Whether or not this handler is optional. If a
+ *     handler is missing and not optional, a debug message will be displayed.

This should explain that optional is defaulted to FALSE.

damiankloip’s picture

Yep, I think I like this approach. Not sure if it makes things more confusing to read, as the params are a bit more self documenting. It's better encapsulating the logic inside views_get_handler() though.

+1

Status: Needs review » Needs work

The last submitted patch, vdc-1966424-2.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
StatusFileSize
new3.15 KB
new14.87 KB

Addressed the comments in #3 (thanks) and fixed two calls to this in tests I missed.

Still needs dedicated tests.

dawehner’s picture

I will either write tests tomorrow morning or in the evening, if noone else stepped forward in the meantime.

dawehner’s picture

StatusFileSize
new3.16 KB
new17.5 KB

Added some test coverage for that.

tim.plunkett’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests

@dawehner++

+++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.phpundefined
@@ -21,6 +22,15 @@ class ModuleTest extends ViewUnitTestBase {
+  public $lastErrorMessage;

Could be protected, I guess. Doesn't really matter.

+++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.phpundefined
@@ -67,7 +77,59 @@ function testviews_get_handler() {
+    // Test non existing tables/fields.

non-existant

+++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.phpundefined
@@ -67,7 +77,59 @@ function testviews_get_handler() {
+    set_error_handler(array($this, 'customErrorHandler'));

Nice!

+++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.phpundefined
@@ -67,7 +77,59 @@ function testviews_get_handler() {
+    $item = array(
+    'table' => 'views_test_data',
+    'field' => 'field_invalid',
+  );
+    views_get_handler($item, 'field');

Weird indentation

damiankloip’s picture

Non-existent?

damiankloip’s picture

Looking good, but then I don't expect anything less.

+++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.phpundefined
@@ -35,7 +45,11 @@ public static function getInfo() {
@@ -44,9 +58,13 @@ function testviews_get_handler() {

I vote we fix the test method name too while we are touching this test? That name is mildy offensive :)

+++ b/core/modules/views/lib/Drupal/views/Tests/ModuleTest.phpundefined
@@ -54,8 +72,64 @@ function testviews_get_handler() {
+    $this->assertFalse($this->lastErrorMessage, "An optional handler does not throw a debug message.");

Is it worth us also just adding a quick test that an actual handler with optional = TRUE doesn't throw a debug. I know it won't but you never know what'll happen in the future.

xjm’s picture

#10 is correct.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new2.26 KB
new18.07 KB
Is it worth us also just adding a quick test that an actual handler with optional = TRUE doesn't throw a debug. I know it won't but you never know what'll happen in the future

Isn't that what assertFalse() is doing? Added a test for explicit 'optional' => FALSE.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

This is awesome! Yay team.

tim.plunkett’s picture

Priority: Normal » Major

This blocks #1851086: Replace admin/people with a View, so it is also major

webchick’s picture

Status: Reviewed & tested by the community » Fixed

This actually looks like nice code clean-up, in addition to providing that capability.

Committed and pushed to 8.x. Thanks!

xjm’s picture

Title: Allow Views handlers to be optional » Change notice: Allow Views handlers to be optional
Project: Drupal core » Views (for Drupal 7)
Version: 8.x-dev » 8.x-3.x-dev
Component: views.module » Code
Priority: Major » Critical
Status: Fixed » Active

Probably merits a change notice?

xjm’s picture

Priority: Critical » Normal
dawehner’s picture

So should we expose this bit in the UI?

xjm’s picture

@dawehner, maybe a followup to discuss the possibility of the UI would be good? My gut reaction is no, because it adds another whole layer of complexity to the view, but not sure.

dawehner’s picture

Well, there are already a lot of different levels of configuration the normal user would never need. Additional I don't think changing config files by hand is recommended at all?

dawehner’s picture

Issue summary: View changes

Updated issue summary.

chris matthews’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.7.x-dev
Component: Code » views.module

For more information as to why this issue was moved to the Drupal core project, please see issue #3030347: Plan to clean process issue queue

chris matthews’s picture

Project: Drupal core » Views (for Drupal 7)
Version: 8.7.x-dev » 7.x-3.x-dev
Component: views.module » Code
Status: Active » Closed (outdated)

Moving back to the contributed Views issue queue and closing as outdated per https://www.drupal.org/project/views/issues/3030347#comment-13023447