You are here

function avatars_install in Avatar Kit 8

Same name and namespace in other branches
  1. 8.2 avatars.install \avatars_install()

Implements hook_install().

File

./avatars.install, line 14
Install, update and uninstall functions for the AK module.

Code

function avatars_install() {
  $picture_settings = [
    'file_extensions' => 'png gif jpg jpeg',
    'file_directory' => 'pictures',
    'max_filesize' => '128 KB',
    'max_resolution' => '1024x1024',
    'min_resolution' => '100x100',
    'alt_field' => '',
    'alt_field_required' => '',
  ];

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $displayRepo */
  $displayRepo = \Drupal::service('entity_display.repository');

  // Shows avatar generator setting
  if (FieldConfig::loadByName('user', 'user', AK_FIELD_AVATAR_GENERATOR)) {
    $displayRepo
      ->getFormDisplay('user', 'user', 'default')
      ->setComponent(AK_FIELD_AVATAR_GENERATOR, [
      'type' => 'avatars_generator_preview',
    ])
      ->save();
  }

  // Drupals picture
  if (FieldConfig::loadByName('user', 'user', AK_FIELD_PICTURE_ACTIVE)) {

    // Hide Drupal picture on form
    $displayRepo
      ->getFormDisplay('user', 'user', 'default')
      ->removeComponent(AK_FIELD_PICTURE_ACTIVE)
      ->save();
  }
  else {

    // Picture may not exist if user did not user 'standard' profile.
    if (!FieldStorageConfig::loadByName('user', AK_FIELD_PICTURE_ACTIVE)) {
      FieldStorageConfig::create([
        'field_name' => AK_FIELD_PICTURE_ACTIVE,
        'entity_type' => 'user',
        'type' => 'image',
      ])
        ->save();
    }
    FieldConfig::create([
      'field_name' => AK_FIELD_PICTURE_ACTIVE,
      'entity_type' => 'user',
      'bundle' => 'user',
      'label' => t('User picture'),
      'description' => t('An image representing your user account. Do not upload your avatars to this field.'),
    ])
      ->setSettings($picture_settings)
      ->save();
  }
  $displayRepo
    ->getViewDisplay('user', 'user', 'default')
    ->setComponent(AK_FIELD_PICTURE_ACTIVE, [])
    ->save();

  // Create the field the user can upload avatars.
  if (!FieldStorageConfig::loadByName('user', AK_FIELD_PICTURE_USER)) {
    FieldStorageConfig::create([
      'field_name' => AK_FIELD_PICTURE_USER,
      'entity_type' => 'user',
      'type' => 'image',
    ])
      ->save();
  }

  // attach the field to the user/user bundle
  if (!FieldConfig::loadByName('user', 'user', AK_FIELD_PICTURE_USER)) {
    FieldConfig::create([
      'field_name' => AK_FIELD_PICTURE_USER,
      'entity_type' => 'user',
      'bundle' => 'user',
      'label' => t('User picture upload'),
      'description' => t('An image representing your user account.'),
    ])
      ->setSettings($picture_settings)
      ->save();

    // Make it visible on form
    $displayRepo
      ->getFormDisplay('user', 'user', 'default')
      ->setComponent(AK_FIELD_PICTURE_USER, [])
      ->save();

    // Hide it on view
    $displayRepo
      ->getViewDisplay('user', 'user', 'default')
      ->removeComponent(AK_FIELD_PICTURE_USER)
      ->save();
  }
}