Problem/Motivation

When a text field uses a not-existing config entity, rendering fails with a fatal error.

Fatal error: Call to a member function filters() on a non-object in core/modules/text/text.module on line 83

Steps to Reproduce
Create a file called test.php in root with the following:


   $node = entity_create('node', array(
      'type' => 'article',
      'title' => 'test',
      'body' => array(
        'value' => md5(uniqid(rand(), true)),
        'format' => 'filtered_html_invalid',
      ),
      'uid' => 1,
    ));
    $node->save();

Followed by the drush command
drush scr test.php

Try to view the entity then.

Proposed resolution

Write a test to show this doesn't work as expected.
Put in a safeguard if incorrect declaration of a text format or fix the missing filter() method.

Remaining tasks

- Write tests.

User interface changes

API changes

-

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

podarok’s picture

Status: Active » Closed (cannot reproduce)

Can't reproduce within current dev

$ drush scr test.php 
$node = entity_create('node', array(
      'type' => 'article',
      'title' => 'test',
      'body' => array(
        'value' => md5(uniqid(rand(), true)),
        'format' => 'filtered_html',
      ),
      'uid' => 1,
    ));
    $node->save();

The same via devel/php
I see no notices. Node test successfully created.

fago’s picture

Issue summary: View changes
Status: Closed (cannot reproduce) » Needs work
FileSize
1.02 KB

I ran into the same issue while working on a site migration. The fatal error only appears when the content is rendered as it tries to call the filters() method on a non-existing config entity. Attached patch fixes the issue.

Usually, we do no support broken configuration like this but clean-up the config instead. However, in the special case of filter formats I think this support of broken configuration is pre-existing but got broken. There is even already a comment for this case, but the check fails.

Agreed, this needs tests also.

fago’s picture

Status: Needs work » Needs review

Testbot, go.

fago’s picture

Status: Needs review » Needs work

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

BrightBold’s picture

I just ran into this upgrading to Drupal 8.1. The solution turned out to be that the Typogrify module, which does not have a D8 version yet, needed to be completely uninstalled, not just disabled, in D7. So if anyone else is reading this issue because you've into this problem, make sure you uninstalled any modules that provide input filters that aren't available in D8.

Dan_Rogers’s picture

Just experienced this during an upgrade, and #6 pointed me in the right direction. For me, the quick fix was to add a text format with the same machine name as a custom one added to the D7 donor site.

hgoto’s picture

I confirmed that this error occurs. And the patch #2 works for me.

I created a simple test for this and would like someone to review it.

The last submitted patch, 8: d8_invalid_format-2359389-8-test_only_should_fail.patch, failed testing.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.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.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.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.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jeqq’s picture

I've rerolled the patch, the issue is still actual in Drupal 8.4.x. When the filter is invalid, I have this error: Error: Call to a member function filters() on null in .../core/modules/text/text.module on line 84

kmoll’s picture

I have tested this and I can confirm it is working and fixes the error.

kmoll’s picture

Status: Needs review » Reviewed & tested by the community
larowlan’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/text/tests/src/Kernel/TextSummaryTest.php
    @@ -208,6 +208,19 @@ public function testLength() {
    +  public function testInvalidFormatter() {
    

    The word formatter has a different meaning in Drupal. We're testing an invalid filter format, we should name the test method testInvalidFilterFormat

  2. +++ b/core/modules/text/tests/src/Kernel/TextSummaryTest.php
    @@ -208,6 +208,19 @@ public function testLength() {
    +    $invalid_format = 'text_invalid';
    +
    +    $text = $this->randomString(100);
    +    $expected = '';
    

    I don't think we need inline variables here, so

    $this->assertTextSummary($this->randomString(100), '', 'text_invalid');

    would suffice.

    But I think we should name the invalid filter format 'non_existent_format' to better reflect what is being tested here.

  3. +++ b/core/modules/text/text.module
    @@ -81,13 +81,14 @@ function text_summary($text, $format = NULL, $size = NULL) {
    +    $format_config = FilterFormat::load($format);
    ...
    +    if (!$format_config) {
    ...
    +    $filters = $format_config->filters();
    

    Sorry to be pedantic, but can we name this variable filter_format as that better reflects what we're working with?

harsha012’s picture

Status: Needs work » Needs review
FileSize
1.59 KB
1.5 KB

fixed the nit picks as per #17

douggreen’s picture

Status: Needs review » Reviewed & tested by the community

I agree with @fago in comment #2 that we normally wouldn't fix this, however the comment makes the code incorrect. So we should either fix it or remove the comment. Since the fix is trivial, and already here, I think we should fix it.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/text/text.module
@@ -81,13 +81,14 @@ function text_summary($text, $format = NULL, $size = NULL) {
-    if (!$filters) {
+    if (!$filter_format) {

We're now changing the behaviour of this function.

Previously, if we found the format, but it had no configured filters, we returned ''. Now we only return '' if the format doesn't exist.

I note however that the comment indicates it was supposed to be checking for the format, not the presence of filters on the format. So it is likely that the existing behaviour is unintended.

However, I think we should reinstate the previous behaviour.

So make the code

if (!$filter_format || !($filters = $filter_format->filters()) {

We can then remove the $filters = line.

It is unlikely that someone was relying on this behaviour, but we can't be sure, so we should err on the side of caution.

harsha012’s picture

Status: Needs work » Needs review
FileSize
1.52 KB
601 bytes

Improved the patch

sureshcj’s picture

FileSize
35.54 KB

Hi harsha012,
I have tested with above patch, which has posted in comment #21. The node creates and node view working fine.
But I got following error on the reports.
"Missing text format: filtered_html_invalid."

mohit1604’s picture

Issue summary: View changes

Fixed typo in issue summary.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

@sureshcj thanks for testing, that is expected and glad there is still reporting for that in the logs.

There is an extra space in the test that could be removed on commit.

Not a super big fan of assignments in conditionals but it was recommended by @larowlan in #20 so don't want to make a big fus there.

Thanks for everybody's help on this!

  • larowlan committed 72c76b7 on 8.5.x
    Issue #2359389 by harsha012, hgoto, jeqq, fago, sureshcj, larowlan: Call...
larowlan’s picture

Issue tags: +8.4.4 Commit freeze

@sureshcj that error is not related to this patch, but is the expected behaviour.

Whilst we normally don't give credit for screenshots unrelated to the patch, it does demonstrate that effort was spent manually testing the patch, so I'm going to add credit for @sureshcj in this instance.

for more information please see https://www.drupal.org/core/maintainers/issue-credit

Committed as 72c76b7 and pushed to 8.5.x.

Leaving at RTBC on 8.4.x.

Will cherry-pick to 8.4.x tomorrow after commit freeze for 8.4.4 is over.

larowlan’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -8.4.4 Commit freeze

Cherry-picked 8baec3f and pushed to 8.4.x

  • larowlan committed 8baec3f on 8.4.x
    Issue #2359389 by harsha012, hgoto, jeqq, fago, sureshcj, larowlan: Call...

  • larowlan committed 540b8df on 8.4.x
    Revert "Issue #2359389 by harsha012, hgoto, jeqq, fago, sureshcj,...
larowlan’s picture

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

As per https://www.drupal.org/core/release-cycle-overview#current-development-c... 8.4 is in 'critical fixes only', so I reverted this from 8.4

Status: Fixed » Closed (fixed)

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