Problem/Motivation

The ListInteger and Boolean fwa plugins are broken. I’m assuming others e.g. ListFloat are also broken. Something has changed in 1.2.x-dev around July 21st, maybe AutomatorBaseAction?

Sample error looks like this:

Error: Call to a member function get() on array in Drupal\ai_automators\Plugin\FieldWidgetAction\Boolean->saveFormValues() (line 49 of /var/www/html/web/modules/contrib/ai/modules/ai_automators/src/Plugin/FieldWidgetAction/Boolean.php)

Steps to reproduce (required for bugs, but not feature requests)

- Enable AI Automator on a boolean field;
- Choose AI Automator Type » LLM Boolean;
- Configure AI Automator Settings;
- Go to e.g. admin/structure/types/manage/page/form-display
- Configure FWA on boolean field
- Edit a Page node and click on the fwa button

Issue fork ai-3538762

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

bisonbleu created an issue. See original summary.

anjaliprasannan’s picture

Assigned: Unassigned » anjaliprasannan

anjaliprasannan’s picture

Assigned: anjaliprasannan » Unassigned
Status: Active » Needs review
bisonbleu’s picture

Thanks for this @anjaliprasannan. That said, your fix is at the plugin level, not upstream which is where, I think, @Marcus_Johansson was alluding to when he wrote «I know where this comes from» in Slack. The issue description also mentions that this affects other plugins e.g. ListFloat, etc.

Perhaps @Marcus_Johansson can provide more details about what he meant by «I know where this comes from»…?

anjaliprasannan’s picture

@bisonbleu

I am trying to improve the saveFormValues() in AutomatorBaseAction like,

protected function saveFormValues(array &$form, string $form_key, $entity, ?int $key = NULL): array {

    if (is_null($key)) {
      // If not key is provided, we should iterate through all items.
      foreach ($entity->get($form_key) as $index => $item) {
        $value = NULL;
        if (is_array($item)) {
          if (isset($item[$this->formElementProperty])) {
            $value = $item[$this->formElementProperty];
          }
        }
        elseif (is_object($item) && method_exists($item, 'get')) {
          if ($item->get($this->formElementProperty)) {
            $value = $item->get($this->formElementProperty)->getValue();
          }
        }
        if ($value !== NULL) {
          $form[$form_key]['widget'][$index][$this->formElementProperty]['#value'] = $value;
        }
      }
    }
    else {
      if (isset($entity->get($form_key)[$key])) {
        $item = NULL;
        foreach ($entity->get($form_key) as $index => $item) {
          if ($index === $key) {
            break;
          }
        }
        $value = NULL;
        if (is_array($item)) {
          if (isset($item[$this->formElementProperty])) {
            $value = $item[$this->formElementProperty];
          }
        }
        elseif (is_object($item) && method_exists($item, 'get')) {
          if ($item->get($this->formElementProperty)) {
            $value = $item->get($this->formElementProperty)->getValue();
          }
        }
        if ($value !== NULL) {
          $form[$form_key]['widget'][$key][$this->formElementProperty]['#value'] = $value;
        }
      }
    }

    return $form[$form_key];
  }

the method now correctly handles both array and object field items. This means that most plugin-level overrides of saveFormValues() are no longer necessary unless they add widget-specific logic.

I recommend removing the redundant saveFormValues() methods from individual FieldWidgetAction plugins and letting them inherit the base implementation. If that is allowed I can move further.

Thanks

marcus_johansson’s picture

Component: Field Widget Actions » AI Automators
marcus_johansson’s picture

Assigned: Unassigned » marcus_johansson
marcus_johansson’s picture

Assigned: marcus_johansson » Unassigned
Status: Needs review » Reviewed & tested by the community

Looks good and works well.

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

  • fago committed aede9249 on 1.2.x authored by anjaliprasannan
    fix: #3538762 FWA plugin error: Call to a member function get() on array...

  • fago committed ac213d18 on 1.3.x authored by anjaliprasannan
    fix: #3538762 FWA plugin error: Call to a member function get() on array...

  • fago committed 1cde9df2 on 2.0.x authored by anjaliprasannan
    fix: #3538762 FWA plugin error: Call to a member function get() on array...
fago’s picture

Status: Reviewed & tested by the community » Fixed

Re-checked ci-pipeline, all good now, thus merged.

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.