Hi,
I have a view with relationships and contextual filters.
When the view has no results, the view crashes due to Views Tree.
Doing some debugging I was able to pinpoint to this part of the code:

$items[] = $rows[$i] . call_user_func(__FUNCTION__, $variables); 

(views_tree.module line 132)

After a bit of investigation it seems this is due to the assignment above $variables['parent'] = $record->views_tree_main; assuming views_tree_main is filled in, when it is in fact 0.

By adding a check to that variable it's fixed.

So changing

  foreach ($result as $i => $record) {
    if ($record->views_tree_parent == $parent) {
      $variables['parent'] = $record->views_tree_main;
      $items[] = $rows[$i] . call_user_func(__FUNCTION__, $variables);
    }
  }

to

  foreach ($result as $i => $record) {
    if (!empty($record->views_tree_main) && $record->views_tree_parent == $parent) {
      $variables['parent'] = $record->views_tree_main;
      $items[] = $rows[$i] . call_user_func(__FUNCTION__, $variables);
    }
  }

Issue fork views_tree-2935831

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kyuubi created an issue. See original summary.

kyuubi’s picture

StatusFileSize
new529 bytes

Here is the patch.

kyuubi’s picture

Status: Active » Needs review
a.sinitsa’s picture

Same problem in 8.x-2.0-alpha1 version:

Error: Call to a member function addClass() on null in template_preprocess_views_tree_table() (line 81 of modules/contrib/views_tree/views_tree.module).

robcarr’s picture

Version: 7.x-2.0 » 8.x-2.0-rc1
StatusFileSize
new621 bytes

Still relevant all these years on. PHP error only seems to appear when View not properly configured

Status: Needs review » Needs work

The last submitted patch, 5: empty-results-2935831.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

robcarr’s picture

StatusFileSize
new1.06 KB

Previous patch failed due to unrelated coding standards.

  • smustgrave committed f50bf191 on 3.0.x
    Issue #2935831 by robcarr, kyuubi: Bug when empty results
    
smustgrave’s picture

Version: 8.x-2.0-rc1 » 3.0.x-dev
Status: Needs work » Fixed

Wasn't able to reproduce but don't mind including this little defense. Will include in latest patch.

  • smustgrave committed 04c37639 on 3.0.x
    Issue #2935831 by robcarr, kyuubi, smustgrave: Bug when empty results -...
smustgrave’s picture

Status: Fixed » Needs work

JK this broke tests

sunlix made their first commit to this issue’s fork.

sunlix’s picture

Status: Needs work » Needs review

Hey,

I just changed the defense check.

The tests are green and the change would prevent the view from breaking if there are no results.

smustgrave’s picture

Status: Needs review » Needs work

can we add test coverage

smustgrave’s picture

Status: Needs work » Postponed (maintainer needs more info)

Actually not able to replicate is this still an issue?

sunlix’s picture

Hey smustgrave,

yes it is replicable with 3.0.1 of views_tree.
If you have any view with views_tree set up and no entries yet the issue displays.
In my personal setup it is a views_tree view to display organizational hierarchy in the admin area.
If no organizational nodes were created yet the error appears.

The full stack trace looks like this

Error: Call to a member function addClass() on null in template_preprocess_views_tree_table() (line 117 of modules/contrib/views_tree/views_tree.module).
call_user_func_array() (Line: 308)
Drupal\Core\Theme\ThemeManager->render() (Line: 497)
Drupal\Core\Render\Renderer->doRender() (Line: 510)
Drupal\Core\Render\Renderer->doRender() (Line: 227)
Drupal\Core\Render\Renderer->render() (Line: 66)
Drupal\views\Form\ViewsFormMainForm::preRenderViewsForm()
call_user_func_array() (Line: 107)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 876)
Drupal\Core\Render\Renderer->doCallback() (Line: 438)
Drupal\Core\Render\Renderer->doRender() (Line: 227)
Drupal\Core\Render\Renderer->render() (Line: 491)
Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 115)
__TwigTemplate_52e73aafba8185f3e0b6ed9c00ea8fad->doDisplay() (Line: 402)
Twig\Template->yield() (Line: 358)
Twig\Template->display() (Line: 373)
Twig\Template->render() (Line: 51)
Twig\TemplateWrapper->render() (Line: 34)
twig_render_template() (Line: 380)
Drupal\Core\Theme\ThemeManager->render() (Line: 497)
Drupal\Core\Render\Renderer->doRender() (Line: 510)
Drupal\Core\Render\Renderer->doRender() (Line: 227)
Drupal\Core\Render\Renderer->render() (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->{closure:Drupal\Core\Render\MainContent\HtmlRenderer::prepare():235}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 235)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 131)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray() (Line: 246)
Symfony\Component\EventDispatcher\EventDispatcher::{closure:Symfony\Component\EventDispatcher\EventDispatcher::optimizeListeners():241}() (Line: 206)
Symfony\Component\EventDispatcher\EventDispatcher->callListeners() (Line: 56)
Symfony\Component\EventDispatcher\EventDispatcher->dispatch() (Line: 188)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 116)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 53)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 715)
Drupal\Core\DrupalKernel->handle() (Line: 19)
sunlix’s picture

