3 namespace Drupal\entity_reference_revisions\Plugin\Field\FieldWidget;
5 use Drupal\Core\Entity\Entity;
6 use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
7 use Drupal\Core\Form\FormStateInterface;
10 * Plugin implementation of the 'entity_reference_autocomplete' widget.
13 * id = "entity_reference_revisions_autocomplete",
14 * label = @Translation("Autocomplete"),
15 * description = @Translation("An autocomplete text field."),
17 * "entity_reference_revisions"
21 class EntityReferenceRevisionsAutocompleteWidget extends EntityReferenceAutocompleteWidget {
26 public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
27 $entity_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type');
28 foreach ($values as $key => $value) {
29 if($value['target_id']) {
30 $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($value['target_id']);
31 // Add the current revision ID.
32 $values[$key]['target_revision_id'] = $entity->getRevisionId();
34 // The entity_autocomplete form element returns an array when an entity
35 // was "autocreated", so we need to move it up a level.
36 if (is_array($value['target_id'])) {
37 unset($values[$key]['target_id']);
38 $values[$key] += $value['target_id'];