Problem with dependency injection (File::load and \Drupal::service) In order to make a managed_file field permanent I did this in my module configuration form:

&lt;?php

namespace Drupal\my_module\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class MymoduleSettings.
 */
class MymoduleSettings extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_module_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config                    = $this-&gt;config('my_module.settings');
    $form['my_module_title'] = [
      '#type'          =&gt; 'textfield',
      '#title'         =&gt; $this-&gt;t('Title'),
      '#description'   =&gt; $this-&gt;t("Enter the title"),
      '#maxlength'     =&gt; 64,
      '#size'          =&gt; 64,
      '#required'      =&gt; TRUE,
      '#default_value' =&gt; $config-&gt;get('my_module_title'),
    ];
    $form['mymodule_icon']   = [
      '#type'              =&gt; 'managed_file',
      '#title'             =&gt; $this-&gt;t('Icon'),
      '#description'       =&gt; $this-&gt;t("Upload the image file for the icon"),
      '#upload_location'   =&gt; 'public://',
      '#upload_validators' =&gt; [
        'file_validate_extensions' =&gt; ['gif png jpg jpeg'],
      ],
      '#default_value'     =&gt; $config-&gt;get('my_module_icon'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);

    $this-&gt;config('my_module.settings')
      -&gt;set('my_module_title', $form_state-&gt;getValue('my_module_title'))
      -&gt;set('my_module_icon', $form_state-&gt;getValue('my_module_icon'))
      -&gt;save();

    // First we just grab the file ID for the icon we uploaded, if any.
    $icon_field = $form_state-&gt;getValue('my_module_icon');
    $file_id    = empty($icon_field) ? FALSE : reset($icon_field);

    if (!empty($file_id)) {
      // Make this a permanent file so that cron doesn't delete it later.
      $file         = File::load($file_id);
      $file-&gt;status = FILE_STATUS_PERMANENT;
      $file-&gt;save();
      $file_usage = \Drupal::service('file.usage');
      $file_usage-&gt;add($file, 'my_module', 'file', $file_id);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'my_module.settings',
    ];
  }

}


but I'm getting warning to use dependency injection principle in this case:

File::load calls should be avoided in classes, use dependency injection instead


and

\Drupal calls should be avoided in classes, use dependency injection instead


I looked through Dependency Injection for a Form but still not sure that I really get it.

Can someone explain me what I need to do in this particular case so I can get a better view on this issue and figure it out?
If you are extending FormBase you can use create to inject dependencies. In this case what I think you want is the entity_type.manager service and file.usage.

  /**
   * Class constructor.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, FileUsageInterface $file_usage) {
    $this-&gt;entityTypeManager = $entity_type_manager;
    $this-&gt;fileUsage = $file_usage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container-&gt;get('entity_type.manager'),
      $container-&gt;get('file.usage')
    );
  }


See this docs page for more examples.

The create method sends arguments to the constructor, and then you set those to instance variables, and voila, there are your dependencies. Be sure to update your file with the necessary use statements and declare your protected members (entityTypeManager and fileUsage) at the top of the form class.

Therefore, you can remove the static ::service call and use:

$this-&gt;fileUsage-&gt;add()...

Also, the reason I am passing entity type manager service, is because you would replace File::load with:

$this-&gt;entityTypeManager-&gt;getStorage('file')-&gt;load($id);

If you need more than that object returns, you may need to use a different service but I don't know that off the top of my head.