You are here

function simplenews_install in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 simplenews.install \simplenews_install()
  2. 8 simplenews.install \simplenews_install()
  3. 5 simplenews.install \simplenews_install()
  4. 6.2 simplenews.install \simplenews_install()
  5. 6 simplenews.install \simplenews_install()
  6. 7.2 simplenews.install \simplenews_install()
  7. 7 simplenews.install \simplenews_install()

Implements hook_install().

File

./simplenews.install, line 102
Install, update and uninstall functions for the simplenews module.

Code

function simplenews_install() {
  if (\Drupal::service('config.installer')
    ->isSyncing()) {
    return;
  }

  // Set the default values for test_address, from_address and from_name.
  $site_mail = \Drupal::config('system.site')
    ->get('mail');
  $site_name = \Drupal::config('system.site')
    ->get('name');
  $config = \Drupal::configFactory()
    ->getEditable('simplenews.settings');
  if (empty($site_mail)) {
    $site_mail = ini_get('sendmail_from');
  }
  $config
    ->set('newsletter.from_address', $site_mail);
  if (empty($site_name)) {
    $site_name = 'Drupal';
  }
  $config
    ->set('newsletter.from_name', $site_name);
  $config
    ->save(TRUE);
  user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
    'subscribe to newsletters',
  ]);
  user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, [
    'subscribe to newsletters',
  ]);

  // Init the default newsletter.
  $newsletter = Newsletter::load('default');
  $newsletter->from_name = $site_name;
  $newsletter->from_address = $site_mail;
  $newsletter
    ->trustData();
  $newsletter
    ->save();
}