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
- Enable LMS and LMS Answer Plugins.
- Create a Free Text activity.
- Add at least one question.
- Configure the activity to use a text format, such as basic_html.
- Add the activity to a lesson and course.
- 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->executeInRenderContexProposed 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
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
Comment #5
graber commentedThanks, just converted that to a one-liner.