3

On Drupal 8 there is by default a user_picture field for the user's photo. When I started my site several months ago, I deleted this field because I did not use it.

enter image description here

Today I need this field, so I created an image type field.

enter image description here

MY PROBLEM : I installed some modules that use the user's photo, but it can not be found because the user_picture is the default

If I create a user_picture field it automatically adds field_ in front of the machine name.

Is it possible to restore the default field with the correct machine name ?

3 Answers 3

8

Instead of importing configuration as suggested previously, you could as an alternative, programmatically re-create the field. Here is some code that does that.

<?php

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;


// Create the user profile field and instance.
FieldStorageConfig::create([
  'entity_type' => 'user',
  'field_name' => 'user_picture',
  'type' => 'image',
  'translatable' => '0',
])->save();
FieldConfig::create([
  'label' => 'User Picture',
  'description' => '',
  'field_name' => 'user_picture',
  'entity_type' => 'user',
  'bundle' => 'user',
  'required' => 0,
])->save();

Assuming the code above is placed in a file called recreateUserPicture.php that is stored in your docroot, you can run the following Drush command:

drush scr recreateUserPicture.php

Best of luck!

N.B.: An alternative answer (since the importing of configuration from the standard profile has a problem with having the field being available in Views UI).

4
  • 1
    @1ad9ac48, you must delete the user_picture field before running the code above. Commented Feb 28, 2018 at 6:27
  • You can easily run this code by pasting into the interactive shell from running "drush php"
    – AdamS
    Commented Nov 26, 2018 at 15:12
  • 1
    That was useful Mr. Petrakis ;)
    – MrUpsidown
    Commented Feb 28, 2020 at 9:41
  • A pleasure to hear that Mr. Upsidown! Commented Feb 28, 2020 at 20:46
5

This can be handled using the install configuration that the standard profile holds. Easiest way would be using drupal's console, that would mean following these steps (from inside your docroot):

  1. drupal config:import:single --file=core/profiles/standard/config/install/field.storage.user.user_picture.yml
  2. drupal config:import:single --file=core/profiles/standard/config/install/field.field.user.user.user_picture.yml
  3. drupal update:entities

In case you can't work on the terminal using drupal console, you can import these two configuration from the administrative interface. This can be found under the admin/config/development/configuration/single/import URL (relative to your host's name). From there, you need to import twice, one time the file storage configuration (contents of core/profiles/standard/config/install/field.storage.user.user_picture.yml) and the second would be the file configuration (contents of core/profiles/standard/config/install/field.field.user.user.user_picture.yml).

Hope this helps, good luck!

4
  • Thank you I updated the question. It does not work
    – 1ad9ac48
    Commented Feb 26, 2018 at 18:39
  • Did you try using the admin interface? Commented Feb 26, 2018 at 18:49
  • There is a problem. The field has been recreated, but it is not available in the views. An idea ?
    – 1ad9ac48
    Commented Feb 27, 2018 at 14:49
  • @1ad9ac48, I can reproduce that issue. I haven't figured out why Views does not make the field available. I posted an alternative answer that solves your issue in a programmatic way. Commented Feb 28, 2018 at 6:26
0

I was facing the same issue, according to the tips above, wanted to go a save way, for me the following solution helped:

  1. drush cex to export my actual configuration into Yaml files

  2. compared them with the original files (by searching (..vendor/drupal/core/profiles/standard/config/install) --> by using the grep command in linux e.g. grep -Ril 'user_picture' /vendor/drupal/core/profiles/standard/config/install

  3. edited the following files of my exported configuration Yaml files:

    +------------+--------------------------------------------+-----------+
    
    | Collection | Config                                     | Operation |
    
    +------------+--------------------------------------------+-----------+
    
    |            | field.storage.user.user_picture            | Create    |
    
    |            | field.field.user.user.user_picture         | Create    |
    
    |            | core.entity_view_display.user.user.default | Update    |
    
    |            | core.entity_view_display.user.user.compact | Update    |
    
    |            | core.entity_form_display.user.user.default | Update    |
    
    +------------+--------------------------------------------+-----------+
    
  4. drush cim to reimport the modified and added files

  5. drush cr to clear cache

Tip: maybe its already enough to copy the two files: field.storage.user.user_picture and field.field.user.user.user_picture and to your config export directory (after a fresh export) and do an import, because the difference within the core files was just a missing dependency.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.