Drupal: "Error: Class not found" when calling a function from a controller within submitForm()

Drupal: "Error: Class not found" when calling a function from a controller within submitForm()

我正在尝试在自定义模块中提交表单时做一些事情。其中一些是通过从控制器调用函数来完成的。那是我得到的时候:

Error: Class 'Drupal\ice_cream\Controller\OrderController' not found in Drupal\ice_cream\Form\OrderForm->submitForm() (line 77 of modules\custom\ice_cream\src\Form\OrderForm.php).

据我所知命名空间没有错?还是与此错误无关?

这是我的 OrderForm.php 和 submitForm() 的样子:

<?php

namespace Drupal\ice_cream\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ice_cream\Controller\OrderController;

/**
 * Implements the order form.
 */
class OrderForm extends FormBase {

... (omitted code for getFormid and buildForm)

    public function submitForm(array &$form, FormStateInterface $form_state) {

        //Check if the order is ice or waffles.   
        if($form_state->getValue('foodType') == 'ice'){
          //Save order to the DB.
          OrderController::saveOrder($form_state->getValue('foodType'), $form_state->getValue('taste'));

          ... (more code)
        }

    }
}

控制器是这样的:

<?php

namespace Drupal\ice_cream\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;

/**
 * Order controller for interacting (insert, select,...) with the ice cream table in the DB.
 */
class OrderController extends ControllerBase {

  /**
   * Saves an order for either Ice or Waffles with their options (tast or toppings).
   */
  public function saveOrder($foodType, $options) {

    $connection = Database::getConnection();

    //Check if ice or waffles (to only insert the right field with $options).
    if($foodType == "ice"){
        $result = $connection->insert('ice_cream')
        ->fields([
            'foodType' => $foodType,
            'taste' => $options,
            'toppings' => "",
        ])
        ->execute();
        return true;

    }elseif($foodType == "waffles"){
        $result = $connection->insert('ice_cream')
        ->fields([
            'foodType' => $foodType,
            'taste' => "",
            'toppings' => $options,
        ])
        ->execute();
        return true;

    }

  }

}

尝试使用以下代码:

$obj= new OrderController;
$obj->saveOrder($form_state->getValue('foodType'), $form_state->getValue('taste'));

已解决:

刚和我的导师一起解决了。代码或多或少是正确的,仍然需要在 OrderController 中使我的函数静态化,而且我犯了一个愚蠢的错误,当我用 'touch' 终端创建文件名时忘记了文件名中的 .php 扩展名命令...