Saving and retrieving EntityAutocomplete value I'm building a block plugin that allows users to specify a menu to appear in a block plugin. I'm taking advantage of the EntityAutocomplete form element.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Element%21EntityAutocomplete.php/class/EntityAutocomplete/8.2.x

Here is how the form is built.

public function blockForm($form, FormStateInterface $form_state) {
    $form['menu'] = [
        '#type'          =&gt; 'entity_autocomplete',
        '#target_type'   =&gt; 'menu',
        '#default_value' =&gt; $this-&gt;configuration['menu'],
    ];
    return $form;
}


It's able to display the form element just fine. And it allows users to select menus as well. I then save the item on submit like this.

public function blockSubmit($form, FormStateInterface $form_state) {
    $this-&gt;configuration['menu'] = $form_state-&gt;getValue('menu');
}


However after saving it and opening it again I receive this error


  InvalidArgumentException: The #default_value property has to be an
  entity object or an array of entity object


How do I successfully save or retrieve the menu? Do I need to put something like $this->configuration['menu']['value']?

Update: When I perform 

die(print_r($this-&gt;configuration['menu']));


It just prints the machine name of the menu I selected. So maybe the issue is that the default value it's being supplied is just a string instead of the actual entity? But if so how would I get the entity from the machine name?

Update 2: I tried using 

$menu = Entity::load($this-&gt;configuration['menu']);
die(print_r($menu));


But that didn't produce anything.
I was able to resolve the issue by loading the entity first like so

$menu = \Drupal::entityTypeManager()-&gt;getStorage('menu')-&gt;load($this-&gt;configuration['menu']);
    $form['menu'] = [
    '#type'          =&gt; 'entity_autocomplete',
    '#target_type'   =&gt; 'menu',
    '#default_value' =&gt; $menu,
];


The value of $this->configuration['menu'] was only the machine name, not the actual entity. So the entity had to be loaded and stored in a variable first.