You are here

function message_subscribe_email_install in Message Subscribe 8

Same name and namespace in other branches
  1. 7 message_subscribe_email/message_subscribe_email.install \message_subscribe_email_install()

Implements hook_install().

Change message subscription to the new views and create a new field.

File

message_subscribe_email/message_subscribe_email.install, line 13
Install hooks for the message_subscribe_email module.

Code

function message_subscribe_email_install() {

  // Add the `message_subscribe_email` field to the user form.

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  if (method_exists(\Drupal::service('entity_display.repository'), 'getFormDisplay')) {
    $display = \Drupal::service('entity_display.repository')
      ->getFormDisplay('user', 'user');
  }
  else {

    // Pre-Drupal 8.8.
    $display = entity_get_form_display('user', 'user', 'default');
  }
  if (!$display
    ->getComponent('message_subscribe_email')) {
    $settings = [
      'type' => 'boolean_checkbox',
      'weight' => 25,
      'settings' => [
        'display_label' => TRUE,
      ],
      'third_party_settings' => [],
    ];
    $display
      ->setComponent('message_subscribe_email', $settings);
    $display
      ->save();
  }

  // Update the UI to use the email views.
  $flags = [
    // Flag name => view name + display.
    'subscribe_node' => 'subscribe_node_email:default',
    'subscribe_term' => 'subscribe_taxonomy_term_email:default',
    'subscribe_user' => 'subscribe_user_email:default',
    // Setup OG in case it exists.
    'subscribe_og' => 'subscribe_node_email:default',
  ];
  foreach ($flags as $flag_id => $view_name) {
    if ($flag = \Drupal::service('flag')
      ->getFlagById($flag_id)) {

      // Verify this hasn't been already overridden.
      $existing = $flag
        ->getThirdPartySetting('message_subscribe_ui', 'view_name');
      if (!$existing || $existing == str_replace('_email', '', $view_name)) {
        $flag
          ->setThirdPartySetting('message_subscribe_ui', 'view_name', $view_name);
        $flag
          ->save();
      }
    }
  }
}