1

I have a content type with a entity reference to another content type/view. The current possible relations could be 1,2,3 .... until 10 or unlimited. These are given in the field attributes screens.

I need 20. After that the user is not allowed to add another field in the gui.

Any solutions how to do this?

1

You should select unlimited as possible relations. Then, using a custom module, you can alter the node edit form to don't allow more than 20 items.

To do this, implement hook form alter:

function mymodule_form_node_form_alter(&$form, &$form_state, $form_id) {
  // do magic.
}

Inside you have to:

  • Check the if the relations field has 20 item; if yes delete the element widget that adds another one (so user don't get confused because it seems more items can be added).

  • Add a new validate handler to check related items are less than 21.

  • I add dpm($form) in the hook to investigate. However when pushing add item the form is not refreshing (problably ajax processed?). So then this will not work is it? – Justme Mar 14 '17 at 10:20
  • The form workflow is complex, more when AJAX is involved. But form should be rebuilt upon clicking on add button, and all form alters should called. Don't trus dpm() in ajax calls, sometimes dpm output is lost. Use xdebug (proces debugging) or use dd function. dd() is like dpm but writes ouptut to drupal_debug.txt file in the tmp drupal directory (defined in media config page). – sanzante Mar 14 '17 at 10:25
  • works perfect thxs – Justme Mar 14 '17 at 18:05

Your Answer

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

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