Work Log content creation code update to with other themes

Created on 5 December 2024, about 1 month ago

Problem/Motivation

At times, the "Work Log" logging tabs content have not been working while trying to add data. After some investigation, it seems that the jQuery wasn't firing against the correct form elements, which would cause user experience degradation and the inability for the form to collect data from the forms.

Possible solution that worked locally on my test environment was adding classes to the form element attribute array. As the example below.

web/modules/contrib/burndown/src/Form/TaskForm.php
      '#attributes' => [
          'class' => ['add_work'], 

        ],
      ];
      $form['log']['work']['body'] = [
        '#type' => 'textarea',
        '#title' => $this->t('Comment'),
        '#attributes' => [
          'class' => ['add_work_text'], <--- updated
        ],
      ];
      $form['log']['work']['quantity'] = [
        '#type' => 'number',
        '#title' => $this->t('Time'),
        '#min' => 0,
        '#default_value' => 0,
        '#attributes' => [
           'class' => ['add_work_quantity'], <--- updated
        ],
      ];
      $form['log']['work']['quantity_type'] = [
        '#type' => 'select',
        '#options' => [
          'm' => $this->t('Minutes'),
          'h' => $this->t('Hours'),
          'd' => $this->t('Days'),
        ],
        '#default_value' => 'h',
        '#attributes' => [
           'class' => ['add_work_quantity_type'], <--- updated
        ],

By updating the jQuery so that it's more closely bound to the form class element, as shown in the example below, you increase the chance of successfully collecting the data for an AJAX call back to the API.

web/modules/contrib/burndown/js/burndown.task_edit.js

       // Post data.
          $.ajax({
              url: "/burndown/api/task/add_work",
              method :'POST',
              data: {
                ticket_id: $('#burndown_task_log').data('ticket-id'),
                comment: $('.add_work .add_work_text').val(), <--- updated 
                work: $('.add_work .add_work_quantity').val(), <---- updated
                work_increment: $('.add_work .add_work_quantity_type').val() <--- updated
              },
              success: function (result) {
                // On success, reload work and clear the form.
                update_log('work');
                $('.add_work .add_work_text').val('');  <--- updated
                $('.add_work .add_work_quantity').val(''); <--- updated
                $('.add_work .add_work_quantity_type').val('h'); <--- updated
              },
              error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log("Could not post comment.");
              }

Once the data was collected via jQuery, the Burndown controller would return an error and wouldn't save the data collected from the form. The controller method was static. Below is the code that was altered to resolve the problem.

public function addWork(Request $request) {
// public static function addWork(Request $request) {
......

    // Instantiate our event.
    // $event = new TaskWorkEvent($task, $filtered_comment, $work_done, $uid);

    // Dispatch the event.
    //static::getContainer()->eventDispatcher->dispatch(TaskWorkEvent::WORKED, $event);
🐛 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
  • 🇨🇦Canada jeremylichtman

    Added to 1.0.51.

  • 🇦🇺Australia purencool

    Hey the following need be removed or commented out as it's throwing a 500. What is they do?

        // Instantiate our event.
       // $event = new TaskWorkEvent($task, $filtered_comment, $work_done, $uid);
    
        // Dispatch the event.
        //static::getContainer()->eventDispatcher->dispatch(TaskWorkEvent::WORKED, $event);
    
  • 🇨🇦Canada jeremylichtman

    Can you please check your Drupal log to see what the error is? I won't have a chance to test this until probably next week...

    These a) actually add the work record that the user entered in the form, and b) dispatch an event that other modules could use.

  • 🇨🇦Canada jeremylichtman

    This should work:

    $this->eventDispatcher->dispatch->(TaskWorkEvent::WORKED, $event);
    
  • 🇦🇺Australia purencool

    Changed the code to the following

     // Instantiate our event.
        $event = new TaskWorkEvent($task, $filtered_comment, $work_done, $uid);
    
        // Dispatch the event.
        $this->eventDispatcher->dispatch(TaskWorkEvent::WORKED, $event);
    

    Got the following error

    TypeError: Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::dispatch(): Argument #1 ($event) must be of type object, string given, called in /var/www/html/web/modules/contrib/burndown/src/Controller/TaskController.php on line 233 in Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (line 89 of /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php).
    
  • 🇦🇺Australia purencool

    This worked.

      // Instantiate our event.
        $event = new TaskWorkEvent($task, $filtered_comment, $work_done, $uid);
    
        // Dispatch the event.
        $this->eventDispatcher->dispatch( $event,TaskWorkEvent::WORKED);
    
  • 🇦🇺Australia purencool

    Interesting the class has the following logic.

    class TaskWorkEvent extends Event {
    
      const WORKED = 'burndown_event_task_work';
    
      /**
       * The task.
       *
       * @var Drupal\Core\Entity\EntityInterface
       */
      public $task;
    
      /**
       * The comment.
       *
       * @var string
       */
      public $comment;
    
      /**
       * The amount of work done.
       *
       * @var string
       */
      public $workDone;
    
      /**
       * The user who did the work.
       *
       * @var int
       */
      public $uid;
    
      /**
       * Constructs the object.
       *
       * @param Drupal\Core\Entity\EntityInterface $task
       *   The newly created task.
       * @param string $comment
       *   The comment.
       * @param string $workDone
       *   The mount of work done.
       * @param int $uid
       *   The id of the user who did the work.
       *
       * @todo Provide more detailed/explanatory comments on parameters.
       */
      public function __construct(EntityInterface $task, $comment, $workDone, $uid) {
        // $created, $uid,
        $this->task = $task;
        $this->comment = $comment;
        $this->workDone = $workDone;
        $this->uid = $uid;
      }
    
    }
    
    
  • 🇨🇦Canada jeremylichtman

    Ah. There's other event dispatches in the controller.

    I've pushed the fix (with your reordering) to dev.

Production build 0.71.5 2024