pantheon_decoupled-1.0.0-alpha3/pantheon_decoupled.module
pantheon_decoupled.module
<?php
/**
* @file
* Pantheon Decoupled module file.
*/
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function pantheon_decoupled_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the pantheon_decoupled module.
case 'help.page.pantheon_decoupled':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Installs recommended dependencies for a Decoupled Drupal backend, and configures an example preview site and oAuth consumer for use with Decoupled Preview.') . '</p>';
$output .= '<h3>' . t('Resources') . '</h3>';
$output .= '<ul><li>' . t('<a href=":backend-docs">Drupal Backend Starter</a>', [
':backend-docs' => 'https://docs.pantheon.io/guides/decoupled/drupal-backend-starters',
]) . '</li>';
$output .= '<li>' . t('<a href=":frontend-docs">Drupal + Next.js Frontend Starter</a>', [
':frontend-docs' => 'https://docs.pantheon.io/guides/decoupled/drupal-nextjs-frontend-starters',
]) . '</li></ul>';
return $output;
}
}
/**
* Automatically generate keys for use with Simple OAuth.
*
* @return array
* public and private key paths.
*/
function _pantheon_decoupled_generate_keys() {
// Build all the dependencies manually to avoid having to rely on the
// container to be ready.
/** @var \Drupal\simple_oauth\Service\KeyGeneratorService $key_gen */
$key_gen = \Drupal::service('simple_oauth.key.generator');
/** @var \Drupal\simple_oauth\Service\Filesystem\FileSystemChecker $file_system_checker */
$file_system_checker = \Drupal::service('simple_oauth.filesystem_checker');
/** @var \Drupal\Core\File\FileSystem $file_system */
$file_system = \Drupal::service('file_system');
/** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */
$logger = \Drupal::logger('pantheon_decpouled');
$private_path = PrivateStream::basePath();
$pub_filename = sprintf('%s/public.key', $private_path);
$pri_filename = sprintf('%s/private.key', $private_path);
if ($file_system_checker->fileExist($pub_filename) && $file_system_checker->fileExist($pri_filename)) {
// 1. If the file already exists, then just set the correct permissions.
$file_system->chmod($pub_filename, 0600);
$file_system->chmod($pri_filename, 0600);
$logger->info('Key pair for OAuth 2 token signing already exists.');
}
else {
// 2. Generate the pair in the selected directory.
try {
$key_gen->generateKeys($private_path);
}
catch (\Exception $e) {
// Unable to generate files after all.
$logger->error($e->getMessage());
return $e;
}
}
// Return public and private paths so they can be set in config.
return [
'public' => $pub_filename,
'private' => $pri_filename,
];
}
function pantheon_decoupled_theme($existing, $type, $theme, $path) {
return [
// Name of the theme hook. This is used in the controller to trigger the hook.
'pantheon_decoupled_fes_settings' => [
'render element' => 'children',
// If no template name is defined here,
// it defaults to the name of the theme hook,
// ie. module-name-theme-hook.html.twig
'template' => 'pantheon_decoupled_fes_settings',
// Optionally define path to Twig template files.
// Defaults to the module's ./templates/ directory.
'path' => $path . '/templates',
],
];
}
/**
* Alter entity operations.
*
* @param array $operations
* Operations array as returned by
* \Drupal\Core\Entity\EntityStorageControllerInterface::getOperations().
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity on which the linked operations will be performed.
*/
function pantheon_decoupled_entity_operation_alter(array &$operations, \Drupal\Core\Entity\EntityInterface $entity) {
// TODO - Check for FES route as well here?
if ($entity->getEntityTypeId() === 'dp_preview_site') {
$operations['environment'] = array(
'title' => t('Environment Variables'),
'url' => Url::fromRoute('pantheon_decoupled.environment', ['preview_site' => $entity->id()]),
'weight' => 10,
'attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode([
'width' => 880,
]),
],
);
$operations['test'] = array(
'title' => t('Test'),
'attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'modal',
'data-dialog-options' => json_encode([
'width' => 880,
]),
],
'url' => Url::fromRoute('pantheon_decoupled.test', ['preview_site' => $entity->id()]),
'weight' => 10,
);
}
}
