diff --git a/web/modules/realname/LICENSE.txt b/web/modules/realname/LICENSE.txt new file mode 100644 index 0000000000000000000000000000000000000000..d159169d1050894d3ea3b98e1c965c4058208fe1 --- /dev/null +++ b/web/modules/realname/LICENSE.txt @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/web/modules/realname/README.md b/web/modules/realname/README.md new file mode 100644 index 0000000000000000000000000000000000000000..fae0834c8dcae3487f7f070b6278de6f53fda96a --- /dev/null +++ b/web/modules/realname/README.md @@ -0,0 +1,51 @@ +# Realname + +## CONTENTS OF THIS FILE + +* Introduction +* Requirements +* Installation +* Configuration +* Maintainers + + +## INTRODUCTION + +The Real name module allows the admin to choose fields from the user profile +that will be used to add a "realname" element (method) to a user object. +Hook_user is used to automatically add this to any user object that is loaded. + + +* For a full description of the module visit + https://www.drupal.org/project/realname. + +* To submit bug reports and feature suggestions, or to track changes visit + https://www.drupal.org/project/issues/realname. + + +## REQUIREMENTS + +This module requires the following module: + + * Token - https://www.drupal.org/project/token + + +## INSTALLATION + +Install the Real name module as you would normally install a contributed +Drupal module. Visit https://www.drupal.org/node/1897420 for further +information. + + +## CONFIGURATION + +The settings page is at Administration >> Configuration >> People >> Real name. + +This pattern will be used to construct Realnames for all users. +Note that if the pattern is changed: all current Realnames will be deleted and +the list in the database will be rebuilt as needed. + + +## MAINTAINERS + +* Alexander Hass (hass) - https://www.drupal.org/u/hass diff --git a/web/modules/realname/composer.json b/web/modules/realname/composer.json new file mode 100644 index 0000000000000000000000000000000000000000..027764245d4a77162631adb6e3c356d25260c244 --- /dev/null +++ b/web/modules/realname/composer.json @@ -0,0 +1,24 @@ +{ + "name": "drupal/realname", + "description": "Provides token-based name displays for users.", + "type": "drupal-module", + "homepage": "https://www.drupal.org/project/realname", + "authors": [ + { + "name": "hass", + "homepage": "https://www.drupal.org/u/hass" + }, + { + "name": "See other contributors", + "homepage":"https://www.drupal.org/node/266280/committers" + } + ], + "support": { + "issues": "https://www.drupal.org/project/issues/realname", + "source": "https://git.drupal.org/project/realname.git" + }, + "license": "GPL-2.0+", + "require": { + "drupal/token": "^1.0.0-alpha2" + } +} diff --git a/web/modules/realname/config/install/realname.settings.yml b/web/modules/realname/config/install/realname.settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..9546e78dfacf086e45cbe9724b25942095689cdf --- /dev/null +++ b/web/modules/realname/config/install/realname.settings.yml @@ -0,0 +1 @@ +pattern: '[user:account-name]' diff --git a/web/modules/realname/config/install/system.action.realname_update_realname_action.yml b/web/modules/realname/config/install/system.action.realname_update_realname_action.yml new file mode 100644 index 0000000000000000000000000000000000000000..d46fa3c1c3999e9390186268fc154be429c7d5d8 --- /dev/null +++ b/web/modules/realname/config/install/system.action.realname_update_realname_action.yml @@ -0,0 +1,10 @@ +langcode: en +status: true +dependencies: + module: + - user +id: realname_update_realname_action +label: 'Update real names of the selected user(s)' +type: user +plugin: realname_update_realname_action +configuration: { } diff --git a/web/modules/realname/config/schema/realname.schema.yml b/web/modules/realname/config/schema/realname.schema.yml new file mode 100644 index 0000000000000000000000000000000000000000..f28a9ad837e89acc558b32108f0d02853c028c59 --- /dev/null +++ b/web/modules/realname/config/schema/realname.schema.yml @@ -0,0 +1,13 @@ +# Schema for the configuration files of the realname module. + +realname.settings: + type: config_object + label: 'Realname settings' + mapping: + pattern: + type: string + label: 'Realname pattern' + +action.configuration.realname_update_realname_action: + type: action_configuration_default + label: 'Update real name' diff --git a/web/modules/realname/migrations/d6_realname_settings.yml b/web/modules/realname/migrations/d6_realname_settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..a3326fbc3d18025b0e1dff133edd9b399d7a3b1e --- /dev/null +++ b/web/modules/realname/migrations/d6_realname_settings.yml @@ -0,0 +1,18 @@ +id: d6_realname_settings +label: Realname 6 configuration +migration_tags: + - Drupal 6 + - Configuration +source: + plugin: variable + variables: + - realname_pattern + source_module: realname +process: + pattern: + plugin: realname_replace_token + source: + - realname_pattern +destination: + plugin: config + config_name: realname.settings diff --git a/web/modules/realname/migrations/d7_realname_settings.yml b/web/modules/realname/migrations/d7_realname_settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..6b11af74df53070f23e4912637da27c10868653a --- /dev/null +++ b/web/modules/realname/migrations/d7_realname_settings.yml @@ -0,0 +1,18 @@ +id: d7_realname_settings +label: Realname 7 configuration +migration_tags: + - Drupal 7 + - Configuration +source: + plugin: variable + variables: + - realname_pattern + source_module: realname +process: + pattern: + plugin: realname_replace_token + source: + - realname_pattern +destination: + plugin: config + config_name: realname.settings diff --git a/web/modules/realname/realname.api.php b/web/modules/realname/realname.api.php new file mode 100644 index 0000000000000000000000000000000000000000..09e813e57c599658ebd21c2cea19b57b41c03304 --- /dev/null +++ b/web/modules/realname/realname.api.php @@ -0,0 +1,58 @@ +<?php + +/** + * @file + * Hooks provided by the Real name module. + */ + +/** + * @addtogroup hooks + * @{ + */ + +/** + * Alter the pattern for a user's real name prior to generation. + * + * @param string $pattern + * The real name pattern string prior to token replacement. + * @param object $account + * A user account object. + * + * @see realname_update() + * + * @ingroup realname + */ +function hook_realname_pattern_alter(&$pattern, $account) { + +} + +/** + * Alter a user's real name before it is saved to the database. + * + * @param string $realname + * The user's generated real name. + * @param object $account + * A user account object. + * + * @see realname_update() + * + * @ingroup realname + */ +function hook_realname_alter(&$realname, $account) { + +} + +/** + * Respond to updates to an account's real name. + * + * @see realname_update() + * + * @ingroup realname + */ +function hook_realname_update($realname, $account) { + +} + +/** + * @} End of "addtogroup hooks". + */ diff --git a/web/modules/realname/realname.info.yml b/web/modules/realname/realname.info.yml new file mode 100644 index 0000000000000000000000000000000000000000..d2f58b16768d0632271a5385ee3b4d18d5612ed6 --- /dev/null +++ b/web/modules/realname/realname.info.yml @@ -0,0 +1,13 @@ +name: 'Real Name' +type: module +description: 'Provides token-based name displays for users.' +configure: realname.admin_settings_form +core: 8.x +core_version_requirement: ^8 || ^9 +dependencies: + - token:token + +# Information added by Drupal.org packaging script on 2021-05-13 +version: '8.x-1.0-rc2+11-dev' +project: 'realname' +datestamp: 1620882450 diff --git a/web/modules/realname/realname.install b/web/modules/realname/realname.install new file mode 100644 index 0000000000000000000000000000000000000000..0fa66a5a669317b7d04dceb5755268187073bd58 --- /dev/null +++ b/web/modules/realname/realname.install @@ -0,0 +1,50 @@ +<?php + +/** + * @file + * Installation file for Realname module. + */ + +/** + * Implements hook_schema(). + */ +function realname_schema() { + $schema['realname'] = [ + 'description' => 'Computed Real Names to reduce overhead.', + 'fields' => [ + 'uid' => [ + 'description' => 'User ID, links to User table.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ], + 'realname' => [ + 'description' => 'The generated real name of the user.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ], + 'created' => [ + 'description' => 'The UNIX timestamp of when the real name was created.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ], + ], + 'primary key' => ['uid'], + 'indexes' => [ + 'realname' => ['realname'], + ], + 'foreign keys' => [ + 'users' => [ + 'table' => 'users', + 'columns' => ['uid' => 'uid'], + ], + ], + ]; + + return $schema; +} diff --git a/web/modules/realname/realname.links.menu.yml b/web/modules/realname/realname.links.menu.yml new file mode 100644 index 0000000000000000000000000000000000000000..6430d3793e547504cef5d5a7145982c776255a86 --- /dev/null +++ b/web/modules/realname/realname.links.menu.yml @@ -0,0 +1,5 @@ +realname.admin_settings_form: + title: 'Real name' + parent: user.admin_index + description: 'Use tokens to configure how user names are displayed.' + route_name: realname.admin_settings_form diff --git a/web/modules/realname/realname.links.task.yml b/web/modules/realname/realname.links.task.yml new file mode 100644 index 0000000000000000000000000000000000000000..8e0d6cb649974710801cf9b35dcf160cca9d525a --- /dev/null +++ b/web/modules/realname/realname.links.task.yml @@ -0,0 +1,4 @@ +realname.admin_settings_form_tab: + route_name: realname.admin_settings_form + title: 'Real name' + base_route: entity.user.admin_form diff --git a/web/modules/realname/realname.module b/web/modules/realname/realname.module new file mode 100644 index 0000000000000000000000000000000000000000..b07b867d1f991222944ea065e6b2a787b8d26ac9 --- /dev/null +++ b/web/modules/realname/realname.module @@ -0,0 +1,291 @@ +<?php + +/** + * @file + * Provides token-based name displays for users. + * + * @todo Add a 'view realname' permission enabled by default + * @todo Allow users to login with their real name + * @todo Disable the username field + */ + +use Drupal\Component\Utility\Html; +use Drupal\Component\Utility\Unicode; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Link; +use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Url; +use Drupal\user\Entity\User; + +/** + * @defgroup realname Real name API + */ + +/** + * Implements hook_help(). + */ +function realname_help($route_name, RouteMatchInterface $route_match) { + switch ($route_name) { + // Main module help for the Realname module. + case 'realname.admin_settings_form': + case 'help.page.realname': + return '<p>' . t("A Real Name is what the site developer decides that users' names should look like. It is constructed from various tokens that are available within the site.") . '</p>'; + } +} + +/** + * Implements hook_entity_extra_field_info(). + */ +function realname_entity_extra_field_info() { + $fields['user']['user']['display']['realname'] = [ + 'label' => t('Real name'), + 'description' => t('Real name'), + 'weight' => -1, + 'visible' => FALSE, + ]; + + return $fields; +} + +/** + * Implements hook_user_format_name_alter(). + */ +function realname_user_format_name_alter(&$name, AccountInterface $account) { + static $in_username_alter = FALSE; + $uid = $account->id(); + + // Don't alter anonymous users or objects that do not have any user ID. + if (empty($uid)) { + return; + } + + // Real name was loaded/generated via hook_user_load(), so re-use it. + if (isset($account->realname)) { + if (mb_strlen($account->realname)) { + // Only if the real name is a non-empty string is $name actually altered. + $name = $account->realname; + } + return; + } + + // Real name was not yet available for the account so we need to generate it. + // Because tokens may call format_username() we need to prevent recursion. + if (!$in_username_alter) { + $in_username_alter = TRUE; + + // If $account->realname was undefined, then the user account object was + // not properly loaded. We must enforce calling user_load(). + if ($realname_account = User::load($uid)) { + realname_user_format_name_alter($name, $realname_account); + } + + $in_username_alter = FALSE; + } +} + +/** + * Implements hook_ENTITY_TYPE_load(). + */ +function realname_user_load(array $accounts) { + $realnames = realname_load_multiple($accounts); + foreach ($realnames as $uid => $realname) { + $accounts[$uid]->realname = $realname; + } +} + +/** + * Implements hook_ENTITY_TYPE_update(). + */ +function realname_user_update(EntityInterface $account) { + // Since user data may have changed, update the realname and its cache. + $realnames = &drupal_static('realname_load_multiple', []); + $realnames[$account->id()] = realname_update($account); +} + +/** + * Implements hook_ENTITY_TYPE_delete(). + */ +function realname_user_delete(EntityInterface $account) { + realname_delete($account->id()); +} + +/** + * Implements hook_ENTITY_TYPE_view() for user entities. + */ +function realname_user_view(array &$build, EntityInterface $account, EntityViewDisplayInterface $display, $view_mode) { + if ($display->getComponent('realname')) { + if ($account->access('view')) { + $url = Url::fromRoute('entity.user.canonical', ['user' => $account->id()]); + $markup = Link::fromTextAndUrl($account->realname, $url)->toString(); + } + else { + $markup = Html::escape($account->realname); + } + + $build['realname'] = [ + '#theme' => 'field', + '#title' => t('Real name'), + '#label_display' => 'inline', + '#view_mode' => '_custom', + '#field_name' => 'realname', + '#field_type' => 'text', + '#field_translatable' => FALSE, + '#entity_type' => 'custom', + '#bundle' => 'custom', + '#object' => $account, + '#items' => [TRUE], + '#is_multiple' => FALSE, + 0 => [ + '#markup' => $markup, + ], + ]; + } +} + +/** + * @addtogroup realname + * @{ + */ + +/** + * Loads a real name. + * + * @param Drupal\user\Entity\User $account + * A user account object. + * + * @return mixed + * The user's generated real name. + */ +function realname_load(User $account) { + $realnames = realname_load_multiple([$account->id() => $account]); + return reset($realnames); +} + +/** + * Loads multiple real names. + * + * @param array $accounts + * An array of user account objects keyed by user ID. + * + * @return array + * An array of real names keyed by user ID. + */ +function realname_load_multiple(array $accounts) { + $realnames = &drupal_static(__FUNCTION__, []); + + if ($new_accounts = array_diff_key($accounts, $realnames)) { + // Attempt to fetch realnames from the database first. + $realnames += \Drupal::database()->query("SELECT uid, realname FROM {realname} WHERE uid IN (:uids[])", [':uids[]' => array_keys($new_accounts)])->fetchAllKeyed(); + + // For each account that was not present in the database, generate its + // real name. + foreach ($new_accounts as $uid => $account) { + if (!isset($realnames[$uid])) { + $realnames[$uid] = realname_update($account); + } + } + } + + return array_intersect_key($realnames, $accounts); +} + +/** + * Update the realname for a user account. + * + * @param Drupal\user\Entity\User $account + * A user account object. + * + * @return string + * A string with the real name. + * + * @see hook_realname_pattern_alter() + * @see hook_realname_alter() + * @see hook_realname_update() + */ +function realname_update(User $account) { + $realname = ''; + + if (!$account->isAnonymous()) { + // Get the default pattern and allow other modules to alter it. + $config = \Drupal::config('realname.settings'); + $pattern = $config->get('pattern'); + \Drupal::moduleHandler()->alter('realname_pattern', $pattern, $account); + + // Perform token replacement on the real name pattern. + $realname = \Drupal::token()->replace($pattern, ['user' => $account], ['clear' => TRUE, 'sanitize' => FALSE]); + + // Remove any HTML tags. + $realname = strip_tags(Html::decodeEntities($realname)); + + // Remove double spaces (if a token had no value). + $realname = preg_replace('/ {2,}/', ' ', $realname); + + // Allow other modules to alter the generated realname. + \Drupal::moduleHandler()->alter('realname', $realname, $account); + + // The name must be trimmed to 255 characters before inserting into the + // database. + $realname = Unicode::truncate(trim($realname), 255); + } + else { + // DisplayName cannot generated with tokens for anonymous users. + $realname = $account->label(); + } + + // Save to the database and the static cache. + \Drupal::database()->merge('realname') + ->key(['uid' => $account->id()]) + ->fields([ + 'realname' => $realname, + 'created' => \Drupal::time()->getRequestTime(), + ]) + ->execute(); + + // Allow modules to react to the realname being updated. + \Drupal::moduleHandler()->invokeAll('realname_update', [$realname, $account]); + + // Clear the entity cache. + /** @var \Drupal\user\UserStorageInterface $user_storage */ + $user_storage = \Drupal::entityTypeManager()->getStorage('user'); + $user_storage->resetCache([$account->id()]); + + return $realname; +} + +/** + * Delete a real name. + * + * @param int $uid + * A user ID. + */ +function realname_delete($uid) { + realname_delete_multiple([$uid]); +} + +/** + * Delete multiple real names. + * + * @param array $uids + * An array of user IDs. + */ +function realname_delete_multiple(array $uids) { + \Drupal::database()->delete('realname')->condition('uid', $uids, 'IN')->execute(); + drupal_static_reset('realname_load_multiple'); + \Drupal::entityTypeManager()->getStorage('user')->resetCache($uids); +} + +/** + * Delete all real names. + */ +function realname_delete_all() { + \Drupal::database()->truncate('realname')->execute(); + drupal_static_reset('realname_load_multiple'); + \Drupal::entityTypeManager()->getStorage('user')->resetCache(); +} + +/** + * @} End of "addtogroup realname". + */ + diff --git a/web/modules/realname/realname.permissions.yml b/web/modules/realname/realname.permissions.yml new file mode 100644 index 0000000000000000000000000000000000000000..6d93be6c8e757e7957ec29dd2c95540ec2fd73b9 --- /dev/null +++ b/web/modules/realname/realname.permissions.yml @@ -0,0 +1,3 @@ +administer realname: + title: 'Administer Real Name' + description: 'Allows users to administer realname settings.' diff --git a/web/modules/realname/realname.routing.yml b/web/modules/realname/realname.routing.yml new file mode 100644 index 0000000000000000000000000000000000000000..c65b1e64689a0c00118836cd324cf7a4a3200d18 --- /dev/null +++ b/web/modules/realname/realname.routing.yml @@ -0,0 +1,7 @@ +realname.admin_settings_form: + path: '/admin/config/people/realname' + defaults: + _form: 'Drupal\realname\Form\RealnameAdminSettingsForm' + _title: 'Real name' + requirements: + _permission: 'administer realname' diff --git a/web/modules/realname/realname.services.yml b/web/modules/realname/realname.services.yml new file mode 100644 index 0000000000000000000000000000000000000000..cf4fe9bb1310e3882bbced712ac26738e1a53b81 --- /dev/null +++ b/web/modules/realname/realname.services.yml @@ -0,0 +1,11 @@ +services: + realname.config_cache_tag: + class: Drupal\realname\EventSubscriber\ConfigCacheTag + arguments: ['@cache_tags.invalidator'] + tags: + - { name: event_subscriber } + + realname.route.subscriber: + class: Drupal\realname\Routing\RealnameRouteSubscriber + tags: + - { name: event_subscriber } diff --git a/web/modules/realname/realname.views.inc b/web/modules/realname/realname.views.inc new file mode 100644 index 0000000000000000000000000000000000000000..a4af06a930ecee3fe5c9df3d67a9d8e2881c3c1b --- /dev/null +++ b/web/modules/realname/realname.views.inc @@ -0,0 +1,41 @@ +<?php + +/** + * @file + * Views integration for the realname module. + */ + +/** + * Implements hook_views_data(). + */ +function realname_views_data() { + $data['realname']['table']['group'] = t('Realname'); + $data['realname']['table']['join'] = [ + 'users' => [ + 'left_field' => 'uid', + 'field' => 'uid', + ], + ]; + + $data['realname']['realname'] = [ + 'title' => t('Real name'), + 'help' => t("The user's real name."), + 'field' => [ + 'handler' => 'views_handler_field_user', + 'click sortable' => TRUE, + ], + 'sort' => [ + 'handler' => 'views_handler_sort', + ], + 'argument' => [ + 'handler' => 'views_handler_argument_string', + ], + 'filter' => [ + 'handler' => 'views_handler_filter_string', + 'title' => t('Name'), + 'help' => t("The user's real name. This filter does not check if the user exists and allows partial matching. Does not utilize autocomplete."), + ], + ]; + + return $data; +} diff --git a/web/modules/realname/src/Controller/RealnameAutocompleteController.php b/web/modules/realname/src/Controller/RealnameAutocompleteController.php new file mode 100644 index 0000000000000000000000000000000000000000..77aeea2cb67dbe7faec6841a53961359a3258a3f --- /dev/null +++ b/web/modules/realname/src/Controller/RealnameAutocompleteController.php @@ -0,0 +1,106 @@ +<?php + +namespace Drupal\realname\Controller; + +use Drupal\system\Controller\EntityAutocompleteController; +use Drupal\Component\Utility\Crypt; +use Drupal\Component\Utility\Tags; +use Drupal\Core\Database\Query\Condition; +use Drupal\Core\Site\Settings; +use Drupal\user\Entity\User; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; + +/** + * Defines a route controller for entity autocomplete form elements. + */ +class RealnameAutocompleteController extends EntityAutocompleteController { + + /** + * {@inheritdoc} + */ + public function handleAutocomplete(Request $request, $target_type, $selection_handler, $selection_settings_key) { + if ($target_type != 'user') { + return parent::handleAutocomplete($request, $target_type, $selection_handler, $selection_settings_key); + } + + $matches = []; + if ($input = $request->query->get('q')) { + $typed_string = Tags::explode($input); + $typed_string = mb_strtolower(array_pop($typed_string)); + $selection_settings = $this->keyValue->get($selection_settings_key, FALSE); + if ($selection_settings !== FALSE) { + $selection_settings_hash = Crypt::hmacBase64(serialize($selection_settings) . $target_type . $selection_handler, Settings::getHashSalt()); + if ($selection_settings_hash !== $selection_settings_key) { + throw new AccessDeniedHttpException('Invalid selection settings key.'); + } + } + else { + throw new AccessDeniedHttpException(); + } + + $matches = $this->getMatches($selection_settings, $typed_string); + } + + return new JsonResponse($matches); + } + + /** + * Gets matched labels based on a given search string. + * + * @param array $selection_settings + * An array of settings that will be passed to the selection handler. + * @param string $string + * (optional) The label of the entity to query by. + * + * @return array + * An array of matched entity labels, in the format required by the AJAX + * autocomplete API (e.g. array('value' => $value, 'label' => $label)). + * + * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * Thrown when the current user doesn't have access to the specified entity. + */ + protected function getMatches(array $selection_settings, $string = '') { + $matches = []; + + if (isset($string)) { + // Get an array of matching entities. + $match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS'; + $include_anonymous = isset($selection_settings['include_anonymous']) ? $selection_settings['include_anonymous'] : TRUE; + + $connection = \Drupal::database(); + $query = $connection->select('users_field_data', 'u'); + $query->fields('u', ['uid']); + $query->leftJoin('realname', 'rn', 'u.uid = rn.uid'); + if ($match_operator == 'CONTAINS') { + $query->condition((new Condition('OR')) + ->condition('rn.realname', '%' . $connection->escapeLike($string) . '%', 'LIKE') + ->condition('u.name', '%' . $connection->escapeLike($string) . '%', 'LIKE') + ); + } + else { + $query->condition((new Condition('OR')) + ->condition('rn.realname', $connection->escapeLike($string) . '%', 'LIKE') + ->condition('u.name', $connection->escapeLike($string) . '%', 'LIKE') + ); + } + if ($include_anonymous == FALSE) { + $query->condition('u.uid', 0, '>'); + } + $query->range(0, 10); + $uids = $query->execute()->fetchCol(); + $accounts = User::loadMultiple($uids); + + /* @var $account User */ + foreach ($accounts as $account) { + $matches[] = [ + 'value' => $this->t('@realname (@id)', ['@realname' => $account->getDisplayName(), '@id' => $account->id()]), + 'label' => $this->t('@realname (@username)', ['@realname' => $account->getDisplayName(), '@username' => $account->getAccountName()]), + ]; + } + } + return $matches; + } + +} diff --git a/web/modules/realname/src/EventSubscriber/ConfigCacheTag.php b/web/modules/realname/src/EventSubscriber/ConfigCacheTag.php new file mode 100644 index 0000000000000000000000000000000000000000..a3144fcdb9adc2970c52842c3df7ece4f63aa211 --- /dev/null +++ b/web/modules/realname/src/EventSubscriber/ConfigCacheTag.php @@ -0,0 +1,58 @@ +<?php + +namespace Drupal\realname\EventSubscriber; + +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; +use Drupal\Core\Config\ConfigCrudEvent; +use Drupal\Core\Config\ConfigEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * A subscriber invalidating cache tags when realname config objects are saved. + */ +class ConfigCacheTag implements EventSubscriberInterface { + + /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $cacheTagsInvalidator; + + /** + * Constructs a RealnameCacheTag object. + * + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator + * The cache tags invalidator. + */ + public function __construct(CacheTagsInvalidatorInterface $cache_tags_invalidator) { + $this->cacheTagsInvalidator = $cache_tags_invalidator; + } + + /** + * Invalidate cache tags when particular realname config objects are saved. + * + * @param \Drupal\Core\Config\ConfigCrudEvent $event + * The Event to process. + */ + public function onSave(ConfigCrudEvent $event) { + // Check if realname settings object has been changed. + if ($event->getConfig()->getName() === 'realname.settings') { + // Clear the realname cache if the pattern was changed. + realname_delete_all(); + + // A change to the display-name pattern must invalidate the render cache + // since the display-name could be used anywhere. + $this->cacheTagsInvalidator->invalidateTags(['rendered']); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[ConfigEvents::SAVE][] = ['onSave']; + return $events; + } + +} diff --git a/web/modules/realname/src/Form/RealnameAdminSettingsForm.php b/web/modules/realname/src/Form/RealnameAdminSettingsForm.php new file mode 100644 index 0000000000000000000000000000000000000000..d8c9a7e9d982bad0f8d94b50b17ca0d845ef26a0 --- /dev/null +++ b/web/modules/realname/src/Form/RealnameAdminSettingsForm.php @@ -0,0 +1,95 @@ +<?php + +namespace Drupal\realname\Form; + +use Drupal\Core\Form\ConfigFormBase; +use Drupal\Core\Form\FormStateInterface; + +/** + * Configure Realname settings for this site. + */ +class RealnameAdminSettingsForm extends ConfigFormBase { + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'realname_admin_settings'; + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return ['realname.settings']; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $config = $this->config('realname.settings'); + + $form['general'] = [ + '#type' => 'fieldset', + '#title' => $this->t('General settings'), + ]; + + $note = '<div>'; + $note .= $this->t('Note that if it is changed, all current Realnames will be deleted and the list in the database will be rebuilt as needed.'); + $note .= '</div>'; + + $form['general']['realname_pattern'] = [ + '#type' => 'textfield', + '#title' => $this->t('Realname pattern'), + '#default_value' => $config->get('pattern'), + '#element_validate' => ['token_element_validate'], + '#token_types' => ['user'], + '#min_tokens' => 1, + '#required' => TRUE, + '#maxlength' => 256, + '#description' => $this->t('This pattern will be used to construct Realnames for all users.') . $note, + ]; + // Add the token tree UI. + $form['general']['token_help'] = [ + '#theme' => 'token_tree_link', + '#token_types' => ['user'], + '#global_types' => FALSE, + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + parent::validateForm($form, $form_state); + + $pattern = $form_state->getValue('realname_pattern'); + + // Tokens that will cause recursion. + $tokens = [ + '[user:name]', + ]; + foreach ($tokens as $token) { + if (strpos($pattern, $token) !== FALSE) { + $form_state->setErrorByName('realname_pattern', $this->t('The %token token cannot be used as it will cause recursion.', ['%token' => $token])); + } + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $config = $this->config('realname.settings'); + + if ($form['general']['realname_pattern']['#default_value'] != $form_state->getValue('realname_pattern')) { + $config->set('pattern', $form_state->getValue('realname_pattern'))->save(); + } + + parent::submitForm($form, $form_state); + } + +} diff --git a/web/modules/realname/src/Plugin/Action/RealnameUpdateRealname.php b/web/modules/realname/src/Plugin/Action/RealnameUpdateRealname.php new file mode 100644 index 0000000000000000000000000000000000000000..ec0d7e2716bbf84f7e32b3fae9fd00cc99585096 --- /dev/null +++ b/web/modules/realname/src/Plugin/Action/RealnameUpdateRealname.php @@ -0,0 +1,37 @@ +<?php + +namespace Drupal\realname\Plugin\Action; + +use Drupal\Core\Action\ActionBase; +use Drupal\Core\Session\AccountInterface; + +/** + * Update user real name. + * + * @Action( + * id = "realname_update_realname_action", + * label = @Translation("Update real name"), + * type = "user" + * ) + */ +class RealnameUpdateRealname extends ActionBase { + + /** + * {@inheritdoc} + */ + public function execute($account = NULL) { + realname_update($account); + } + + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + /** @var \Drupal\user\UserInterface $object */ + $access = $object->status->access('edit', $account, TRUE) + ->andIf($object->access('update', $account, TRUE)); + + return $return_as_object ? $access : $access->isAllowed(); + } + +} diff --git a/web/modules/realname/src/Plugin/migrate/process/RealnameReplaceToken.php b/web/modules/realname/src/Plugin/migrate/process/RealnameReplaceToken.php new file mode 100644 index 0000000000000000000000000000000000000000..f800f1dab05659e5192da61a62d11ac4850f9334 --- /dev/null +++ b/web/modules/realname/src/Plugin/migrate/process/RealnameReplaceToken.php @@ -0,0 +1,33 @@ +<?php + +namespace Drupal\realname\Plugin\migrate\process; + +use Drupal\migrate\ProcessPluginBase; +use Drupal\migrate\MigrateExecutableInterface; +use Drupal\migrate\Row; + +/** + * If the source evaluates to empty, we skip the current row. + * + * @MigrateProcessPlugin( + * id = "realname_replace_token", + * handle_multiples = TRUE + * ) + */ +class RealnameReplaceToken extends ProcessPluginBase { + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + list($realname_pattern) = $value; + + // Previous D7 realname token need to be replaced by D8 core token. + // + // At least two tokens may exists: + // - [user:name-raw] + // - [current-user:name-raw] + return str_ireplace(':name-raw]', ':account-name]', $realname_pattern); + } + +} diff --git a/web/modules/realname/src/Routing/RealnameRouteSubscriber.php b/web/modules/realname/src/Routing/RealnameRouteSubscriber.php new file mode 100644 index 0000000000000000000000000000000000000000..4c57b383ea9f88d1f84018310c022642e2cad3d2 --- /dev/null +++ b/web/modules/realname/src/Routing/RealnameRouteSubscriber.php @@ -0,0 +1,22 @@ +<?php + +namespace Drupal\realname\Routing; + +use Drupal\Core\Routing\RouteSubscriberBase; +use Symfony\Component\Routing\RouteCollection; + +/** + * Subscriber for routes. + */ +class RealnameRouteSubscriber extends RouteSubscriberBase { + + /** + * {@inheritdoc} + */ + protected function alterRoutes(RouteCollection $collection) { + if ($route = $collection->get('system.entity_autocomplete')) { + $route->setDefault('_controller', '\Drupal\realname\Controller\RealnameAutocompleteController::handleAutocomplete'); + } + } + +} diff --git a/web/modules/realname/tests/src/Functional/RealnameBasicTest.php b/web/modules/realname/tests/src/Functional/RealnameBasicTest.php new file mode 100644 index 0000000000000000000000000000000000000000..fecac08ff084321340f5e06ef4709bf56bd6a793 --- /dev/null +++ b/web/modules/realname/tests/src/Functional/RealnameBasicTest.php @@ -0,0 +1,186 @@ +<?php + +namespace Drupal\Tests\realname\Functional; + +use Drupal\Tests\BrowserTestBase; +use Drupal\user\Entity\User; + +/** + * Test basic functionality of Realname module. + * + * @group Realname + */ +class RealnameBasicTest extends BrowserTestBase { + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * The user to set up realname. + * + * @var \Drupal\user\UserInterface + */ + protected $adminUser; + + /** + * {@inheritdoc} + */ + protected static $modules = [ + 'realname', + 'field_ui', + ]; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $permissions = [ + 'access administration pages', + 'administer modules', + 'administer realname', + 'administer site configuration', + 'administer user fields', + 'administer user form display', + 'administer user display', + 'administer users', + ]; + + $this->adminUser = $this->drupalCreateUser($permissions); + $this->drupalLogin($this->adminUser); + } + + /** + * Test realname configuration. + */ + public function testRealnameConfiguration() { + $assert_session = $this->assertSession(); + + // Check if Configure link is available on 'Modules' page. + // Requires 'administer modules' permission. + $this->drupalGet('admin/modules'); + // Assert that the configure link from Modules page to Realname settings + // page exists. + $assert_session->responseContains('admin/config/people/realname'); + + // Check for setting page's presence. + $this->drupalGet('admin/config/people/realname'); + // Assert the settings page is displayed. + $assert_session->pageTextContains(t('Realname pattern')); + + // Save form with allowed token. + $edit['realname_pattern'] = '[user:account-name]'; + $this->drupalGet('admin/config/people/realname'); + $this->submitForm($edit, 'Save configuration'); + + $this->drupalGet('admin/config/people/realname'); + $this->submitForm($edit, 'Save configuration'); + + // Assert that the settings form has been saved. + $assert_session->pageTextContains(t('The configuration options have been saved.')); + + // Check token recursion protection. + $edit['realname_pattern'] = '[user:name]'; + $this->drupalGet('admin/config/people/realname'); + $this->submitForm($edit, 'Save configuration'); + // Assert that an invalid token is found. + $assert_session->pageTextContains(t('The [user:name] token cannot be used as it will cause recursion.')); + } + + /** + * Test realname alter functions. + */ + public function testRealnameUsernameAlter() { + $assert_session = $this->assertSession(); + + // Add a test string and see if core username has been replaced by realname. + $edit['realname_pattern'] = '[user:account-name] (UID: [user:uid])'; + $this->drupalGet('admin/config/people/realname'); + $this->submitForm($edit, 'Save configuration'); + + // Assert real name is shown on the user page. + $this->drupalGet('user/' . $this->adminUser->id()); + $assert_session->pageTextContains($this->adminUser->getDisplayName()); + + // Assert real name is shown on the user edit page. + $this->drupalGet('user/' . $this->adminUser->id() . '/edit'); + $assert_session->pageTextContains($this->adminUser->getDisplayName()); + + /** @var \Drupal\user\Entity\User $user_account */ + $user_account = $this->adminUser; + $username_before = $user_account->getAccountName(); + $user_account->save(); + $username_after = $user_account->getAccountName(); + $this->assertEquals($username_after, $username_before, 'Username did not change after save'); + } + + /** + * Test realname display configuration. + */ + public function testRealnameManageDisplay() { + $assert_session = $this->assertSession(); + + $edit['realname_pattern'] = '[user:account-name]'; + $this->drupalGet('admin/config/people/realname'); + $this->submitForm($edit, 'Save configuration'); + + $this->drupalGet('admin/config/people/accounts/fields'); + $assert_session->titleEquals('Manage fields | Drupal'); + // Assert real name field is not shown in manage fields list. + $assert_session->pageTextNotContains('Real name'); + + $this->drupalGet('admin/config/people/accounts/form-display'); + $this->assertSession()->titleEquals('Manage form display | Drupal'); + // Assert real name field is not shown in manage form display list. + $assert_session->pageTextNotContains('Real name'); + + $this->drupalGet('admin/config/people/accounts/display'); + $this->assertSession()->titleEquals('Manage display | Drupal'); + // Assert real name field is shown in manage display. + $assert_session->pageTextContains('Real name'); + + // By default the realname field is not visible. + $this->drupalGet('user/' . $this->adminUser->id()); + // Assert real name field not visible on user page. + $assert_session->pageTextNotContains('Real name'); + + // Make realname field visible on user page. + $this->drupalGet('admin/config/people/accounts/display'); + $edit = ['fields[realname][region]' => 'content']; + $this->submitForm($edit, 'Save'); + $assert_session->statusCodeEquals(200); + + $this->drupalGet('user/' . $this->adminUser->id()); + // Assert real name field is visible on user page. + $assert_session->pageTextContains('Real name'); + } + + /** + * Test realname user update. + */ + public function testRealnameUserUpdate() { + $edit['realname_pattern'] = '[user:account-name]'; + $this->drupalGet('admin/config/people/realname'); + $this->submitForm($edit, 'Save configuration'); + + $user1 = User::load($this->adminUser->id()); + $realname1 = $user1->realname; + + // Update user name. + $user1->name = $this->randomMachineName(); + $user1->save(); + + // Reload the user. + $user2 = User::load($this->adminUser->id()); + $realname2 = $user2->realname; + + // Check if realname changed. + $this->assertNotEmpty($realname1); + $this->assertNotEmpty($realname2); + $this->assertNotEquals($realname1, $realname2, '[testRealnameUserUpdate]: Real name changed.'); + } + +}