任何人都可以帮我解决我的错误吗?服务器返回内部服务器错误500.它看起来我无法获得控制器。任何人都可以解释我的错误在哪里?我认为这是在路由自动完成路径,但我不确定。如果是我该如何解决? 非常感谢,伙计们
我的表单自动填充表单元素:
$form['field_father'] = array(
'#title' => t('Father'),
'#type' => 'textfield',
'#autocomplete_route_name' => 'cows.autocomplete',
'#autocomplete_route_parameters' => array(),
);
模块路由文件:
cows.form:
path: '/cows/add'
defaults:
_title: 'Add a cow'
_form: '\Drupal\cows\Form\CowsForm'
requirements:
_permission: 'access content'
cows.autocomplete:
path: '/cows/autocomplete'
defaults:
_controller: '\Drupal\cows\Controller\CowsController::autocomplete'
requirements:
_permission: 'access content'
控制器代码:
namespace Drupal\cows\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
class CowsController extends ControllerBase
{
public function autocomplete(Request $request)
{
$matches = [];
$string = $request->query->get('q');
$query = \Drupal::database()->select('node__field_name', 'fn');
$query->fields('fn', ['field_name_value', 'entity_id']);
$query->addField('fg', 'field_gender_value');
$query->join('node__field_gender', 'fg', 'fg.entity_id = fn.entity_id ');
$query->condition('fn.field_name_value', '%' . $string . '%', 'LIKE');
$query->condition('fg.field_gender_value', array('bull','ox'), 'IN');
$result = $query->execute();
foreach ($result as $row) {
$matches[] = ['value' => $row->entity_id, 'label' => $row->field_name_value];
}
return new JsonResponse($matches);
}
}
非常感谢
答案 0 :(得分:0)
使用命名空间来支持Request $请求。
namespace Drupal\cows\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/****this is newly added****/
use Symfony\Component\HttpFoundation\Request;
class CowsController extends ControllerBase
{
public function autocomplete(Request $request)
{
$matches = [];
$string = $request->query->get('q');
$query = \Drupal::database()->select('node__field_name', 'fn');
$query->fields('fn', ['field_name_value', 'entity_id']);
$query->addField('fg', 'field_gender_value');
$query->join('node__field_gender', 'fg', 'fg.entity_id = fn.entity_id ');
$query->condition('fn.field_name_value', '%' . $string . '%', 'LIKE');
$query->condition('fg.field_gender_value', array('bull','ox'), 'IN');
$result = $query->execute();
foreach ($result as $row) {
$matches[] = ['value' => $row->entity_id, 'label' => $row->field_name_value];
}
return new JsonResponse($matches);
}
}
答案 1 :(得分:0)
真的很有效。所以问题解决了。 我只是想重复一遍 - 我错过了添加Request命名空间:
使用Symfony \ Component \ HttpFoundation \ Request;
谢谢,@ vishwa!