0

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.

0

You can create a new module use hook_theme_registry_alter like module simplelogin like this:

/**
 * Implements hook_theme_registry_alter
 *
 * {@inheritdoc}
 */
function YOUR_MODULE_theme_registry_alter(&$theme_registry) {
  $theme_registry['page__simplelogin'][preprocess functions] = [
    0 => 'simplelogin_preprocess_simplelogin',
    1 => 'YOUR_MODULE_preprocess_function'
  ];
}
  • I tried this, but it still not triggered the preprocess function... – richardson Jan 8 at 8:50
  • Did you try clear cache? If doesn' working try create module start with string t (u, v .. string after S). – Jonh Jan 8 at 9:00
  • Yes, I did clear cache. What do you mean by "create module start with string...", rename the module? – richardson Jan 8 at 9:13
  • Yes, the list hooks is array. I guess this array order by module name. So may be your hook run before simplelogin's hook. – Jonh Jan 8 at 9:19
  • Do I need to declare the function path somewhere? The two functions locate in different place. – richardson Jan 8 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.