1

I have a custom content type that contains an entity reference field (field_referencefield) and one more field as select box (field_triggerfield).

The values of field_referencefield should depend on the selection of field_triggerfield. This should happen in the create node form. I.e. If I change the selected item in field_triggerfield during node creation, different values for field_referencefield should be available. Maybe this can be achieved using views reference.

closed as unclear what you're asking by Kevin, Pierre.Vriens, Jdrupal, leymannx, mradcliffe Nov 8 at 20:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

Step 1

On the field definition for the reference field that is dependent on another field you are going to change the reference method to views

field edit screen

Step 2

  1. You'll need to create a reference view for this data.
  2. Make sure to add a contextual field/relationships/etc on the reference view that takes the other value from your form that helps it pre-filter the autocomplete results.

reference view

Step 3

in a module or wherever hook into hook_form_alter and replace the reference field


/**
 * implements hook_form_alter
 *
 * @param array $form
 * @param array $form_state
 * @param string $form_id
 */
function my_module_form_alter(&$form, $form_state, $form_id) {

  if (in_array($form_id, ['node_mycontenttype_form', 'node_mycontenttype_edit_form'])) {
    // Add a wrapper around the reference field.
    $form['field_referencefield']['#prefix'] = '<div id="some-wrapper">';
    $form['field_referencefield']['#suffix'] = '</div>';

    // Add an ajax callback that replaces the contents of the wrapper.
    $form['field_triggerfield']['widget'][0]['target_id']['#ajax'] = [
      'callback' => 'mymodule_field_mycontenttype_trigger',
      'wrapper' => 'some-wrapper',
      'event' => 'autcompletechange autocompleteclose',
      'progress' => [
        'type' => 'throbber',
        'message' => t('Fetching filtered content...'),
      ],
    ];

    $field_triggerfield_value = $form_state->getValue('field_triggerfield');
    if (!empty($field_triggerfield_value)) {
      $contextual_filter_value = $field_triggerfield_value[0]['target_id'];
      $form['field_referencefield']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][0] = $contextual_filter_value;
    }
  }
}

function mymodule_field_mycontenttype_trigger(array $form, $form_state) {
  return $form['field_referencefield'];
}

so what this is doing (I've assumed node edit form and node add form for a content type):

  1. putting a wrapper around the reference field whose available values should change
  2. adding an ajax callback to the trigger field that should cause the reference field to filter (the contextual field)
  3. when the trigger field changes
    • add a new contextual value to the view reference field's view from the trigger field
    • replace the contents of the wrapper field in the form
  • that seems to be the solution, although I don't exactly understand why it works. In the trigger function you are only returning the field_referencefield. I don't get why this is enough. Why are you not setting the view argument here? – Peter Nov 9 at 18:49
  • hook_form_alter happens every time the form is rendered. the callback triggers that hook, so adding the view arg there is sufficient. the wrapper argument says that whatever comes back from the callback get injected into that wrapper. we only want to return the contents of that wrapper (once the hook_form_alter has been run that is). – Francis Yaconiello Nov 10 at 23:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.