I'm a Drupal8 newbie. I installed a third party module simplelogin, and I attempted to change the {{site_name}}
value in page--simplelogin.html.twig
but can not find a right way.
I noticed that the variable is defined in simplelogin.module:
function simplelogin_preprocess_simplelogin(&$variables) {
$path = \Drupal::service('path.current')->getPath();
$module_path = drupal_get_path('module', 'simplelogin');
$variables['#attached']['library'][] = 'simplelogin/simplelogin-library';
// Able to use custom variables
$variables['path'] = $path;
$variables['base_path'] = base_path();
$variables['logo'] = file_url_transform_relative(file_create_url(theme_get_setting('logo.url')));
$variables['background_class'] = simple_login_settings('active');
$variables['background_opacity'] = simple_login_settings('opacity');
$variables['wrapper_width'] = simple_login_settings('width');
$variables['site_name'] = \Drupal::config('system.site')->get('name');
$variables['site_register'] = \Drupal::config('user.settings')->get('register');
}
I guess the second "simplelogin" presented in the hook function is a form element in the theme (I'm not quite clear although), as I found $form['simplelogin']
appears in the buildForm function of form controller.
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$form_state->disableCache();
$simplelogin_config = $this->config('simplelogin.settings');
$imageid = $simplelogin_config->get('background_image');
if ($imageid) {
$file = \Drupal\file\Entity\File::load($imageid[0]); // File Load
$fileUrl = $file->getFileUri();
// check if image is valid.
$image = \Drupal::service('image.factory')->get($fileUrl);
if ($image->isValid()) {
$image_render = array(
'#theme' => 'image_style',
'#width' => $image->getWidth(),
'#height' => $image->getHeight(),
'#style_name' => 'medium',
'#uri' => $fileUrl,
);
}
}
//$renderer = \Drupal::service('renderer');
//$renderer->addCacheableDependency($image_render, $file);
$form['simplelogin'] = array(
'#type' => 'details',
'#title' => $this->t('Configuration'),
'#open' => TRUE,
'background_active' => array(
WHAT I HAVE DONE:
1. created a custom module named "mylogin", this is the info.yml:
name: myLogin
description: my login module
type: module
version: '8.x-1.0'
package: 8.x
core: '8.x'
project: 'mylogin'
dependencies:
- simplelogin
2. override hook_preprocess_page in mylogin.module:
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\Core\Controller\ControllerBase;
use Drupal\simplelogin\Controller;
function mylogin_preprocess_page(&$variables) {
if (\Drupal::routeMatch()->getRouteName() == 'user.login') {
$variables['site_name']= 'test';
}
}
But it cannot work, and I believe the preprocess is not triggered. Is there a simple way to override the needed variable while keep the others in the module's preprocess function? Any help will be very appreciated.