Status: Postponed (maintainer needs more info) » Needs review

Hey,

I debugged up to the missing view-style option empty_table.
If this option is true, the error appears.

I added a condition in the template_preprocess_views_tree_table to only iterate over the rows if there is a result.

The test now deletes all entities and try to render the view to force the error if the fix is not present.

hopefully the code is good enough :-)

smustgrave’s picture

Status: Needs review » Needs work

Thanks for working on this. If we are adding a new key it will need a schema entry and update hook using a ConfigImporter. May be able to find time to help with that last one.

sunlix’s picture

Status: Needs work » Needs review

Hey Steven,

thank you for your response.
I dont think we need any kind of schema update here.
empty_table is already defined in Drupal\views\Plugin\views\style\Table (config schema: views.style.table)
Since we lately corrected the config schema definition of views.style.tree_table to be type of views.style.table we are already fine with it. The plugin class do extend the core class.

Do you agree?

smustgrave’s picture

Status: Needs review » Needs work

Both points are still needed. We are defining our own style so the key tree_table.empty_table does not exist

sunlix’s picture

Hey Steven,

config schema's are cascading by extending already defined types. You can visualize that behavior by printing the full extended config schema via drush
empty_table is already defined by the inherited config type.
So I really think we are good to go here.

vendor/bin/drush php:eval "print_r(\Drupal::service('config.typed')->getDefinition('views.style.tree_table'));"
Array
(
    [label] => Views Tree (table)
    [class] => Drupal\Core\Config\Schema\Mapping
    [definition_class] => \Drupal\Core\TypedData\MapDataDefinition
    [mapping] => Array
        (
            [grouping] => Array
                (
                    [type] => sequence
                    [label] => Grouping field number %i
                    [sequence] => Array
                        (
                            [type] => mapping
                            [label] => Field
                            [mapping] => Array
                                (
                                    [field] => Array
                                        (
                                            [type] => string
                                            [label] => Field
                                        )

                                    [rendered] => Array
                                        (
                                            [type] => boolean
                                            [label] => Use rendered output to group rows
                                        )

                                    [rendered_strip] => Array
                                        (
                                            [type] => boolean
                                            [label] => Remove tags from rendered output
                                        )

                                )

                        )

                )

            [row_class] => Array
                (
                    [type] => string
                    [label] => Row class
                )

            [default_row_class] => Array
                (
                    [type] => boolean
                    [label] => Add views row classes
                )

            [uses_fields] => Array
                (
                    [type] => boolean
                    [label] => Force using fields
                )

            [columns] => Array
                (
                    [type] => sequence
                    [label] => Columns
                    [sequence] => Array
                        (
                            [type] => string
                            [label] => Columns name
                        )

                )

            [default] => Array
                (
                    [type] => string
                    [label] => Default sort
                )

            [info] => Array
                (
                    [type] => sequence
                    [label] => Columns info
                    [sequence] => Array
                        (
                            [type] => mapping
                            [label] => Column info
                            [mapping] => Array
                                (
                                    [sortable] => Array
                                        (
                                            [type] => boolean
                                            [label] => Sortable
                                        )

                                    [default_sort_order] => Array
                                        (
                                            [type] => string
                                            [label] => Default order
                                        )

                                    [align] => Array
                                        (
                                            [type] => string
                                            [label] => Align
                                        )

                                    [separator] => Array
                                        (
                                            [type] => string
                                            [label] => Separator
                                        )

                                    [empty_column] => Array
                                        (
                                            [type] => boolean
                                            [label] => Hide empty columns
                                        )

                                    [responsive] => Array
                                        (
                                            [type] => string
                                            [label] => Responsive
                                        )

                                )

                        )

                )

            [override] => Array
                (
                    [type] => boolean
                    [label] => Override normal sorting if click sorting is used
                )

            [sticky] => Array
                (
                    [type] => boolean
                    [label] => Enable Drupal style "sticky" table headers
                )

            [summary] => Array
                (
                    [type] => label
                    [label] => Summary title
                )

            [order] => Array
                (
                    [type] => string
                    [label] => Default order
                )

            [empty_table] => Array
                (
                    [type] => boolean
                    [label] => Show the empty text in the table
                )

            [caption] => Array
                (
                    [type] => label
                    [label] => Caption for the table
                )

            [description] => Array
                (
                    [type] => text
                    [label] => Table description
                )

            [class] => Array
                (
                    [type] => string
                    [label] => Table class
                )

            [main_field] => Array
                (
                    [type] => string
                    [label] => Main field
                )

            [parent_field] => Array
                (
                    [type] => string
                    [label] => Parent field
                )

            [display_hierarchy_column] => Array
                (
                    [type] => string
                    [label] => Column in which to represent the hierarchy
                )

        )

    [constraints] => Array
        (
            [ValidKeys] => <infer>
        )

    [unwrap_for_canonical_representation] => 1
    [type] => views.style.tree_table
)

Maybe I am missing here something. So please enlighten me if I am totally wrong. :-)

sunlix’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Fixed

I stand corrected,

Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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