Entity reference field that uses view as reference method does not save
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3512079. -->
Reported by: [maskedjellybean](https://www.drupal.org/user/2402554)
Related to !9 !7
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>When I have an entity reference field that references Content (nodes), and I configure the field to use a View as the reference method, and under Form display, I set the field widget to Draggable Table, when I save an entity (node) of this type after dragging items in the entity reference field, it will not set the values of the entity reference field, and so it will not save the deltas. The next time the entity is edited, the order of the table is reset, making the table drag useless.</p>
<p>Using a view in this way to set the values of a multi value field is core functionality, so it would be great if this module could support it.</p>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<ol>
<li>Create a view of Content (nodes). Display only the title field. Ensure the view shows more than one result.</li>
<li>Edit a different content type than the one displayed in the view and go the Manage fields tab. Add an entity reference field to Content. Under Reference type > Reference method, choose "Views: Filter by an entity reference view". Select your View in "View used to select the entities". Save the field.</li>
<li>Continue editing the content type. Go to the Manage form display tab. Set the widget for the entity reference field to "Draggable Table". Save.</li>
<li>Edit a node of the content type you added the entity reference field to. Reorder the field values by dragging them. Save.</li>
<li>Edit the node again. See that the order you dragged the field values was not saved.</li>
<li>You can verify this further by looking at the database table for the entity reference field and see that it has no values for the <code>entity_id</code> of the node you've saved.</li>
</ol>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>This can be fixed by removing one if statement from <code>\Drupal\options_table\Plugin\Field\FieldWidget\OptionsTableWidget</code>, although I'm unsure of the repercussions (if any).</p>
<p>This is what we have now:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br> </span><span style="color: #007700">public static function </span><span style="color: #0000BB">validateElement</span><span style="color: #007700">(array </span><span style="color: #0000BB">$element</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$selected_options </span><span style="color: #007700">= [];<br><br> if (</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'#multiple'</span><span style="color: #007700">]) {<br> foreach (</span><span style="color: #0000BB">Element</span><span style="color: #007700">::</span><span style="color: #0000BB">children</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'table'</span><span style="color: #007700">]) as </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br> if (</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'table'</span><span style="color: #007700">][</span><span style="color: #0000BB">$item</span><span style="color: #007700">][</span><span style="color: #DD0000">'enabled'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#value'</span><span style="color: #007700">]) {<br> </span><span style="color: #0000BB">$selected_options</span><span style="color: #007700">[</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'table'</span><span style="color: #007700">][</span><span style="color: #0000BB">$item</span><span style="color: #007700">][</span><span style="color: #DD0000">'weight'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#value'</span><span style="color: #007700">]] = </span><span style="color: #0000BB">$item</span><span style="color: #007700">;<br> }<br> }<br> }<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>If we change it to this, the field values are saved as expected:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br> </span><span style="color: #FF8000">/**<br> * {@inheritdoc}<br> */<br> </span><span style="color: #007700">public static function </span><span style="color: #0000BB">validateElement</span><span style="color: #007700">(array </span><span style="color: #0000BB">$element</span><span style="color: #007700">, </span><span style="color: #0000BB">FormStateInterface $form_state</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$selected_options </span><span style="color: #007700">= [];<br><br> if (</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'#multiple'</span><span style="color: #007700">]) {<br> foreach (</span><span style="color: #0000BB">Element</span><span style="color: #007700">::</span><span style="color: #0000BB">children</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'table'</span><span style="color: #007700">]) as </span><span style="color: #0000BB">$item</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$selected_options</span><span style="color: #007700">[</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'table'</span><span style="color: #007700">][</span><span style="color: #0000BB">$item</span><span style="color: #007700">][</span><span style="color: #DD0000">'weight'</span><span style="color: #007700">][</span><span style="color: #DD0000">'#value'</span><span style="color: #007700">]] = </span><span style="color: #0000BB">$item</span><span style="color: #007700">;<br> }<br> }<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>This is because when using a View as the reference method, <code>$element['table'][$item]['enabled']['#value']</code> is an empty string. <code>$item</code> in this case is the ID of the referenced entity.</p>
<p>Thinking about it, I'm unsure why we would check for <code>$element['table'][$item]['enabled']['#value']</code>, because doesn't the fact that the item is in the table at all indicate that it should be saved as a field value?</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<h3 id="summary-ui-changes">User interface changes</h3>
<h3 id="summary-api-changes">API changes</h3>
<h3 id="summary-data-model-changes">Data model changes</h3>
issue