I have a custom node creation form I am rendering within a views preprocess hook. In that form I have two entity reference fields. I am setting the [#value] and [#default_value] of those reference fields programmatically.
On form submit the values I've set are created, but only if the fields are "editable" by the user. I want to make the fields "disabled" and/or "hidden" from the user. Not just hidden with CSS but with the entire autocomplete widget being made inaccessible. The field needs to be type "hidden".
How can I make an autocomplete entity reference disabled and/or inaccessible to the user creating the content?
Or another way of putting it, how can I add entity reference values programmatically on form submit without the user being able to interact with the fields?
Here are some things I've tried that do not work:
- $form['field_entity_reference']['#access'] = false; // Setting #access to false seems as though it should work because the form fields still exist when debugging $form, but it does not work as it prevents the value from being submitted.
- Adding ['#attributes']['readonly'], changing ['#type'] to hidden or another widget type, adding ['#attributed']['disabled']. None work
- Entity prepopulate - This does not work for this particular situation as some values cannot be provided by tokens
Code for reference
function <theme>_preprocess_views_view_field(&$variables) {
$view = $variables['view'];
if ($view->id() == '<view>') {
$field = $variables['field'];
switch($field->options['id']) {
case '<view_field>':
$node = \Drupal::entityTypeManager()
->getStorage('node')
->create(['type' => '<node_type>']);
$form = \Drupal::service('entity.form_builder')->getForm($node, 'secondary');
// Set default_value and value
$form['field_entity_reference']['widget']['#default_value'] = (int)$cid; // Note this is using the "select list" widget for the entity reference field
$form['field_entity_reference']['widget']['#value'] = (int)$cid; // Same as above
// Stuff that doesn't work. Values do not submit unless field is editable by user, or the field is not hidden/disabled
$form['field_entity_reference']['#access'] = false; // Value does not submit
$form['field_entity_reference']['widget'][0]['#attributes']['readonly'] = 'readonly'; // Doesn't make field read only
$form['field_entity_reference']['widget'][0]['value']['#type'] = 'hidden'; // Seems autocomplete widget and select widget do not have a "hidden" type
$form['field_entity_reference']['widget'][0]['target_id']['#type'] = 'textfield'; // Seems one cannot change the type to textfield
// Change view field output to the form
$variables['output'] = $form;
break;
}
}
}
hook_entity_presave()
instead of setting#value
in the form itself. You can usehook_entity_presave()
even when the fields are hidden or the user does not have access to edit the fields.#access
toFALSE
on a form element, and it will not be sent to the browser to be rendred. However, the value will be processed when the form is submitted, so as long as the value is pre-set on the element either in the form definition, or a form_alter hook, the value can be passed through.