Problem/Motivation

Testing out navigation in some LMS course content, I got a WSOD when clicking on a free-text activity.

  • Drupal Core: 10.6.17
  • Drupal LMS: 1.2.3
  • PHP: 8.3.x
  • Affected plugin: lms_answer_plugins Free Text activity

Steps to reproduce

  1. Enable LMS and LMS Answer Plugins.
  2. Create a Free Text activity.
  3. Add at least one question.
  4. Configure the activity to use a text format, such as basic_html.
  5. Add the activity to a lesson and course.
  6. As a learner who has not answered the question, open the activity from course nav block.

Actual results

The activity page crashes with:

TypeError: strlen(): Argument #1 ($string) must be of type string, array given

Stack trace:

TypeError: strlen(): Argument #1 ($string) must be of type string, array given in strlen() (line 395 of core/lib/
  Drupal/Component/Utility/Unicode.php).
  Drupal\Component\Utility\Unicode::validateUtf8() (Line: 65)
  Drupal\Component\Utility\Xss::filter() (Line: 70)
  Drupal\editor\EditorXssFilter\Standard::filterXss() (Line: 354)
  editor_filter_xss() (Line: 109)
  Drupal\editor\Element->preRenderTextFormat()
  call_user_func_array() (Line: 113)
  Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 886)
  Drupal\Core\Render\Renderer->doCallback() (Line: 431)
  Drupal\Core\Render\Renderer->doRender() (Line: 503)
  Drupal\Core\Render\Renderer->doRender() (Line: 503)
  Drupal\Core\Render\Renderer->doRender() (Line: 503)
  Drupal\Core\Render\Renderer->doRender() (Line: 248)
  Drupal\Core\Render\Renderer->render() (Line: 238)
  Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 637)
  Drupal\Core\Render\Renderer->executeInRenderContex

Proposed resolution

The error is coming from line 50 of
modules/lms_answer_plugins/src/Plugin/ActivityAnswer/FreeText.php

'#default_value' => $data['answer'][$delta] ?? [],

which makes it an empty Array if the value doesn't exist already.

Suggest normalizing the saved answer into a string before building the element:

$default_value = $data['answer'][$delta] ?? '';
if (\is_array($default_value)) {
$default_value = $default_value['value'] ?? '';
}
$element = [
'#title' => $question_item->getValue()['value'],
'#type' => $text_format === NULL ? 'textarea' : 'text_format',
'#default_value' => $default_value,
];

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork lms-3625825

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

leeksoup created an issue. See original summary.

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

graber’s picture

Version: 1.2.3 » 1.2.x-dev
Status: Active » Fixed

Thanks, just converted that to a one-liner.

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.