Sometimes it is needed that the referenced entities depend on the referencing entity.
for this a (optional) views argument is needed. (besides tha "already referenced entities" argument)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

geek-merlin’s picture

if my current project demands it i might implement it and roll a patch.

for this we need to decide the order of arguments and hom optional.

what makes sense to me:
* rename option "pass argument" to "pass selected entities as views argument"
* add option "pass referencing entity as views argument"
* i'm not totally decided on the order of the arguments, but my gut feeling is that the new argument "referencing entity" should be first.
as both arguments can be switched on separately legacy views will not break.

stella’s picture

Status: Active » Needs review
FileSize
1.35 KB

I've a similar but different issue - I wanted to pass in additional arguments from another form field value on the form (only on edit). To do this I just added in a call to drupal_alter() on 'entityreference_view_widget_views_arguments' and in my custom module then called hook_entityreference_view_widget_views_arguments_alter() to modify the arguments to the View. You might be able to use it or extend it for what you're looking for.

See attached patch.

tedbow’s picture

I have found this issue because I have similar need as @axel.rutz

@stella I like your idea for the patch. I uploaded a version with 1 small change. I called the drupal_alter after the logic for added the referenced entities as the first argument.

The problem I see with calling the drupal_alter before is that
$arguments[] = implode(',', $selected_entity_ids);
Will add the argument after other arguments that are added in the alter hook. This conflicts with the description on the front end.

With the alter hook being after the hook code can react to whether they the first argument is supplied or not.

Here is the implementation that I needed to pass the Current nid as the second argument.

function modulename_entityreference_view_widget_views_arguments_alter(&$arguments, $context) {
  $node = $context['element']['#entity'];
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }
  if (!empty($node) && !empty($node->nid)) {
    $arguments[] = $node->nid;
  }
}

This way if the nid is available it is always the second argument and by passing 'all' as the first argument the View can will work even if there are no currently selected entities.

tedbow’s picture

I started another issue to add this hook #1978084: Provide alter hook for view arguments

bojanz’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Issue summary: View changes
Status: Needs review » Needs work

This is a reasonable request.
1.x branch is frozen and won't be getting further non-critical commits.
Hence, moving the issue to 2.x and marking "needs work" for a reroll.

daulet2030’s picture

Status: Needs work » Needs review
FileSize
1.16 KB

Re-rolled previous patch for 7.x-2.x
After you apply this patch you need to create your own module which will call hook_entityreference_view_widget_views_arguments_alter(&$arguments, $form_state), and add your arguments to $arguments array based on information in $form_state variable. And also update your view to accept this arguments.
For example:

<?php


function filter_player_entityreference_view_widget_views_arguments_alter(&$arguments, $form_state) {
  $parent = $form_state['clicked_button']['#parents']['0'];
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }
  if (!empty($parent)) {
    if($parent == 'field_squad_1_list' && !empty($form_state['values']['field_team1']['und'])){
		$arguments[] = $form_state['values']['field_team1']['und'];
	} else 
	if($parent == 'field_squad_2_list' && !empty($form_state['values']['field_team2']['und'])){
		$arguments[] = $form_state['values']['field_team2']['und'];
	}
	
  }
}

Note: Previous patch used $context variable instead of $form_state

jsacksick’s picture

Status: Needs review » Needs work

Hi, thanks for the patch, however, could you fix the space issues (2 spaces instead of 4) and could you also create a .api.php file to document the alter hook?

daulet2030’s picture

Number #4 might be a better candidate for re-roll, it also supports field collections from my understanding.
As requested here's re-rolled patch and api.php. Please note that I am new to patching and documentation :)

<?php

/**
 * @file
 * Hooks provided by Entity Reference View Widget.
 */

/**
 * Alter arguments passed to entity reference view based on edit form
 *
 * Sometimes you need to filter your entity reference view based on form
 * your editing (based on referencing entity). For this task you need to
 * alter arguments passed (besides the "already referenced entities" 
 * argument) to entity reference view. This hook allows you to do that.
 *
 * @param $arguments
 *  Arguments to be altered
 * @param $form_state
 *  Form state. Contains fields based on which you may want to alter your view arguments
 */
function hook_entityreference_view_widget_views_arguments_alter(&$arguments, $form_state) {
  if (empty($arguments)) {
	// Make sure that our alteration always come after and will work even if there are no currently selected entities.
    $arguments[0] = 'all';
  }
  // An example of additional arguments based on selected team
  // Select element of the form
  $parent = $form_state['clicked_button']['#parents']['0'];
  if (!empty($parent)) {
    // If "field_squad_1_list" entity reference button is pressed
    if($parent == 'field_squad_1_list' && !empty($form_state['values']['field_team1']['und'])){
	  // Then pass the id of the team 1, based on which referenced players will be filtered
	  $arguments[] = $form_state['values']['field_team1']['und'];
	}
	// Else if "field_squad_2_list" entity reference button is pressed
	else if($parent == 'field_squad_2_list' && !empty($form_state['values']['field_team2']['und'])){
	  // Then pass the id of the team 2, based on which referenced players will be filtered
	  $arguments[] = $form_state['values']['field_team2']['und'];
	}
  }
}
jsacksick’s picture

Status: Needs work » Fixed

Thanks for your patch, I committed a slightly different patch.

  • jsacksick committed 576909c on 7.x-2.x authored by daulet2030
    Issue #1626668 by daulet2030, tedbow, stella: Provide referencing entity...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

arzuga’s picture

Why this feature is not available in last version?