l10n_server-2.x-dev/l10n_server/l10n_server.module
l10n_server/l10n_server.module
<?php /** * @file * The localization server module. */ declare(strict_types=1); use Drupal\Core\Render\Element; use Drupal\views\Views; /** * Action flag for string addition. */ define('L10N_SERVER_ACTION_ADD', 1); /** * Action flag for approval. */ define('L10N_SERVER_ACTION_APPROVE', 2); /** * Action flag for denial. */ define('L10N_SERVER_ACTION_DECLINE', 3); /** * Action flag for demotes. */ define('L10N_SERVER_ACTION_DEMOTE', 4); /** * Action flag for re-additions. */ define('L10N_SERVER_ACTION_READD', 5); /** * Action medium flag for unknown sources. */ define('L10N_SERVER_MEDIUM_UNKNOWN', 0); /** * Action medium flag for web based actions. */ define('L10N_SERVER_MEDIUM_WEB', 1); /** * Action medium flag for web based import. */ define('L10N_SERVER_MEDIUM_IMPORT', 2); /** * Action medium flag for remote action. */ define('L10N_SERVER_MEDIUM_REMOTE', 3); /** * Implements hook_cron(). */ function l10n_server_cron(): void { // Queue releases to be parsed. $queue = \Drupal::queue('l10n_server_parser'); $storage = \Drupal::entityTypeManager() ->getStorage('l10n_server_release'); $ids = $storage->getIdsToQueue(); foreach ($storage->loadMultiple($ids) as $release) { if ($queue->createItem($release)) { // Add timestamp to avoid queueing item more than once. $release->setQueuedTime(\Drupal::time()->getRequestTime()); $release->save(); } } /** @var \Drupal\l10n_server\ConnectorManager $connectorManager */ $connectorManager = \Drupal::service('plugin.manager.l10n_server.connector'); $connectors = \Drupal::config('l10n_server.settings') ->get('enabled_connectors'); \Drupal::logger('cron')->info('Start scanning l10n_server connectors...'); $connector_count = $project_count = $release_count = 0; foreach ($connectors as $connector_id) { /** @var \Drupal\l10n_server\ConnectorInterface $connector */ $connector = $connectorManager->createInstance($connector_id); if ($connector->isScannable() && $connector->getSourceInstance()->getConfiguration()['cron_scanning_enabled']) { $connector_count++; \Drupal::logger('cron')->info('Scanning @connector_label @source_label...', [ '@connector_label' => $connector->getLabel(), '@source_label' => $connector->getSourceInstance()->getLabel(), ]); /** @var \Drupal\l10n_server\ConnectorScanHandlerResultInterface $result */ $result = $connector->scanHandler(); $project_count = $project_count + $result->getProjectCount(); $release_count = $release_count + $result->getReleaseCount(); } } \Drupal::logger('cron')->info('Done scanning l10n_server connectors.', [ '@connector_label' => $connector->getLabel(), '@source_label' => $connector->getSourceInstance()->getLabel(), ]); \Drupal::logger('cron')->info('Scanned @projects project(s) and @releases release(s) in @connectors connector(s).', [ '@connectors' => $connector_count, '@projects' => $project_count, '@releases' => $release_count, ]); } /** * Implements hook_element_info_alter(). */ function l10n_server_element_info_alter(array &$types): void { if (isset($types['toolbar'])) { $types['toolbar']['#attached']['library'][] = 'l10n_server/toolbar'; } } /** * Implements hook_theme(). */ function l10n_server_theme(): array { return [ 'l10n_server_project' => [ 'render element' => 'elements', ], 'l10n_server_release' => [ 'render element' => 'elements', ], ]; } /** * Prepares variables for project templates. * * Default template: l10n-server-project.html.twig. * * @param array $variables * An associative array containing: * - elements: An associative array containing the project information and any * fields attached to the entity. * - attributes: HTML attributes for the containing element. */ function template_preprocess_l10n_server_project(array &$variables): void { $variables['view_mode'] = $variables['elements']['#view_mode']; foreach (Element::children($variables['elements']) as $key) { $variables['content'][$key] = $variables['elements'][$key]; } } /** * Prepares variables for release templates. * * Default template: l10n-server-release.html.twig. * * @param array $variables * An associative array containing: * - elements: An associative array containing the release information and any * fields attached to the entity. * - attributes: HTML attributes for the containing element. */ function template_preprocess_l10n_server_release(array &$variables): void { $variables['view_mode'] = $variables['elements']['#view_mode']; foreach (Element::children($variables['elements']) as $key) { $variables['content'][$key] = $variables['elements'][$key]; } $view = Views::getView('l10n_server_warnings'); $view->setDisplay('embed_1'); $view->setArguments([$variables['elements']['#l10n_server_release']->id()]); $view->execute(); $variables['content']['warnings'] = $view->render(); $variables['content']['warning_count'] = count($view->result); $view = Views::getView('l10n_server_files'); $view->setDisplay('embed_1'); $view->setArguments([$variables['elements']['#l10n_server_release']->id()]); $view->execute(); $variables['content']['files'] = $view->render(); $variables['content']['file_count'] = count($view->result); } /** * Implements hook_page_attachments(). */ function l10n_server_page_attachments(array &$page): void { $page['#attached']['library'][] = 'l10n_server/table'; } /** * Implements hook_preprocess_page_title(). */ function l10n_server_preprocess_page_title(&$variables) { $route_name = \Drupal::routeMatch()->getRouteName(); if ($route_name === 'entity.l10n_server_project.canonical') { /** @var \Drupal\l10n_server\Entity\L10nServerProjectInterface $project */ $project = \Drupal::routeMatch()->getParameter('l10n_server_project'); $variables['title'] = t('@project_name project translations', [ '@project_name' => $project->label(), ]); } }