psv-8.x-2.9/psv.module
psv.module
<?php
/**
* @file
* Provides a Password Strength Visualization configuration.
*/
use Drupal\Core\Render\Element;
use Drupal\file\Entity\File;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function psv_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.psv':
$text = file_get_contents(dirname(__FILE__) . '/README.md');
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . $text . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
return NULL;
}
/**
* Implements hook_theme_suggestions_alter().
*
* {@inheritdoc}
*/
function psv_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if ($hook == 'page') {
$path = \Drupal::service('path.current')->getPath();
$current_user = \Drupal::currentUser();
if (!$current_user->id()) {
if ($path == '/user/register') {
$suggestions[] = 'page__psv';
}
}
}
}
/**
* Add variables register page.
*
* {@inheritdoc}
*/
function psv_preprocess_psv(&$variables) {
$psv_settings = \Drupal::config('psv.config');
$variables['psv_background'] = '';
if ($psv_settings->get('psv_image')) {
$fid = $psv_settings->get('psv_image');
foreach ($fid as $key => $value) {
$fid1 = $value;
}
$file = File::load($fid1);
$url = Url::fromUri(file_create_url($file->getFileUri()));
$variables['psv_background'] = $url->getUri();
}
if ($psv_settings->get('psv_reverse')) {
$variables['#attached']['library'][] = 'psv/reversed';
}
else {
$variables['#attached']['library'][] = 'psv/normal';
}
}
/**
* Implements hook_theme_registry_alter().
*
* {@inheritdoc}
*/
function psv_theme_registry_alter(&$theme_registry) {
$psv_settings = \Drupal::config('psv.config');
if ($psv_settings->get('psv_enable') != 0) {
$module_path = drupal_get_path('module', 'psv');
$theme_registry['page__psv'] = [
'preprocess functions' => [
0 => 'psv_preprocess_psv',
],
'template' => 'page--psv',
'path' => $module_path . '/templates',
'render element' => 'page',
'type' => 'base_theme_engine',
];
}
}
/**
* Implements hook_preprocess_image_widget().
*
* {@inheritdoc}
*/
function psv_preprocess_image_widget(&$variables) {
$element = $variables['element'];
$variables['attributes'] = [
'class' =>
['image-widget', 'js-form-managed-file', 'form-managed-file', 'clearfix'],
];
if (!empty($element['fids']['#value'])) {
$file = reset($element['#files']);
$element['file_' . $file->id()]['filename']['#suffix'] = ' <span class="file-size">(' . format_size($file->getSize()) . ')</span> ';
$file_variables = [
'style_name' => $element['#preview_image_style'],
'uri' => $file->getFileUri(),
];
// Determine image dimensions.
if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
$file_variables['width'] = $element['#value']['width'];
$file_variables['height'] = $element['#value']['height'];
}
else {
$image = \Drupal::service('image.factory')->get($file->getFileUri());
if ($image->isValid()) {
$file_variables['width'] = $image->getWidth();
$file_variables['height'] = $image->getHeight();
}
else {
$file_variables['width'] = $file_variables['height'] = NULL;
}
}
$element['preview'] = [
'#weight' => -10,
'#theme' => 'image_style',
'#width' => $file_variables['width'],
'#height' => $file_variables['height'],
'#style_name' => $file_variables['style_name'],
'#uri' => $file_variables['uri'],
];
// Store the dimensions in the form so the file doesn't have to be
// accessed again. This is important for remote files.
$element['width'] = [
'#type' => 'hidden',
'#value' => $file_variables['width'],
];
$element['height'] = [
'#type' => 'hidden',
'#value' => $file_variables['height'],
];
}
$variables['data'] = [];
foreach (Element::children($element) as $child) {
$variables['data'][$child] = $element[$child];
}
}
