In an institutional website, there's an archive section listing all events (content type: Event). Each event is coordinated by an event manager (content type: Event Manager). CT Event has an entityreference field to select an Event Manager. The available options are defined by an entityreference view. This process works well so far.

Next, I needed to limit the available managers depending on their status. The status is a simple boolean field in the CT Event Manager defining if that manager is still active or retired. This limitation also works such as new events can't be assigned to retired managers.

However, if I want to edit an (old) event which was managed by a now retired manager, I can't save the node since the reference is not valid anymore.

My question is, how can I restrict the number of available managers for new events but still maintain the relations between old events and retired managers?

2 Answers 2


It can be done with a few short lines of code if you are prepared to write a custom module for it.

First, you need to alter your Entity Reference View. Add a Contextual filter of your boolean field that indicates whether the manager is active or not. Set it to 'Display all results for the specified field' WHEN THE FILTER VALUE IS NOT AVAILABLE.

Now note the machine name and display_id of your view, then create a custom module, and add these lines to it:

function MODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == "VIEW_MACHINE_NAME" && $display_id == "DISPLAY_ID") {
    $node = menu_get_object();
    if (!$node) {
      $args[] = 1;

What happens here is that we are checking if we are on a node page and have an existing node object. If we do, we assign an argument of '1' to the view, which is boolean yes, and the view subsequently only displays active managers. If we do not have a node object, then we are in the process of creating a new node, so we leave the argument empty, and the view has been set to then display all nodes.

  • Interesting approach. I will see if I can fit that into the system.
    – Paul
    Dec 9, 2014 at 16:09
  • Due to some other projects this got delayed but now I finally got around to try this approach. And it actually works. Kind of. Instead menu_get_object()(which returns nothing since the current object is the view) I used menu_get_item(). This array has another sub-array called page_arguments which includes the nid for existing nodes (edit mode) or is "NULL" for new nodes (add). - Very nice and sleek solution!
    – Paul
    Mar 10, 2015 at 14:37

You need to render views in the Entity Reference field.

  1. Add a reference type display in a view.
  2. Add a nid and title field in the view.
  3. A list of views will be displayed in the field configuration. Select specific view in field's configuration.

enter image description here


May be adding hook_form_alter to form can do the trick for you.

function hook_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'your_node_form') {
    // $form['#node'] if it is node edit form.
    // $form['reference_field']['#options'] = $altered_list
  • That's what I did, see my question: "The available options are defined by an entityreference view." - My question is how to limit the view for new entries only. I want to keep old (already stored) relationships even when editing old events.
    – Paul
    Dec 9, 2014 at 11:00
  • I dont think if its possible without altering the form. Dec 9, 2014 at 12:02
  • Checkout my updated answer. Dec 9, 2014 at 12:06
  • I was hoping to achieve that without adding code. I will take a look into form altering although I don't really want to alter the form. I would think, I just need to alter the validation criteria, sth like if (manager == retired && node[manager] == updated_node[manager]) { let it pass; }.
    – Paul
    Dec 9, 2014 at 16:07
  • I don't think if there is any solution other than custom code. If you just want to validate you can simply use hook_node_validate. Dec 10, 2014 at 4:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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