Deprecated function: str_replace(): Passing null

Created on 28 November 2024, 2 months ago

Problem/Motivation

On a fresh install of the Burndown module, when using a testing comment under a task provided by the module, the comment won't save when using PHP 8.3 or PHP 8.2. It just keeps getting the following error. Does anyone have any suggestions how to solve this issue?

Deprecated function: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in Drupal\Component\Utility\Xss::filter() (line 69 of /var/www/html/web/core/lib/Drupal/Component/Utility/Xss.php)
#0 /var/www/html/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real()
#1 [internal function]: _drupal_error_handler()
#2 /var/www/html/web/core/lib/Drupal/Component/Utility/Xss.php(69): str_replace()
#3 /var/www/html/web/modules/contrib/burndown/src/Controller/TaskController.php(163): Drupal\Component\Utility\Xss::filter()
#4 [internal function]: Drupal\burndown\Controller\TaskController->addComment()
#5 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): 

Drupal version.

Drupal version   : 10.3.10
......
DB name          : services
Database         : Connected
Drupal bootstrap : Successful
Default theme    : claro
Admin theme      : claro
PHP binary       : /usr/bin/php8.3
PHP config       : /etc/php/8.3/cli/php.ini
......

Burndown version.

  Burndown     Burndown (burndown)           Enabled    1.0.43
🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇦🇺Australia purencool

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @purencool
  • 🇮🇳India arunsahijpal

    Hey @purencool,
    could you please tell steps reproduce this issue.

  • 🇨🇦Canada jeremylichtman

    The Xss:filter() error can only happen if the string being passed into it is null.

    Looking at the code in burndown/src/Controller/TaskController.php(163):

    public function addComment(Request $request) {
        // Get data from request (validated below).
        $ticket_id = $request->request->get('ticket_id');
        $comment = $request->request->get('comment');
    
        // Load the task.
        $task = Task::loadFromTicketId($ticket_id);
        if ($task === FALSE) {
          // Task doesn't exist; throw 404.
          throw new NotFoundHttpException();
        }
    
        // Add comment to the log.
        $type = 'comment';
        $filtered_comment = Xss::filter($comment);
    ...
    

    This can only happen if "$comment = $request->request->get('comment');" is producing a null value.

    This is called via a POST:

    burndown.task_add_comment:
      path: '/burndown/api/task/add_comment'
      defaults:
        _controller: '\Drupal\burndown\Controller\TaskController::addComment'
      methods:  [POST]
      requirements:
        _permission: 'burndown comment on task'
    

    and the POST, in turn is called via burndown\js\burndown.task_edit.js.

    // Posting a comment.
          $(once('postCommentAction','body'))
            .on('click', '.add_comment a.button', function (e) {
              // Do not follow the link.
              e.preventDefault();
              e.stopPropagation();
    
              // Post data.
              $.ajax({
                  url: "/burndown/api/task/add_comment",
                  method :'POST',
                  data: {
                    ticket_id: $('#burndown_task_log').data('ticket-id'),
                    comment: $('.add_comment .form-item-body textarea').val()
                  },
                  success: function (result) {
                    // On success, reload comments and clear the form.
                    update_log('comment');
                    $('.add_comment .form-item-body textarea').val('');
                  },
                  error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log("Could not post comment.");
                  }
              });
            });
    

    You may want to open the console log (and maybe try a js breakpoint) to see what is going on. This sounds like it could be browser-related though, rather than server-side.

  • 🇦🇺Australia purencool

    Thanks @jeremylichtman,

    I will look into your frontend suggestion to see if what is causing the problem.

  • 🇦🇺Australia purencool

    It seems to be a html class issue. Claro is rendering the html class .form-item--body, while jQuery is looking for a .form-item-body class.

    Code that fails.

              $.ajax({
                  url: "/burndown/api/task/add_comment",
                  method :'POST',
                  data: {
                    ticket_id: $('#burndown_task_log').data('ticket-id'),
                    comment: $('.add_comment .form-item-body textarea').val()
                  },
    

    Code that works.

              // Post data.
              $.ajax({
                  url: "/burndown/api/task/add_comment",
                  method :'POST',
                  data: {
                    ticket_id: $('#burndown_task_log').data('ticket-id'),
                    comment: $('.add_comment .form-item--body textarea').val()
                  },
    

    Possible solution could be.

                  data: {
                    ticket_id: $('#burndown_task_log').data('ticket-id'),
                    comment: $('.add_comment textarea').val()
                  },
    

  • 🇨🇦Canada jeremylichtman

    Ah. You're using a different theme.

    I think the correct solution is to push a custom class onto that field, and target that instead.

    Note that there's probably other instances of this. The whole thing is full of AJAX calls of this sort, and it was built against the custom theme. I think I have an old todo somewhere regarding the possibility of this happening.

  • 🇨🇦Canada jeremylichtman

    Ok. Fixed and added tag 1.0.45.

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

Production build 0.71.5 2024