Hi,
is it possible to show (as checked) already selected items in the modal view?

Thanks,
Anna

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

annared created an issue. See original summary.

TD44’s picture

Yes, i need too.

14 000 guys are using this module and only 2 need this feature xD

Ace Cooper’s picture

Version: 7.x-2.0-rc7 » 7.x-2.x-dev
Assigned: Unassigned » Ace Cooper
Category: Support request » Feature request
Status: Active » Needs review
FileSize
831 bytes

If you want all the previously selected items of Entityreference field to be checked in the Modal window:

1) In Views - add the contextual filter Global: Null into your Entityreference Views Widget display used by your field. (turns out - it's not required, arguments are passed from field to the view even without a contextual filter)

2) In Entityreference field settings - make sure to tick the checkbox "Pass selected entity ids to View" (enabled/true).

3) Use the patch or manually find the following lines inside entityreference_view_widget/views/entityreference_view_widget_handler_field_checkbox.inc :

$form[$this->options['id']][$row_index] = array(
  '#type' => 'checkbox',
  '#title' => check_plain(entity_label($this->entity_type, $entity)),
  '#title_display' => $this->options['ervw']['label_display'],
  '#return_value' => $entity_id,
  '#default_value' => FALSE,
  '#attributes' => array('class' => array('entity-reference-view-widget-select')),
);

and replace only a single line '#default_value':

$form[$this->options['id']][$row_index] = array(
  '#type' => 'checkbox',
  '#title' => check_plain(entity_label($this->entity_type, $entity)),
  '#title_display' => $this->options['ervw']['label_display'],
  '#return_value' => $entity_id,
  '#default_value' => in_array($entity_id,explode('+',$this->view->args[0])),
  '#attributes' => array('class' => array('entity-reference-view-widget-select')),
);

The code in_array($entity_id,explode('+',$this->view->args[0])) checks if the ID of each checkbox exists in the array of previously selected entity IDs. The Entityreference Views Widget passed those IDs to the view as a single string within $this->view->args[0], concatenated with +. E.g.: 123+234+345, etc.

Help yourself to the patch attached.
This turned out so simple - I am disconcerted it took me 2 hours to debug...
I'm eating a cookie for this one.

UPD1. What I found wrong with the ERVW behaviour in general - you can see with this patch the Items you checked before, but if you unchecked any previously selected items and submit the modal - these unchecked items don't get removed and remain in the entityreference field. I suggest we take a look at #2771357: Support a "replace" workflow (where a modal submission overwrites all items) for rewriting the entityreference field values every time the modal is submitted.

UPD2. The patches from both issues work fine and don't interfere with each other. But in my case the Entityreference View displayed only first 20 of 85000+ entities with a pager below. If I apply the "overwrite" patch and post the Modal form - the entities that are not immediately visible and checked on the current views page - get lost (overwritten), even if they were checked before. I understand now that for this to work correctly we need another feature - the ability to preserve checked/selected state of items throughtout the whole views with a pager (like VBO does). Basically, we need to store all the checked IDs in a variable and pass it to the field on submit.

Ace Cooper’s picture

[removed]

Ace Cooper’s picture

hgoto’s picture

Status: Needs review » Needs work

Thank you for the patch. I think this must be a good improvement.

The patch #3 surely works when the option "Pass selected entity ids to View" is checked on. But if the option is unchecked, the following warning appears as $this->view->args[0] is not set.

Notice: Undefined offset: 0

Also, I think it's better to make this a new option for the backward compatibility for existing sites.

katiaPa’s picture

Hi, are there some news about this issue? Is there a way to make it works with a pager?

MiriamDrupal’s picture

If I check "Pass selected entity ids to view".
In the view I can select the items. But when I retry to re-open the modal window it don´t show items, it show an error like this:

An error HTTP AJAX OCCURED
Result code HTTP:500
Response text: PDOException: SQLSTATE[22018];
[Microsoft][ODBC Driver 17 for SQL Server [SQL Server]Conversion failed when converting the varchar value &=039;164+163' to data type int: SELECT td[name] AS [name] FROM taxonomy_term_data td WHERE ( ([td].[tid] IN (:db_condition_placeholder_0)) );Arrray (:db_condition_placeholder_0]=> 164+163) in views_handler_argument_term_node_tid->title_query() (line 57 modules\taxonomy\views_handler_argument_term_node_tid.inc):