You are here

l10n_client_contributor.install in Localization client 8

Install, update and uninstall functions for the l10n_client_contributor module.

File

l10n_client_contributor/l10n_client_contributor.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the l10n_client_contributor module.
 */
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

/**
 * Implements hook_install().
 */
function l10n_client_contributor_install() {

  // If the module is installed as part of a config sync to a new environment,
  // the configuration should already be present and therefore does not need to
  // be created.
  if (!\Drupal::service('config.installer')
    ->isSyncing()) {
    FieldStorageConfig::create(array(
      'entity_type' => 'user',
      'field_name' => 'l10n_client_contributor_key',
      'type' => 'l10n_client_contributor_key',
      'cardinality' => 1,
    ))
      ->save();
    FieldConfig::create(array(
      'entity_type' => 'user',
      'field_name' => 'l10n_client_contributor_key',
      'bundle' => 'user',
      'label' => 'Localization client contributor API key',
      'translatable' => FALSE,
    ))
      ->save();

    /** @var Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
    $form_display = \Drupal::entityTypeManager()
      ->getStorage('entity_form_display')
      ->load('user.user.default');

    // If the site does not yet have a default user form display in
    // configuration, create one now for further modification.
    // @see entity_get_form_display()
    if (!$form_display) {
      $values = array(
        'targetEntityType' => 'user',
        'bundle' => 'user',
        'mode' => 'default',
        'status' => TRUE,
      );
      $form_display = Drupal::entityTypeManager()
        ->getStorage('entity_form_display')
        ->create($values);
    }
    $form_display
      ->setComponent('l10n_client_contributor_key', array(
      'type' => 'l10n_client_contributor_key_widget',
    ))
      ->save();
  }
}

Functions