8 – How to render a downloaded image from a configuration form

I am working on a custom module, which contains a block and I am currently having a problem to render an image being downloaded via a form during the configuration of the block.

I receive this error:

Error: Calling a member function getFileUri () on null in Drupal onyx_experiencia Plugin Block onyx_experiencia-> build () (line 52 of ….)

The file is loaded correctly in the specified directory. I have read articles on similar problems and mistakes, but I have not found the appropriate solution.

What I'm trying to achieve is to get the uri of the image uploaded via the form. I've read that it's sometimes necessary to load the entity or the image first, I've already tried it, but I still get the same error, but on the line where I try to load the file.

That's the code:

getConfiguration ();
$ build = array ();

if (! empty ($ config['card_title'])) {
$ name = $ config['card_title'];
}
other {
$ name = $ this-> t (Onyx Group Map & # 39;);
}

if (! empty ($ config['card_text'])) {
$ cardText = $ config['card_text'];
}
other {
$ cardText = $ this-> t (& # 39; One Onyx Group Service & # 39;);
}

$ cardImg = File :: load ($ config['card_image'])
$ cardUrl = $ config['card_link'];

$ imgVar = array (
& # 39; style_name & # 39; => & # 39; miniature & # 39;
& # 39; uri & # 39; => $ cardImg-> getFileUri (), //<--- THIS IS LINE 52:ERROR
    );

    $image = Drupal::service('image.factory')->get ($ cardImg-> getFileUri ());
if ($ image-> isValid ()) {
$ imgVar['width'] = $ image-> getWidth ();
$ imgVar['height'] = $ image-> getHeight ();
} other {
$ imgVar['width'] = $ imgVar['height'] = NULL;
}


to build[]['#attached']['library'][]    = & # 39; onyx_experiencia / onyx-experiencecia-style & # 39 ;;

to build['CardTitle'] = table (
& # 39; # prefix & # 39; => & # 39;
& # 39; # markup & # 39; => $ this-> t (& # 39; Hello @name! & # 39; array (& # 39; @ name & # 39; => $ name,)),
& # 39; # suffix & # 39; => & # 39;
)

to build['CardText'] = table (
& # 39; # prefix & # 39; => & # 39;
& # 39; # markup & # 39; => $ this-> t ('Card text: @cardText!', array (@ cardText => $ cardText,)),
& # 39; # suffix & # 39; => & # 39;
)

$ cardImg_array = array (
& # 39; # prefix & # 39; => & # 39;
& # 39; # theme & # 39; => & # 39; image_style & # 39 ;,
& # 39; # style_name & # 39; => $ imgVar['style_name'],
& # 39; # width & # 39; => $ imgVar['width'],
& # 39; # height & # 39; => $ imgVar['height'],
& # 39; # uri & # 39; => $ imgVar['uri'],
& # 39; # suffix & # 39; => & # 39;
)

$ renderer =  Drupal :: service (& # 39; renderer & # 39;);
$ renderer-> addCacheableDependecy ($ cardImg_array, $ cardImg);

return $ build;


}


/ **
* {@inheritdoc}
* /
public function blockForm ($ form, FormStateInterface $ form_state) {
$ form = parent :: blockForm ($ form, $ form_state);

$ config = $ this-> getConfiguration ();

$ form['card_title'] =[
& # 39; # type & # 39; => & # 39; textfield & # 39 ;,
& # 39; # title & # 39; => $ this-> t ('Card title'),
& # 39; # description & # 39; => $ this-> t (& # 39; Type the title of the service card. & nbsp;)
& # 39; # default_value & # 39; => isset ($ config['card_title'])? $ config['card_title'] : & # 39;
];

$ form['card_text'] =[
& # 39; # type & # 39; => & # 39; textarea & # 39 ;,
& # 39; # title & # 39; => $ this-> t ('Map Text'),
& # 39; # size & # 39; => & # 39; full_html & # 39;
& # 39; # description & # 39; => $ this-> t (& # 39; Type list of services. & # 39;),
& # 39; # default_value & # 39; => isset ($ config['card_text'])? $ config['card_text'] : & # 39;
];

$ form['card_image'] = [
        '#type' => 'managed_file',
        '#title' => t('Card image background'),
        '#upload_validators' => array(
            'file_validate_extensions' => array('gif png jpg jpeg'),
            'file_validate_size' => array(25600000),
        ),
        '#theme' => 'image_widget',
        '#preview_imgage_style' => 'medium',
        '#upload_location' => 'private://card_service_img',
        '#progress_message' => 'One moment while we save your file...',
        '#required' => TRUE,
    ];

$ form['card_link'] = [
        '#title' => t('Type card link. Example: /erp_cloud'),
        '#type' => 'url',
    ];

return $ form;
}

/ **
* Custom submission actions
* /
public function custom_submit_form ($ form, FormStateInterface $ form_state) {
$ values ​​= $ form_state-> getValues ​​();
// Perform the required actions
}

/ **
* {@inheritdoc}
* /
Public function blockSubmit ($ form, FormStateInterface $ form_state) {
$ this-> configuration['card_title'] = $ form_state-> getValue (& # 39; card_title & # 39;);
$ this-> configuration['card_text'] = $ state_format-> getValue (& # 39; card_text & # 39;);
$ this-> configuration['card_image'] = $ state_format-> getValue (& # 39; card_view & # 39;);
$ this-> configuration['card_link'] = $ state_format-> getValue (& # 39; map_link & # 39;);
}

}