g2-8.x-1.x-dev/g2.install
g2.install
<?php
/**
* @file
* Install file for G2 Glossary.
*
* @copyright 2005-2024 Frédéric G. Marand, for Ouest Systèmes Informatiques.
*/
declare(strict_types=1);
use Drupal\block\Entity\Block;
use Drupal\g2\G2;
use Drupal\g2\Requirements;
use Drush\Drush;
/**
* Get a function that will show something both on the Web UI and in Drush.
*
* @return callable
* A logger function taking just a message.
*/
function _g2_get_logger(): callable {
$default = [Drupal::messenger(), 'addWarning'];
if (php_sapi_name() !== 'cli' || !class_exists('\Drush\Drush')) {
return $default;
}
try {
return [Drush::logger(), 'notice'];
}
catch (\RuntimeException $_) {
return $default;
}
}
/**
* Implements hook_install().
*
* Automatically place the user profile block if the current theme allows it.
*/
function g2_install(): void {
$log = _g2_get_logger();
$theme = \Drupal::theme()->getActiveTheme();
$name = $theme->getName();
$region = 'content';
if (!in_array($region, $theme->getRegions())) {
$log(t('The current theme "%theme" has no "%region" region, so the "authored" block will not be placed automatically. You can still place it manually after installation.', [
'%theme' => $name,
'%region' => $region,
]));
return;
}
$view = G2::VIEW_AUTHORED;
$display = G2::VIEW_AUTHORED_DISPLAY;
$plugin_definition = [
'id' => "views_block__{$view}_{$display}",
'theme' => $name,
'region' => $region,
'weight' => 15,
'plugin' => "views_block:{$view}-{$display}",
'visibility' => [
'request_path' => [
'id' => 'request_path',
'negate' => FALSE,
'pages' => '/user/*',
],
],
];
$block = Block::create($plugin_definition);
$block->save();
}
/**
* Implements hook_requirements().
*
* @phpstan-return array<string,mixed[]>
*/
function g2_requirements(string $phase): array {
if ($phase != 'runtime') {
return [];
}
$requirements = Requirements::create(\Drupal::getContainer());
$requirements->checkControllers();
$requirements->checkStatistics();
$result = $requirements->getResult();
return $result;
}
/* ===== Code below this line not checked for D8 ============================ */
/**
* Implements hook_schema().
*
* Define the structure of the non-core tables used by G2.
*
* Schema API does not define it, but these tables should have UTF-8
* as their default charset
*
* @phpstan-return array<string,mixed[]>
*/
function g2_schema(): array {
$schema = [];
// G2 per-node referer stats.
$schema[G2::TBL_REFERER] = [
'fields' => [
'nid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The node id for the current G2 entry',
],
'referer' => [
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'The URL on which a link was found to the current item',
],
'incoming' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The number of hits coming from this referer',
],
],
'primary key' => ['nid', 'referer'],
'unique keys' => [],
'indexes' => [
'referer' => ['referer'],
],
'description' => 'The referer tracking table for G2 entries',
];
return $schema;
}
