posthog-1.0.0-alpha5/modules/posthog_js/posthog_js.install
modules/posthog_js/posthog_js.install
<?php
/**
* @file
* Install, update and uninstall functions for the Posthog: Events module.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Implements hook_requirements().
*/
function posthog_js_requirements($phase) {
$requirements = [];
// If we are not in runtime phase, there is nothing to do. So bail out early.
if ($phase !== 'runtime') {
return [];
}
if (\Drupal::config('posthog_js.settings')->get('cdn')) {
// IF CDN is enabled, we cam exit early.
$requirements['posthog_js'] = [
'title' => t('Posthog JS'),
'severity' => REQUIREMENT_OK,
'value' => t('Loading from CDN'),
'description' => t('posthog-js is loaded directly from Posthog ("CDN").'),
];
return $requirements;
}
/** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
$library_file_finder = \Drupal::service('library.libraries_directory_file_finder');
/** @var \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery */
$library_definition = \Drupal::service('library.discovery')->getLibraryByName('posthog_js', 'local');
$help_text = t('
<strong>Manual installation</strong>
<ol>
<li>Download the @pluginDownloadLink</li>
<li>Extract the archive and place the "posthog-js" directory in the "libraries" directory on your server.</li>
</ol>
<strong>Composer installation (recommended)</strong></br>
<ol>
<li>Enable usage of third-party libraries using composer, see @here for an explanation.</li>
<li>Require the "posthog-js" library using "npm-asset/posthog-js"</li>
</ol>
',
[
'@pluginDownloadLink' => Link::fromTextAndUrl(t('posthog-js'), Url::fromUri($library_definition['remote']))->toString(),
'@here' => Link::fromTextAndUrl(t('here'), Url::fromUri('https://www.drupal.org/docs/develop/using-composer/manage-dependencies#third-party-libraries'))->toString(),
]);
// Library not found, show a warning:
if (!$library_file_finder->find($library_definition['js'][0]['data'])) {
$requirements['posthog_js'] = [
'title' => t('Posthog JS local library'),
'severity' => REQUIREMENT_WARNING,
'value' => t('Missing local posthog-js library'),
'description' => t('You chose to load the posthog-js library locally, not from Posthog CDN, but the "all-external-dependencies.js" file could not be found inside the "/libraries/posthog-js/build" folder.
<br>@helpText', [
'@helpText' => $help_text,
]),
];
}
return $requirements;
}
/**
* Rebuild the container, to remove old dependency injected service.
*/
function posthog_js_update_10001() {
\Drupal::service('kernel')->rebuildContainer();
}
/**
* Remove the old "enable_identifying_users" setting.
*/
function posthog_js_update_10002() {
\Drupal::configFactory()->getEditable('posthog_js.settings')
->clear('enable_identifying_users')
->save();
}
