使用参考字段中的动态参数过滤实体参考视图

时间:2018-08-17 15:55:39

标签: drupal drupal-8

我需要通过在我的内容类型中传递“引用字段”中的参数来过滤“实体引用视图”自动完成小部件。

Autocomplete by URL

我对此进行了研究,发现PHP代码类型的上下文过滤器是实现此目的的最建议的方法,但是由于PHP代码现在已从Drupal 8 Core中删除,因此该通用用例有哪些替代方案? (较早的建议使用PHP代码:Drupal 7: how to filter view content (with entity reference field) based on current page content

在编辑参考字段时,似乎在“查看参数”字段中仅提及硬编码的值,并且没有实现此目的的动态方法(例如,通过URL /查询字符串等)。

View Arguments field

1 个答案:

答案 0 :(得分:0)

这很hacky,但是您可以使用hook_form_alter。这是将当前NID作为参数传递的示例:

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) 
{

  if ($form_id == 'node_NODETYPE_edit_form') {
    $node = $form_state->getFormObject()->getEntity();
    $form['field_MY_REF_FIELD']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][0] = $node->id();
  }
}