You are here

function lightning_core_field_config_presave in Lightning Core 8.4

Same name and namespace in other branches
  1. 8.5 lightning_core.module \lightning_core_field_config_presave()
  2. 8.3 lightning_core.module \lightning_core_field_config_presave()

Implements hook_ENTITY_TYPE_presave().

File

./lightning_core.module, line 266
Contains core functionality for the Lightning distribution.

Code

function lightning_core_field_config_presave(FieldConfigInterface $field) {

  // Set the default image for user profiles.
  if ($field
    ->getTargetEntityTypeId() === 'user' && $field
    ->getName() === 'user_picture' && $field
    ->isNew() && !\Drupal::isConfigSyncing()) {
    $source = drupal_get_path('module', 'lightning_core') . '/images/default-avatar.png';
    $uri = 'public://default-avatar.png';
    try {
      \Drupal::service('file_system')
        ->copy($source, $uri, FileSystemInterface::EXISTS_ERROR);
      $file = File::create([
        'uri' => $uri,
      ]);
      $file
        ->setPermanent();
      $file
        ->save();
      $field
        ->setSetting('default_image', [
        'uuid' => $file
          ->uuid(),
        'alt' => (string) t('A generic silhouette of a person.'),
        'title' => '',
        'width' => 140,
        'height' => 140,
      ]);
    } catch (FileException $e) {

      // Fail silently.
    }
  }
}