Drupal 8 - entity_autocomplete 的自定义选择处理程序导致无限加载
Drupal 8 - custom Selection Handler for entity_autocomplete causes indefinite loading
由于项目在最后一刻发生了一些变化,我正在尝试更改自动完成字段的结果,例如删除某些已标记有名称为 "Empty" 的分类术语的节点。
我已经成功创建了自己的选择处理程序,并且在我的自定义表单上调用了它。
在我的选择处理程序中,我使用 entityQueryAlter
方法向查询添加条件:
class MyCustomSelection extends NodeSelection {
public function entityQueryAlter(SelectInterface $query) {
parent::entityQueryAlter($query);
//Get the tid for the term which was used to tag nodes which should be EXCLUDED from the results
$emptyTid = array_values(\Drupal::entityQuery('taxonomy_term')
->condition('name', 'Empty')
->execute())[0];
if ($emptyTid != null && $emptyTid > -1) {
//Get nids for nodes which were tagged with this term
$excludeQuery = db_query('
SELECT entity_id
FROM node__field_custom_tags
WHERE field_custom_tags_target_id = :tid',
array(
':tid' => $emptyTid
)
)->fetchAllKeyed(0, 0);
//Reset array keys
$result = array_values($excludeQuery);
//Typecast all the values to int, just to be safe
$typecastedResult = array();
foreach ($result as $k => $v) {
$typecastedResult[] = (int)$v;
}
//Add a condition to the query such as to exclude the nodes found above
$query->condition('entity_id', $typecastedResult, 'NOT IN');
}
return $query;
}
我知道使用 db_select
而不是 db_query
被认为是最佳实践,但上述方法返回了我需要的结果并且一切似乎都按预期工作,除了事实上,只有包含 $query->condition
行,我的自动完成字段才会无限期加载:
$query->condition('entity_id', $typecastedResult, 'NOT IN');
$typecastedResult
中的值相当于以下数组:
Array
(
[0] => 100
[1] => 101
etc...
)
有人可以提供一些关于为什么自动完成字段会无限期加载而没有结果的见解吗?再一次,是新条件的添加导致了这种行为。
我刚刚解决了这个问题。出于某种原因,我没有生成错误日志。解决了这个问题后,问题就很简单了 => "entity_id" 是错误的,因为我们正在查询 node_field_data table。正确的字段是 node_field_data.nid。大多数问题都是由于简单的错误:) .. 多么尴尬
由于项目在最后一刻发生了一些变化,我正在尝试更改自动完成字段的结果,例如删除某些已标记有名称为 "Empty" 的分类术语的节点。
我已经成功创建了自己的选择处理程序,并且在我的自定义表单上调用了它。
在我的选择处理程序中,我使用 entityQueryAlter
方法向查询添加条件:
class MyCustomSelection extends NodeSelection {
public function entityQueryAlter(SelectInterface $query) {
parent::entityQueryAlter($query);
//Get the tid for the term which was used to tag nodes which should be EXCLUDED from the results
$emptyTid = array_values(\Drupal::entityQuery('taxonomy_term')
->condition('name', 'Empty')
->execute())[0];
if ($emptyTid != null && $emptyTid > -1) {
//Get nids for nodes which were tagged with this term
$excludeQuery = db_query('
SELECT entity_id
FROM node__field_custom_tags
WHERE field_custom_tags_target_id = :tid',
array(
':tid' => $emptyTid
)
)->fetchAllKeyed(0, 0);
//Reset array keys
$result = array_values($excludeQuery);
//Typecast all the values to int, just to be safe
$typecastedResult = array();
foreach ($result as $k => $v) {
$typecastedResult[] = (int)$v;
}
//Add a condition to the query such as to exclude the nodes found above
$query->condition('entity_id', $typecastedResult, 'NOT IN');
}
return $query;
}
我知道使用 db_select
而不是 db_query
被认为是最佳实践,但上述方法返回了我需要的结果并且一切似乎都按预期工作,除了事实上,只有包含 $query->condition
行,我的自动完成字段才会无限期加载:
$query->condition('entity_id', $typecastedResult, 'NOT IN');
$typecastedResult
中的值相当于以下数组:
Array
(
[0] => 100
[1] => 101
etc...
)
有人可以提供一些关于为什么自动完成字段会无限期加载而没有结果的见解吗?再一次,是新条件的添加导致了这种行为。
我刚刚解决了这个问题。出于某种原因,我没有生成错误日志。解决了这个问题后,问题就很简单了 => "entity_id" 是错误的,因为我们正在查询 node_field_data table。正确的字段是 node_field_data.nid。大多数问题都是由于简单的错误:) .. 多么尴尬