0

I need to understand how i can pass a special argument into a field entity reference filtred by a View to show specifics results. My view use contextual filter. My problem and how to anticipate the total number of items (references added with the button "Add an other element") , and to pass my argument with each new addition.

I can do it with limited number of items like that:

function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'node_page_edit_form':

        $form['field_references']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][] = $special_arg_id;
        $form['field_references']['widget'][1]['target_id']['#selection_settings']['view']['arguments'][] = $special_arg_id;
        $form['field_references']['widget'][2]['target_id']['#selection_settings']['view']['arguments'][] = $special_arg_id;
        $form['field_references']['widget'][3]['target_id']['#selection_settings']['view']['arguments'][] = $special_arg_id;
        $form['field_references']['widget'][4]['target_id']['#selection_settings']['view']['arguments'][] = $special_arg_id;
  }
}

enter image description here

  • how to anticipate the total number of items of what? – No Sssweat Oct 5 '17 at 8:59
  • to anticipate the total number of items of references added with the button "Add an other element" – florian_drupal Oct 5 '17 at 9:15
  • You can use tokens to pass the argument to the field which is an entity reference field – Shreya Shetty Oct 5 '17 at 9:18
  • I know, my argument is dependent on the value of a previous field, it is for this reason that I go through form_alter to add my argument programmatically. – florian_drupal Oct 5 '17 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.