UserHooksTest.php

Namespace

Drupal\user_hooks_test\Hook

File

core/modules/user/tests/modules/user_hooks_test/src/Hook/UserHooksTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\user_hooks_test\Hook;

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\State\StateInterface;

/**
 * Contains hook implementations.
 */
class UserHooksTest {
    public function __construct(StateInterface $state) {
    }
    
    /**
     * Alters the username.
     *
     * @param string|\Drupal\Component\Render\MarkupInterface $name
     *   The username that is displayed for a user.
     * @param \Drupal\Core\Session\AccountInterface $account
     *   The object on which the operation is being performed.
     *
     * @return void
     */
    public function userFormatNameAlter(&$name, AccountInterface $account) {
        if ($this->state
            ->get('user_hooks_test_user_format_name_alter', FALSE)) {
            if ($this->state
                ->get('user_hooks_test_user_format_name_alter_safe', FALSE)) {
                $name = new FormattableMarkup('<em>@uid</em>', [
                    '@uid' => $account->id(),
                ]);
            }
            else {
                $name = '<em>' . $account->id() . '</em>';
            }
        }
    }

}

Classes

Title Deprecated Summary
UserHooksTest Contains hook implementations.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.