first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

42
modules/README.txt Executable file
View File

@@ -0,0 +1,42 @@
Modules extend your site functionality beyond Drupal core.
WHAT TO PLACE IN THIS DIRECTORY?
--------------------------------
Placing downloaded and custom modules in this directory separates downloaded and
custom modules from Drupal core's modules. This allows Drupal core to be updated
without overwriting these files.
DOWNLOAD ADDITIONAL MODULES
---------------------------
Contributed modules from the Drupal community may be downloaded at
https://www.drupal.org/project/project_module.
ORGANIZING MODULES IN THIS DIRECTORY
------------------------------------
You may create subdirectories in this directory, to organize your added modules,
without breaking the site. Some common subdirectories include "contrib" for
contributed modules, and "custom" for custom modules. Note that if you move a
module to a subdirectory after it has been enabled, you may need to clear the
Drupal cache so it can be found.
There are number of directories that are ignored when looking for modules. These
are 'src', 'lib', 'vendor', 'assets', 'css', 'files', 'images', 'js', 'misc',
'templates', 'includes', 'fixtures' and 'Drupal'.
MULTISITE CONFIGURATION
-----------------------
In multisite configurations, modules found in this directory are available to
all sites. You may also put modules in the sites/all/modules directory, and the
versions in sites/all/modules will take precedence over versions of the same
module that are here. Alternatively, the sites/your_site_name/modules directory
pattern may be used to restrict modules to a specific site instance.
MORE INFORMATION
----------------
Refer to the “Developing for Drupal” section of the README.md in the Drupal
root directory for further information on extending Drupal with custom modules.

View File

@@ -0,0 +1,2 @@
* For a full list of fixes in the latest release, visit:
https://www.drupal.org/project/admin_toolbar/releases

View File

@@ -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.

View File

@@ -0,0 +1,66 @@
# Admin Toolbar
Admin Toolbar intends to improve the default Drupal Toolbar to transform it into
a drop-down menu, providing a fast and full access to all administration links.
For a full description of the module, visit the
[project page](https://www.drupal.org/project/admin_toolbar).
Submit bug reports and feature suggestions, or track changes in the
[issue queue](https://www.drupal.org/project/issues/search/admin_toolbar).
## Table of contents
- Requirements
- Recommended modules
- Installation
- Configuration
- Maintainers
## Requirements
This module requires the following modules:
- [Toolbar](https://www.drupal.org/project/toolbar)
- [Breakpoint](https://www.drupal.org/project/breakpoint)
## Recommended modules
[Admin Toolbar Extra Tools](https://www.drupal.org/project/admin_toolbar):
Provides menu links to administration pages or actions (eg. Flushing caches)
that are not generated by Drupal core.
[Admin Toolbar Links Access Filter](https://www.drupal.org/node/2474539):
Provides a workaround for the common problem that users with
'Use the administration pages and help' permission see menu links they don't
have access permission for.
## Installation
Install as you would normally install a contributed Drupal module. For further
information, see
[Installing Drupal Modules](https://www.drupal.org/docs/extending-drupal/installing-drupal-modules).
## Configuration
Configure the admin toolbar tools at (/admin/config/user-interface/admin-toolbar).
## Maintainers
Current maintainers:
- [Romain Jarraud (romainj)](https://www.drupal.org/u/romainj)
- [Adrian Cid Almaguer (adriancid)](https://www.drupal.org/u/adriancid)
- [Wilfrid Roze (eme)](https://www.drupal.org/u/eme)
- [bilel khalil (bolbol)](https://www.drupal.org/u/bolbol)
- [fethi.krout (fethi.krout)](https://www.drupal.org/u/fethi.krout)
- [Mohamed Anis Taktak (matio89)](https://www.drupal.org/u/matio89)
- [Thomas MUSA (Musa.thomas)](https://www.drupal.org/u/musathomas)
Supporting organizations:
- [emerya](https://www.drupal.org/emerya) Created this module for you!
- [Trained People](https://www.drupal.org/trained-people) Sponsored the module development
- [Drupiter](https://www.drupal.org/drupiter) Sponsored the module development
- [Dropteam](https://www.drupal.org/dropteam) Sponsored the module development
- [Alliance of Digital Builders (AODB)](https://www.drupal.org/alliance-of-digital-builders-aodb) Sponsored the module development

View File

@@ -0,0 +1,13 @@
name: Admin Toolbar
description: Provides an improved drop-down menu interface to the site Toolbar.
package: Administration
type: module
configure: admin_toolbar.settings
core_version_requirement: ^9.2 || ^10
dependencies:
- drupal:toolbar
# Information added by Drupal.org packaging script on 2023-09-29
version: '3.4.2'
project: 'admin_toolbar'
datestamp: 1696006156

View File

@@ -0,0 +1,30 @@
<?php
/**
* @file
* Install, update and uninstall functions for the Admin Toolbar module.
*/
/**
* Rebuild routes to mitigate issue 2938884.
*
* @see https://www.drupal.org/project/admin_toolbar/issues/2938884
*/
function admin_toolbar_update_8001() {
// Rebuilding the route cache.
\Drupal::service("router.builder")->rebuild();
}
/**
* Add menu_depth param into the config.
*
* @see https://www.drupal.org/project/admin_toolbar/issues/3200542
*/
function admin_toolbar_update_8002() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('admin_toolbar.settings');
if (empty($config->get('menu_depth'))) {
$config->set('menu_depth', 4);
$config->save(TRUE);
}
}

View File

@@ -0,0 +1,21 @@
toolbar.tree:
css:
theme:
css/admin.toolbar.css: {}
js:
js/admin_toolbar.js: {}
dependencies:
- core/jquery
- core/drupal
toolbar.tree.hoverintent:
js:
js/jquery.hoverIntent.js: {}
js/admin_toolbar.hoverintent.js: {}
dependencies:
- core/jquery
toolbar.tree.hover:
js:
js/admin_toolbar.hover.js: {}
dependencies:
- core/jquery

View File

@@ -0,0 +1,5 @@
admin_toolbar.settings:
title: 'Admin Toolbar'
description: 'Configure the Admin Toolbar module.'
route_name: admin_toolbar.settings
parent: system.admin_config_ui

View File

@@ -0,0 +1,74 @@
<?php
/**
* @file
* This is the module to create a drop-down menu for the core toolbar.
*/
use Drupal\admin_toolbar\Render\Element\AdminToolbar;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
/**
* Implements hook_toolbar_alter().
*/
function admin_toolbar_toolbar_alter(&$items) {
$items['administration']['tray']['toolbar_administration']['#pre_render'] = [
[AdminToolbar::class, 'preRenderTray'],
];
$items['administration']['#attached']['library'][] = 'admin_toolbar/toolbar.tree';
$hoverintent_functionality = \Drupal::config('admin_toolbar_tools.settings')->get('hoverintent_functionality');
if ($hoverintent_functionality === TRUE) {
// Use jQuery hover() effect.
$items['administration']['#attached']['library'][] = 'admin_toolbar/toolbar.tree.hoverintent';
}
else {
// User hoverIntent plugin.
$items['administration']['#attached']['library'][] = 'admin_toolbar/toolbar.tree.hover';
}
}
/**
* Implements hook_help().
*/
function admin_toolbar_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.admin_toolbar':
$variables = [
':toolbar' => Url::fromRoute('help.page', ['name' => 'toolbar'])->toString(),
':automated_cron' => (\Drupal::moduleHandler()->moduleExists('automated_cron')) ? Url::fromRoute('help.page', ['name' => 'automated_cron'])->toString() : '#',
];
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Admin Toolbar module enhances the <a href=":toolbar">Toolbar</a> module by providing fast access to all the administrative links at the top of your site. Admin Toolbar remains a very "lightweight" module by closely integrating with all Toolbar functionality. It can be used in conjunction with all the sub modules included on Admin Toolbar, for quick access to system commands such as Flush all caches, <a href=":automated_cron">Run cron</a>, Run Updates, etc.', $variables) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<p>' . t('The Admin Toolbar greatly improves the user experience for those who regularly interact with the site Toolbar by providing fast, full access to all links in the site Toolbar without having to click to get there.') . '</p>';
return $output;
}
}
/**
* Adds toolbar-specific attributes to the menu link tree.
*
* @param \Drupal\Core\Menu\MenuLinkTreeElement[] $tree
* The menu link tree to manipulate.
*
* @return \Drupal\Core\Menu\MenuLinkTreeElement[]
* The manipulated menu link tree.
*/
function toolbar_tools_menu_navigation_links(array $tree) {
foreach ($tree as $element) {
if ($element->subtree) {
toolbar_tools_menu_navigation_links($element->subtree);
}
$link = $element->link;
// Get the non-localized title to make the icon class.
$definition = $link->getPluginDefinition();
$element->options['attributes']['class'][] = 'toolbar-icon';
$string = strtolower(str_replace(['.', ' ', '_'], ['-', '-', '-'], $definition['id']));
$element->options['attributes']['class'][] = Html::cleanCssIdentifier('toolbar-icon-' . $string);
$element->options['attributes']['title'] = $link->getDescription();
}
return $tree;
}

View File

@@ -0,0 +1,7 @@
admin_toolbar.settings:
path: '/admin/config/user-interface/admin-toolbar'
defaults:
_form: '\Drupal\admin_toolbar\Form\AdminToolbarSettingsForm'
_title: 'Admin Toolbar settings'
requirements:
_permission: 'administer site configuration'

View File

@@ -0,0 +1,55 @@
# Admin Toolbar Tools
The Admin Toolbar Links Access Filter module Provides a workaround for the
common problem that users with 'Use the administration pages and help'
permission see menu links they don't have access permission for. Once the issue
[296693](https://www.drupal.org/node/296693) be solved, this module will be deprecated.
For a full description of the module, visit the
[project page](https://www.drupal.org/project/admin_toolbar).
Submit bug reports and feature suggestions, or track changes in the
[issue queue](https://www.drupal.org/project/issues/search/admin_toolbar).
## Table of contents
- Requirements
- Installation
- Configuration
- Maintainers
## Requirements
This module requires the following modules:
- [Admin Toolbar](https://www.drupal.org/project/admin_toolbar)
## Installation
Install as you would normally install a contributed Drupal module. For further
information, see
[Installing Drupal Modules](https://www.drupal.org/docs/extending-drupal/installing-drupal-modules).
## Configuration
No configuration is needed.
## Maintainers
Current maintainers:
- [Romain Jarraud (romainj)](https://www.drupal.org/u/romainj)
- [Adrian Cid Almaguer (adriancid)](https://www.drupal.org/u/adriancid)
- [Wilfrid Roze (eme)](https://www.drupal.org/u/eme)
- [bilel khalil (bolbol)](https://www.drupal.org/u/bolbol)
- [fethi.krout (fethi.krout)](https://www.drupal.org/u/fethi.krout)
- [Mohamed Anis Taktak (matio89)](https://www.drupal.org/u/matio89)
- [Thomas MUSA (Musa.thomas)](https://www.drupal.org/u/musathomas)
Supporting organizations:
- [emerya](https://www.drupal.org/emerya) Created this module for you!
- [Trained People](https://www.drupal.org/trained-people) Sponsored the module development
- [Drupiter](https://www.drupal.org/drupiter) Sponsored the module development
- [Dropteam](https://www.drupal.org/dropteam) Sponsored the module development
- [Alliance of Digital Builders (AODB)](https://www.drupal.org/alliance-of-digital-builders-aodb) Sponsored the module development

View File

@@ -0,0 +1,12 @@
name: Admin Toolbar Links Access Filter
description: Provides a workaround for the common problem that users with 'Use the administration pages and help' permission see menu links they don't have access permission for. Once the issue <a href='https://www.drupal.org/node/296693'>https://www.drupal.org/node/296693</a> be solved, this module will be deprecated.
package: Administration
type: module
core_version_requirement: ^9.2 || ^10
dependencies:
- admin_toolbar:admin_toolbar
# Information added by Drupal.org packaging script on 2023-09-29
version: '3.4.2'
project: 'admin_toolbar'
datestamp: 1696006156

View File

@@ -0,0 +1,193 @@
<?php
/**
* @file
* This module don't show menu links that you don't have access permission for.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\Role;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function admin_toolbar_links_access_filter_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help.
case 'help.page.admin_toolbar_links_access_filter':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Admin Toolbar Links Access Filter module provides a workaround for the common problem that users with <em>Use the administration pages and help</em> permission see menu links they done not have access permission for.') . '</p>';
return $output;
}
}
/**
* Implements hook_preprocess_menu().
*
* Hides links from admin menu, if user doesn't have access rights.
*/
function admin_toolbar_links_access_filter_preprocess_menu(&$variables) {
if (empty($variables['items'])) {
// Additional empty check to prevent exotic situations, where the preprocess
// function is entered even without items.
// @see https://www.drupal.org/node/2833885
return;
}
// Ensure that menu_name exists.
if (!isset($variables['menu_name'])) {
// In rare cases (for unknown reasons) menu_name may not be set.
// As fallback, we can fetch it from the first menu item.
$first_link = reset($variables['items']);
/** @var Drupal\Core\Menu\MenuLinkDefault $original_link */
// Fetch the menu_name from the original link.
$original_link = $first_link['original_link'];
$variables['menu_name'] = $original_link->getMenuName();
}
if ($variables['menu_name'] == 'admin') {
if (!admin_toolbar_links_access_filter_user_has_admin_role($variables['user'])) {
admin_toolbar_links_access_filter_filter_non_accessible_links($variables['items']);
}
}
}
/**
* Hides links from admin menu, if user doesn't have access rights.
*/
function admin_toolbar_links_access_filter_filter_non_accessible_links(array &$items) {
if (Drupal::currentUser()->id() == 1) {
// Admin can access everything.
return;
}
$access_manager = \Drupal::accessManager();
foreach ($items as $menu_id => &$item) {
try {
$route_name = NULL;
$route_params = [];
if (!empty($item['original_link'])) {
/** @var \Drupal\Core\Menu\MenuLinkBase $original_link */
$original_link = $item['original_link'];
if ($original_link->getUrlObject()->isExternal() || !$original_link->getUrlObject()->isRouted()) {
// Do not filter external URL at all.
continue;
}
$route_name = $original_link->getRouteName() ?: $original_link->getUrlObject()->getRouteName();
$route_params = $original_link->getRouteParameters() ?: $original_link->getUrlObject()->getRouteParameters();
}
elseif (!empty($item['url'])) {
/** @var \Drupal\Core\Url $url */
$url = $item['url'];
if ($url->isExternal() || !$original_link->getUrlObject()->isRouted()) {
// Do not filter external URL at all.
continue;
}
$route_name = $url->getRouteName();
$route_params = $url->getRouteParameters();
}
// Check, if user has access rights to the route.
if (!$access_manager->checkNamedRoute($route_name, $route_params)) {
unset($items[$menu_id]);
}
else {
if (!empty($items[$menu_id]['below'])) {
// Recursively call this function for the child items.
admin_toolbar_links_access_filter_filter_non_accessible_links($items[$menu_id]['below']);
}
if (empty($items[$menu_id]['below'])) {
// Every child item has been cleared out.
// Now check, if the given route represents an overview page only,
// without having functionality on its own. In this case, we can
// safely unset this item, as there aren't any children left.
// This assumption is only valid, when the admin_toolbar module is
// installed because otherwise we won't have child items at all.
if (admin_toolbar_links_access_filter_is_overview_page($route_name)) {
unset($items[$menu_id]);
}
// If there are no sub-items and the parent does not have a link, then
// it is safe to remove it.
elseif ($route_name === '<nolink>') {
unset($items[$menu_id]);
}
else {
// Let's remove the expanded flag.
$items[$menu_id]['is_expanded'] = FALSE;
}
}
}
}
catch (\UnexpectedValueException $e) {
// Skip on errors like "base:block has no corresponding route":
\Drupal::logger('my_module')->error($e->getMessage());
continue;
}
}
}
/**
* Checks if the given route name is an overview page.
*
* Checks if the given route name matches a pure (admin) overview page that can
* be skipped, if there are no child items set. The typical example are routes
* having the SystemController::systemAdminMenuBlockPage() function as their
* controller callback set.
*
* @param string $route_name
* The route name to check.
*
* @return bool
* TRUE, if the given route name matches a pure admin overview page route,
* FALSE otherwise.
*/
function admin_toolbar_links_access_filter_is_overview_page($route_name) {
/** @var \Drupal\Core\Routing\RouteProviderInterface $route_provider. */
$route_provider = \Drupal::service('router.route_provider');
$overview_page_controllers = [
'\Drupal\system\Controller\AdminController::index',
'\Drupal\system\Controller\SystemController::overview',
'\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage',
];
try {
$route = $route_provider->getRouteByName($route_name);
$controller = $route->getDefault('_controller');
return !empty($controller) && in_array($controller, $overview_page_controllers);
}
catch (RouteNotFoundException $ex) {
}
return FALSE;
}
/**
* Checks, if the given user has admin rights.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The account to check.
*
* @return bool
* TRUE, if the given user account has at least one role with admin rights
* assigned, FALSE otherwise.
*/
function admin_toolbar_links_access_filter_user_has_admin_role(AccountInterface $account) {
static $user_has_admin_role = [];
$uid = $account->id();
if (!isset($user_has_admin_role[$uid])) {
$user_has_admin_role[$uid] = FALSE;
$roles = Role::loadMultiple($account->getRoles());
// It is possible for a user account to have no roles assigned.
if (!empty($roles)) {
foreach ($roles as $role) {
if ($role->isAdmin()) {
$user_has_admin_role[$uid] = TRUE;
break;
}
}
}
}
return $user_has_admin_role[$uid];
}

View File

@@ -0,0 +1,13 @@
name: Admin Toolbar Search
description: Provides search of Admin Toolbar items.
package: Administration
type: module
core_version_requirement: ^9.2 || ^10
configure: admin_toolbar_search.settings
dependencies:
- admin_toolbar:admin_toolbar_tools
# Information added by Drupal.org packaging script on 2023-09-29
version: '3.4.2'
project: 'admin_toolbar'
datestamp: 1696006156

View File

@@ -0,0 +1,11 @@
search:
css:
theme:
css/admin.toolbar_search.css: {}
js:
js/admin_toolbar_search.js: {}
dependencies:
- core/jquery
- core/drupal
- core/once
- core/drupal.autocomplete

View File

@@ -0,0 +1,5 @@
admin_toolbar_search.settings:
title: 'Admin Toolbar Search'
description: 'Configure the Admin Toolbar Search module.'
route_name: admin_toolbar_search.settings
parent: system.admin_config_ui

View File

@@ -0,0 +1,142 @@
<?php
/**
* @file
* Functionality for search of Admin Toolbar.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function admin_toolbar_search_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help.
case 'help.page.admin_toolbar_search':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Admin Toolbar Search module add a search option to the toolbar for site administrative tasks.') . '</p>';
return $output;
}
}
/**
* Implements hook_toolbar_alter().
*/
function admin_toolbar_search_toolbar_alter(&$items) {
if (!\Drupal::currentUser()->hasPermission('use admin toolbar search')) {
return;
}
$admin_toolbar_tools_enabled = \Drupal::service('module_handler')
->moduleExists('admin_toolbar_tools');
$config = \Drupal::config('admin_toolbar_search.settings');
$display_menu_item = $config->get('display_menu_item');
if (!$display_menu_item) {
$items['administration_mobile_search'] = [
'#type' => 'toolbar_item',
'#weight' => 100,
'tab' => [
'#type' => 'link',
'#title' => new TranslatableMarkup('Search'),
'#url' => Url::fromRoute('system.admin'),
'#attributes' => [
'class' => [
'toolbar-icon',
],
],
],
'#wrapper_attributes' => [
'id' => 'admin-toolbar-mobile-search-tab',
],
];
$items['administration_search'] = [
"#type" => "toolbar_item",
'#weight' => 101,
'tab' => [
'search' => [
'#title' => t('Search'),
'#title_display' => 'invisible',
'#type' => 'search',
'#size' => 30,
'#attributes' => [
'placeholder' => new TranslatableMarkup('Admin Toolbar quick search'),
],
'#id' => 'admin-toolbar-search-input',
],
],
'#attached' => [
'library' => [
'admin_toolbar_search/search',
],
'drupalSettings' => [
'adminToolbarSearch' => [
'loadExtraLinks' => $admin_toolbar_tools_enabled,
],
],
],
'#wrapper_attributes' => [
'id' => 'admin-toolbar-search-tab',
],
'#cache' => [
'contexts' => [
'user.permissions',
],
'tags' => [
'config:admin_toolbar_search.settings',
],
],
];
}
else {
$items['administration_search'] = [
"#type" => "toolbar_item",
'tab' => [
'#type' => 'link',
'#title' => new TranslatableMarkup('Search'),
'#url' => URL::fromRoute('system.admin'),
'#attributes' => [
'class' => [
'toolbar-icon',
],
],
],
'tray' => [
'search' => [
'#title' => t('Search'),
'#type' => 'search',
'#size' => 60,
'#id' => 'admin-toolbar-search-input',
],
],
'#attached' => [
'library' => [
'admin_toolbar_search/search',
],
'drupalSettings' => [
'adminToolbarSearch' => [
'loadExtraLinks' => $admin_toolbar_tools_enabled,
],
],
],
'#wrapper_attributes' => [
"id" => "admin-toolbar-search-tab",
],
'#cache' => [
'contexts' => [
'user.permissions',
],
'tags' => [
'config:admin_toolbar_search.settings',
],
],
];
}
}

View File

@@ -0,0 +1,2 @@
use admin toolbar search:
title: 'Use Admin Toolbar search'

View File

@@ -0,0 +1,14 @@
admin_toolbar.search:
path: '/admin/admin-toolbar-search'
defaults:
_controller: '\Drupal\admin_toolbar_search\Controller\AdminToolbarSearchController::search'
requirements:
_permission: 'use admin toolbar search'
admin_toolbar_search.settings:
path: '/admin/config/user-interface/admin-toolbar-search-settings'
defaults:
_title: 'Admin toolbar search settings'
_form: 'Drupal\admin_toolbar_search\Form\AdminToolbarSearchSettingsForm'
requirements:
_permission: 'administer site configuration'

View File

@@ -0,0 +1,10 @@
services:
admin_toolbar_search.search_links:
class: Drupal\admin_toolbar_search\SearchLinks
arguments:
- '@entity_type.manager'
- '@module_handler'
- '@router.route_provider'
- '@cache_contexts_manager'
- '@cache.toolbar'
- '@config.factory'

View File

@@ -0,0 +1,110 @@
#admin-toolbar-mobile-search-tab ~ #admin-toolbar-search-tab {
display: none;
}
#admin-toolbar-mobile-search-tab ~ #admin-toolbar-search-tab.visible {
display: block;
width: 100%;
}
#admin-toolbar-mobile-search-tab ~ #admin-toolbar-search-tab .js-form-item.form-item {
margin: 0.75rem 0;
padding-left: 1rem;
padding-right: 1rem;
}
#admin-toolbar-mobile-search-tab .toolbar-item::before {
background-image: url('../../misc/icons/bebebe/loupe.svg');
}
#admin-toolbar-mobile-search-tab ~ #admin-toolbar-search-tab #admin-toolbar-search-input {
width: 100%;
}
@media only screen and (min-width: 769px) {
#admin-toolbar-mobile-search-tab {
display: none;
}
#admin-toolbar-mobile-search-tab ~ #admin-toolbar-search-tab {
display: block;
}
#admin-toolbar-mobile-search-tab ~ #admin-toolbar-search-tab.visible {
width: auto;
}
#admin-toolbar-mobile-search-tab ~ #admin-toolbar-search-tab .js-form-item.form-item {
margin-top: 0.3rem;
margin-bottom: 0;
}
}
#admin-toolbar-search-input {
min-height: 30px;
height: 100%;
padding: 0 0.4rem;
line-height: 1.75rem;
margin: 0;
color: #3b3b3b;
background: #fcfcfa;
border: 1px solid #ccc;
border-radius: unset;
font-size: 1em;
}
.ui-autocomplete .ui-menu-item span.admin-toolbar-search-url {
display: none;
}
.admin-toolbar-search-autocomplete-list {
max-height: 300px;
overflow-y: scroll;
}
.admin-toolbar-search-autocomplete-list .ui-menu-item .ui-state-active {
margin: 0;
}
.admin-toolbar-search-autocomplete-list .ui-menu-item .ui-menu-item-wrapper {
border: 1px solid transparent;
padding: 3px 1em 3px 0.4em;
}
.admin-toolbar-search-autocomplete-list .ui-menu-item a {
text-decoration: none;
}
.admin-toolbar-search-autocomplete-list .ui-menu-item .ui-state-active:hover a,
.admin-toolbar-search-autocomplete-list .ui-menu-item .ui-state-active a {
color: white;
}
#toolbar-item-administration-search-tray label {
display: inline-block;
color: #000000;
margin-right: .5em;
font-weight: bold;
}
#toolbar-item-administration-search-tray div.form-item {
margin: 0.75em;
}
#toolbar-item-administration-search-tray input {
display: inline-block;
padding: 0.3em 0.4em 0.3em 0.5em;
font-size: 1em;
}
#admin-toolbar-search-tab .toolbar-item:before {
background-image: url('../../misc/icons/bebebe/loupe.svg');
}
#admin-toolbar-search-tab .toolbar-item:active:before,
#admin-toolbar-search-tab .toolbar-item.is-active:before {
background-image: url('../../misc/icons/ffffff/loupe.svg');
}
#toolbar-item-administration-search-tray div.form-item.js-form-type-textfield {
margin: 0.75em 0;
}

View File

@@ -0,0 +1,151 @@
/**
* @file
* Behaviors for the search widget in the admin toolbar.
*/
(function ($, Drupal) {
'use strict';
Drupal.behaviors.adminToolbarSearch = {
// If extra links have been fetched.
extraFetched: false,
attach: function (context) {
if (context != document) {
return;
}
var $self = this;
const elements = once('admin-toolbar-search', '#toolbar-bar', context);
$(elements).each(function () {
$self.links = [];
var $searchTab = $(this).find('#admin-toolbar-search-tab')
var $searchInput = $searchTab.find('#admin-toolbar-search-input');
if ($searchInput.length === 0) {
return;
}
$searchInput.autocomplete({
minLength: 2,
position: { collision : 'fit' },
source: function (request, response) {
var data = $self.handleAutocomplete(request.term);
if (!$self.extraFetched && drupalSettings.adminToolbarSearch.loadExtraLinks) {
$.getJSON( Drupal.url('admin/admin-toolbar-search'), function ( data ) {
$(data).each(function () {
var item = this;
item.label = this.labelRaw + ' ' + this.value;
$self.links.push(item);
});
$self.extraFetched = true;
var results = $self.handleAutocomplete(request.term);
response(results);
});
}
else {
response(data);
}
},
open: function () {
var zIndex = $('#toolbar-item-administration-tray')
.css('z-index') + 1;
$(this).autocomplete('widget').css('z-index', zIndex);
return false;
},
select: function (event, ui) {
if (ui.item.value) {
location.href = ui.item.value;
return false;
}
}
}).data('ui-autocomplete')._renderItem = (function (ul, item) {
ul.addClass('admin-toolbar-search-autocomplete-list');
return $('<li>')
.append('<div ><a href="' + item.value + '" onclick="window.open(this.href); return false;" >' + item.labelRaw + ' <span class="admin-toolbar-search-url">' + item.value + '</span></a></div>')
.appendTo(ul);
});
// Populate the links for search results when the input is pressed.
$searchInput.focus(function () {
Drupal.behaviors.adminToolbarSearch.populateLinks($self);
});
// Show/hide search input field when mobile tab item is pressed.
$('#admin-toolbar-mobile-search-tab .toolbar-item', context).click(function (e) {
e.preventDefault();
$(this).toggleClass('is-active');
$searchTab.toggleClass('visible');
$searchInput.focus();
});
});
},
getItemLabel: function (item) {
var breadcrumbs = [];
$(item).parents().each(function () {
if ($(this).hasClass('menu-item')) {
var $link = $(this).find('a:first');
if ($link.length && !$link.hasClass('admin-toolbar-search-ignore')) {
breadcrumbs.unshift($link.text());
}
}
});
return breadcrumbs.join(' > ');
},
handleAutocomplete: function (term) {
var $self = this;
var keywords = term.split(" "); // Split search terms into list.
var suggestions = [];
$self.links.forEach(function (element) {
var label = element.label.toLowerCase();
// Add exact matches.
if (label.indexOf(term.toLowerCase()) >= 0) {
suggestions.push(element);
}
else {
// Add suggestions where it matches all search terms.
var matchCount = 0;
keywords.forEach(function (keyword) {
if (label.indexOf(keyword.toLowerCase()) >= 0) {
matchCount++;
}
});
if (matchCount == keywords.length) {
suggestions.push(element);
}
}
});
return suggestions;
},
/**
* Populates the links in admin toolbar search.
*/
populateLinks: function ($self) {
// Populate only when links array is empty (only the first time).
if ($self.links.length === 0) {
var getUrl = window.location;
var baseUrl = getUrl.protocol + "//" + getUrl.host + "/";
$('.toolbar-tray a[data-drupal-link-system-path]').each(function () {
if (this.href !== baseUrl) {
var label = $self.getItemLabel(this);
$self.links.push({
'value': this.href,
'label': label + ' ' + this.href,
'labelRaw': Drupal.checkPlain(label)
});
}
});
}
},
};
})(jQuery, Drupal);

View File

@@ -0,0 +1,50 @@
<?php
namespace Drupal\admin_toolbar_search\Controller;
use Drupal\admin_toolbar_search\SearchLinks;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class AdminToolbarSearchController to the search functionality.
*
* @package Drupal\admin_toolbar_tools\Controller
*/
class AdminToolbarSearchController extends ControllerBase {
/**
* The search links service.
*
* @var \Drupal\admin_toolbar_search\SearchLinks
*/
protected $links;
/**
* Constructs an AdminToolbarSearchController object.
*
* @param \Drupal\admin_toolbar_search\SearchLinks $links
* The search links service.
*/
public function __construct(SearchLinks $links) {
$this->links = $links;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('admin_toolbar_search.search_links')
);
}
/**
* Return additional search links.
*/
public function search() {
return new JsonResponse($this->links->getLinks());
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Drupal\admin_toolbar_search\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure Admin Toolbar Search settings for this site.
*/
class AdminToolbarSearchSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'admin_toolbar_search_admin_toolbar_search_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['admin_toolbar_search.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['display_menu_item'] = [
'#type' => 'checkbox',
'#title' => $this->t('Display the search input as a menu item.'),
'#description' => $this->t("If set, instead of displaying a text input field, it displays a menu item in the toolbar so the user has to click on it to toggle the search input."),
'#default_value' => $this->config('admin_toolbar_search.settings')->get('display_menu_item'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('admin_toolbar_search.settings')
->set('display_menu_item', $form_state->getValue('display_menu_item'))
->save();
parent::submitForm($form, $form_state);
}
}

View File

@@ -0,0 +1,317 @@
<?php
namespace Drupal\admin_toolbar_search;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\system\Entity\Menu;
/**
* Extra search links.
*/
class SearchLinks {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The cache context manager service.
*
* @var \Drupal\Core\Cache\Context\CacheContextsManager
*/
protected $cacheContextManager;
/**
* The toolbar cache bin.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $toolbarCache;
/**
* The admin toolbar tools configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* Constructs a SearchLinks object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
* @param \Drupal\Core\Cache\Context\CacheContextsManager $cache_context_manager
* The cache contexts manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $toolbar_cache
* Cache backend instance to use.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory mservice.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RouteProviderInterface $route_provider, CacheContextsManager $cache_context_manager, CacheBackendInterface $toolbar_cache, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->routeProvider = $route_provider;
$this->cacheContextManager = $cache_context_manager;
$this->toolbarCache = $toolbar_cache;
$this->config = $config_factory->get('admin_toolbar_tools.settings');
}
/**
* Gets extra links for admin toolbar search feature.
*
* @return array
* An array of link data for the JSON used for search.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getLinks() {
$max_bundle_number = $this->config->get('max_bundle_number');
$additional_keys = $this->cacheContextManager->convertTokensToKeys([
'languages:' . LanguageInterface::TYPE_INTERFACE,
'user.permissions',
])->getKeys();
$cid_parts = array_merge(['admin_toolbar_search:links'], $additional_keys);
$cid = implode(':', $cid_parts);
if ($cache = $this->toolbarCache->get($cid)) {
return $cache->data;
}
$links = [];
$cache_tags = [];
$content_entities = $this->getBundleableEntitiesList();
// Adds common links to entities.
foreach ($content_entities as $entities) {
$content_entity_bundle = $entities['content_entity_bundle'];
$content_entity = $entities['content_entity'];
// Load the remaining items that were not loaded by the toolbar.
$content_entity_bundle_storage = $this->entityTypeManager->getStorage($content_entity_bundle);
$bundles_ids = $content_entity_bundle_storage->getQuery()->sort('weight')->range($max_bundle_number)->execute();
if (!empty($bundles_ids)) {
$bundles = $this->entityTypeManager
->getStorage($content_entity_bundle)
->loadMultiple($bundles_ids);
foreach ($bundles as $machine_name => $bundle) {
$cache_tags = Cache::mergeTags($cache_tags, $bundle->getEntityType()->getListCacheTags());
$tparams = [
'@entity_type' => $bundle->getEntityType()->getLabel(),
'@bundle' => $bundle->label(),
];
$label_base = $this->t('@entity_type > @bundle', $tparams);
$params = [$content_entity_bundle => $machine_name];
if ($this->routeExists('entity.' . $content_entity_bundle . '.overview_form')) {
// Some bundles have an overview/list form that make a better root
// link.
$url = Url::fromRoute('entity.' . $content_entity_bundle . '.overview_form', $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $label_base,
'value' => $url_string,
];
}
}
if ($this->routeExists('entity.' . $content_entity_bundle . '.edit_form')) {
$url = Url::fromRoute('entity.' . $content_entity_bundle . '.edit_form', $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $label_base . ' > ' . $this->t('Edit'),
'value' => $url_string,
];
}
}
if ($this->moduleHandler->moduleExists('field_ui')) {
if ($this->routeExists('entity.' . $content_entity . '.field_ui_fields')) {
$url = Url::fromRoute('entity.' . $content_entity . '.field_ui_fields', $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $label_base . ' > ' . $this->t('Manage fields'),
'value' => $url_string,
];
}
}
if ($this->routeExists('entity.entity_form_display.' . $content_entity . '.default')) {
$url = Url::fromRoute('entity.entity_form_display.' . $content_entity . '.default', $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $label_base . ' > ' . $this->t('Manage form display'),
'value' => $url_string,
];
}
}
if ($this->routeExists('entity.entity_view_display.' . $content_entity . '.default')) {
$url = Url::fromRoute('entity.entity_view_display.' . $content_entity . '.default', $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $label_base . ' > ' . $this->t('Manage display'),
'value' => $url_string,
];
}
}
if ($this->moduleHandler->moduleExists('devel') && $this->routeExists('entity.' . $content_entity_bundle . '.devel_load')) {
$url = Url::fromRoute($route_name = 'entity.' . $content_entity_bundle . '.devel_load', $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $label_base . ' > ' . $this->t('Devel'),
'value' => $url_string,
];
}
}
if ($this->routeExists('entity.' . $content_entity_bundle . '.delete_form')) {
$url = Url::fromRoute('entity.' . $content_entity_bundle . '.delete_form', $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $label_base . ' > ' . $this->t('Delete'),
'value' => $url_string,
];
}
}
}
}
}
}
// Add menu links.
if ($this->moduleHandler->moduleExists('menu_ui')) {
$menus = $this->entityTypeManager->getStorage('menu')->loadMultiple();
uasort($menus, [Menu::class, 'sort']);
$menus = array_slice($menus, $max_bundle_number);
$cache_tags = Cache::mergeTags($cache_tags, ['config:menu_list']);
foreach ($menus as $menu_id => $menu) {
$route_name = 'entity.menu.edit_form';
$params = ['menu' => $menu_id];
$url = Url::fromRoute($route_name, $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $this->t('Menus > @menu_label', ['@menu_label' => $menu->label()]),
'value' => $url_string,
];
}
$route_name = 'entity.menu.add_link_form';
$params = ['menu' => $menu_id];
$url = Url::fromRoute($route_name, $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $this->t('Menus > @menu_label > Add link', ['@menu_label' => $menu->label()]),
'value' => $url_string,
];
}
$menus = ['admin', 'devel', 'footer', 'main', 'tools', 'account'];
if (!in_array($menu_id, $menus)) {
$route_name = 'entity.menu.delete_form';
$params = ['menu' => $menu_id];
$url = Url::fromRoute($route_name, $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $this->t('Menus > @menu_label > Delete', ['@menu_label' => $menu->label()]),
'value' => $url_string,
];
}
}
if ($this->moduleHandler->moduleExists('devel') && $this->routeExists('entity.menu.devel_load')) {
$route_name = 'entity.menu.devel_load';
$params = ['menu' => $menu_id];
$url = Url::fromRoute($route_name, $params);
if ($url->access()) {
$url_string = $url->toString();
$links[] = [
'labelRaw' => $this->t('Menus > @menu_label > Devel', ['@menu_label' => $menu->label()]),
'value' => $url_string,
];
}
}
}
}
$this->toolbarCache->set($cid, $links, Cache::PERMANENT, $cache_tags);
return $links;
}
/**
* Gets a list of content entities.
*
* @return array
* An array of metadata about content entities.
*/
protected function getBundleableEntitiesList() {
$entity_types = $this->entityTypeManager->getDefinitions();
$content_entities = [];
foreach ($entity_types as $key => $entity_type) {
if ($entity_type->getBundleEntityType() && ($entity_type->get('field_ui_base_route') != '')) {
$content_entities[$key] = [
'content_entity' => $key,
'content_entity_bundle' => $entity_type->getBundleEntityType(),
];
}
}
return $content_entities;
}
/**
* Determine if a route exists by name.
*
* @param string $route_name
* The name of the route to check.
*
* @return bool
* Whether a route with that route name exists.
*/
public function routeExists($route_name) {
return (count($this->routeProvider->getRoutesByNames([$route_name])) === 1);
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Drupal\Tests\admin_toolbar_search\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Test the functionality of admin toolbar search.
*
* @group admin_toolbar
* @group admin_toolbar_search
*/
class AdminToolbarSearchSettingTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'admin_toolbar_search',
'node',
'media',
'field_ui',
'menu_ui',
'block',
];
/**
* A user with the 'Use Admin Toolbar search' permission.
*
* @var \Drupal\user\UserInterface
*/
protected $userWithAccess;
/**
* A test user without the 'Use Admin Toolbar search' permission..
*
* @var \Drupal\user\UserInterface
*/
protected $noAccessUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$permissions = [
'access toolbar',
'administer menu',
'access administration pages',
'administer site configuration',
'administer content types',
];
$this->noAccessUser = $this->drupalCreateUser($permissions);
$permissions[] = 'use admin toolbar search';
$this->userWithAccess = $this->drupalCreateUser($permissions);
}
/**
* Tests search functionality without admin_toolbar_tools enabled.
*/
public function testToolbarSearch() {
$this->drupalLogin($this->userWithAccess);
$this->drupalGet(Url::fromRoute('system.admin'));
$this->assertSession()->responseNotContains('id="toolbar-item-administration-search');
$this->config('admin_toolbar_search.settings')->set('display_menu_item', 1);
$this->config('admin_toolbar_search.settings')->save();
$this->drupalGet(Url::fromRoute('system.admin'));
$this->assertSession()->responseContains('id="toolbar-item-administration-search');
$this->config('admin_toolbar_search.settings')->set('display_menu_item', 0);
$this->config('admin_toolbar_search.settings')->save();
$this->drupalGet(Url::fromRoute('system.admin'));
$this->assertSession()->responseNotContains('id="toolbar-item-administration-search');
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Drupal\Tests\admin_toolbar_search\FunctionalJavascript;
/**
* Test the functionality of admin toolbar search.
*
* @group admin_toolbar
* @group admin_toolbar_search
*/
class AdminToolbarSearchTest extends AdminToolbarSearchTestBase {
/**
* Tests search functionality without admin_toolbar_tools enabled.
*/
public function testToolbarSearch() {
$search_tab = '#admin-toolbar-search-tab';
$search_toolbar_item = '#toolbar-item-administration-search';
$search_tray = '#toolbar-item-administration-search-tray';
$this->drupalLogin($this->userWithAccess);
$assert_session = $this->assertSession();
$assert_session->responseContains('admin.toolbar_search.css');
$assert_session->responseContains('admin_toolbar_search.js');
$assert_session->waitForElementVisible('css', $search_tab);
$assert_session->waitForElementVisible('css', $search_toolbar_item);
$assert_session->waitForElementVisible('css', $search_tray);
$this->assertSuggestionContains('perfor', 'admin/config/development/performance');
$this->assertSuggestionContains('develop', 'admin/config/development/maintenance');
$this->assertSuggestionContains('types', 'admin/structure/types');
}
/**
* Tests a user without the search permission can't use search.
*/
public function testNoAccess() {
$search_tab = '#admin-toolbar-search-tab';
$search_toolbar_item = '#toolbar-item-administration-search';
$search_tray = '#toolbar-item-administration-search-tray';
$this->drupalLogin($this->noAccessUser);
$assert_session = $this->assertSession();
$assert_session->responseNotContains('admin.toolbar_search.css');
$assert_session->responseNotContains('admin_toolbar_search.js');
$assert_session->elementNotExists('css', $search_tab);
$assert_session->elementNotExists('css', $search_toolbar_item);
$assert_session->elementNotExists('css', $search_tray);
}
}

View File

@@ -0,0 +1,192 @@
<?php
namespace Drupal\Tests\admin_toolbar_search\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\system\Entity\Menu;
/**
* Base class for testing the functionality of admin toolbar search.
*
* @group admin_toolbar
* @group admin_toolbar_search
*/
abstract class AdminToolbarSearchTestBase extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'admin_toolbar_search',
'node',
'media',
'field_ui',
'menu_ui',
'block',
];
/**
* A user with the 'Use Admin Toolbar search' permission.
*
* @var \Drupal\user\UserInterface
*/
protected $userWithAccess;
/**
* A test user without the 'Use Admin Toolbar search' permission..
*
* @var \Drupal\user\UserInterface
*/
protected $noAccessUser;
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$baby_names = [
'ada' => 'Ada',
'amara' => 'Amara',
'amelia' => 'Amelia',
'arabella' => 'Arabella',
'asher' => 'Asher',
'astrid' => 'Astrid',
'atticus' => 'Atticus',
'aurora' => 'Aurora',
'ava' => 'Ava',
'cora' => 'Cora',
'eleanor' => 'Eleanor',
'eloise' => 'Eloise',
'felix' => 'Felix',
'freya' => 'Freya',
'genevieve' => 'Genevieve',
'isla' => 'Isla',
'jasper' => 'Jasper',
'luna' => 'Luna',
'maeve' => 'Maeve',
'milo' => 'Milo',
'nora' => 'Nora',
'olivia' => 'Olivia',
'ophelia' => 'Ophelia',
'posie' => 'Posie',
'rose' => 'Rose',
'silas' => 'Silas',
'soren' => 'Soren',
];
foreach ($baby_names as $id => $label) {
$menu = Menu::create([
'id' => $id,
'label' => $label,
]);
$menu->save();
}
$this->drupalPlaceBlock('local_tasks_block');
$permissions = [
'access toolbar',
'administer menu',
'access administration pages',
'administer site configuration',
'administer content types',
];
$this->noAccessUser = $this->drupalCreateUser($permissions);
$permissions[] = 'use admin toolbar search';
$this->userWithAccess = $this->drupalCreateUser($permissions);
}
/**
* Assert that the search suggestions contain a given string with given input.
*
* @param string $search
* The string to search for.
* @param string $contains
* Some HTML that is expected to be within the suggestions element.
*/
protected function assertSuggestionContains($search, $contains) {
$this->resetSearch();
$page = $this->getSession()->getPage();
$page->fillField('admin-toolbar-search-input', $search);
$this->getSession()->getDriver()->keyDown('//input[@id="admin-toolbar-search-input"]', ' ');
$page->waitFor(3, function () use ($page) {
return ($page->find('css', 'ul.ui-autocomplete')->isVisible() === TRUE);
});
$suggestions_markup = $page->find('css', 'ul.ui-autocomplete')->getHtml();
$this->assertStringContainsString($contains, $suggestions_markup);
}
/**
* Assert that the search suggestions does not contain a given string.
*
* Assert that the search suggestions does not contain a given string with a
* given input.
*
* @param string $search
* The string to search for.
* @param string $contains
* Some HTML that is not expected to be within the suggestions element.
*/
protected function assertSuggestionNotContains($search, $contains) {
$this->resetSearch();
$page = $this->getSession()->getPage();
$page->fillField('admin-toolbar-search-input', $search);
$this->getSession()->getDriver()->keyDown('//input[@id="admin-toolbar-search-input"]', ' ');
$page->waitFor(3, function () use ($page) {
return ($page->find('css', 'ul.ui-autocomplete')->isVisible() === TRUE);
});
if ($page->find('css', 'ul.ui-autocomplete')->isVisible() === FALSE) {
return;
}
else {
$suggestions_markup = $page->find('css', 'ul.ui-autocomplete')->getHtml();
$this->assertStringNotContainsString($contains, $suggestions_markup);
}
}
/**
* Search for an empty string to clear out the autocomplete suggestions.
*/
protected function resetSearch() {
$page = $this->getSession()->getPage();
// Empty out the suggestions.
$page->fillField('admin-toolbar-search-input', '');
$this->getSession()->getDriver()->keyDown('//input[@id="admin-toolbar-search-input"]', ' ');
$page->waitFor(3, function () use ($page) {
return ($page->find('css', 'ul.ui-autocomplete')->isVisible() === FALSE);
});
}
/**
* Checks that there is a link with the specified url in the admin toolbar.
*
* @param string $url
* The url to assert exists in the admin menu.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
*/
protected function assertMenuHasHref($url) {
$this->assertSession()
->elementExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]');
}
/**
* Checks that there is no link with the specified url in the admin toolbar.
*
* @param string $url
* The url to assert exists in the admin menu.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
protected function assertMenuDoesNotHaveHref($url) {
$this->assertSession()
->elementNotExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]');
}
}

View File

@@ -0,0 +1,216 @@
<?php
namespace Drupal\Tests\admin_toolbar_search\FunctionalJavascript;
use Drupal\media\Entity\MediaType;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
/**
* Test the functionality of admin toolbar search.
*
* @group admin_toolbar
* @group admin_toolbar_search
*/
class AdminToolbarToolsSearchTest extends AdminToolbarSearchTestBase {
use MediaTypeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'admin_toolbar_tools',
'admin_toolbar_search',
'node',
'media',
'field_ui',
'menu_ui',
'block',
];
/**
* The admin user for tests.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$dog_names = [
'archie' => 'Archie',
'bailey' => 'Bailey',
'bella' => 'Bella',
'buddy' => 'Buddy',
'charlie' => 'Charlie',
'coco' => 'Coco',
'daisy' => 'Daisy',
'frankie' => 'Frankie',
'jack' => 'Jack',
'lola' => 'Lola',
'lucy' => 'Lucy',
'max' => 'Max',
'milo' => 'Milo',
'molly' => 'Molly',
'ollie' => 'Ollie',
'oscar' => 'Oscar',
'rosie' => 'Rosie',
'ruby' => 'Ruby',
'teddy' => 'Teddy',
'toby' => 'Toby',
'tonga' => 'Tonga',
'tracey' => 'Tracey',
'tuna' => 'Tuna',
'uno' => 'Uno',
'venus' => 'Venus',
'vicky' => 'Vicky',
'wimpy' => 'Wimpy',
'yellow' => 'Yellow',
'zac' => 'zac',
'zora' => 'zora',
];
foreach ($dog_names as $machine_name => $label) {
$this->createMediaType('image', [
'id' => $machine_name,
'label' => $label,
]);
}
$this->adminUser = $this->drupalCreateUser([
'access toolbar',
'administer menu',
'access administration pages',
'administer site configuration',
'administer content types',
'administer node fields',
'access media overview',
'administer media',
'administer media fields',
'administer media form display',
'administer media display',
'administer media types',
'use admin toolbar search',
]);
}
/**
* Tests search functionality with admin_toolbar_tools enabled.
*/
public function testToolbarSearch() {
$search_tab = '#admin-toolbar-search-tab';
$search_toolbar_item = '#toolbar-item-administration-search';
$search_tray = '#toolbar-item-administration-search-tray';
$this->drupalLogin($this->adminUser);
$assert_session = $this->assertSession();
$assert_session->responseContains('admin.toolbar_search.css');
$assert_session->responseContains('admin_toolbar_search.js');
$assert_session->waitForElementVisible('css', $search_tab);
$assert_session->waitForElementVisible('css', $search_toolbar_item);
$assert_session->waitForElementVisible('css', $search_tray);
$this->assertSuggestionContains('basic', 'admin/config/system/site-information');
// Rebuild menu items.
drupal_flush_all_caches();
// Test that the route admin_toolbar.search returns expected json.
$this->drupalGet('/admin/admin-toolbar-search');
$search_menus = [
'maeve',
'milo',
'nora',
'olivia',
'ophelia',
'posie',
'rose',
'silas',
'soren',
];
$toolbar_menus = [
'ada',
'amara',
'amelia',
'arabella',
'asher',
'astrid',
'atticus',
'aurora',
'ava',
];
foreach ($search_menus as $menu_id) {
$assert_session->responseContains('\/admin\/structure\/menu\/manage\/' . $menu_id);
}
foreach ($toolbar_menus as $menu_id) {
$assert_session->responseNotContains('\/admin\/structure\/menu\/manage\/' . $menu_id);
}
$this->drupalGet('/admin');
foreach ($search_menus as $menu_id) {
$this->assertMenuDoesNotHaveHref('/admin/structure/menu/manage/' . $menu_id);
}
foreach ($toolbar_menus as $menu_id) {
$this->assertMenuHasHref('/admin/structure/menu/manage/' . $menu_id);
}
$this->drupalGet('admin/structure/types/manage/article/fields');
$assert_session->waitForElementVisible('css', $search_tray);
$this->assertSuggestionContains('article manage fields', '/admin/structure/types/manage/article/fields');
$suggestions = $assert_session
->waitForElementVisible('css', 'ul.ui-autocomplete');
// Assert there is only one suggestion with a link to
// /admin/structure/types/manage/article/fields.
$count = count($suggestions->findAll('xpath', '//span[contains(text(), "/admin/structure/types/manage/article/fields")]'));
$this->assertEquals(1, $count);
// Test that bundle within admin toolbar appears in search.
$this->assertSuggestionContains('lola', 'admin/structure/media/manage/lola/fields');
// Assert that a link after the limit doesn't appear in admin toolbar.
$zora_url = '/admin/structure/media/manage/zora/fields';
$assert_session->elementNotContains('css', '#toolbar-administration', $zora_url);
// Assert that a link excluded from admin toolbar appears in search.
$this->assertSuggestionContains('zora', $zora_url);
// Test that adding a new bundle updates the extra links loaded from
// admin_toolbar.search route.
$this->createMediaType('image', [
'id' => 'zuzu',
'label' => 'Zuzu',
]);
$this->drupalGet('admin');
$assert_session->waitForElementVisible('css', $search_tray);
$this->assertSuggestionContains('zuzu', '/admin/structure/media/manage/zuzu/fields');
// Test that deleting a bundle updates the extra links loaded from
// admin_toolbar.search route.
$zora = MediaType::load('zora');
$zora->delete();
$this->getSession()->reload();
$assert_session->waitForElementVisible('css', $search_tray);
$this->assertSuggestionNotContains('zora', $zora);
}
}

View File

@@ -0,0 +1,56 @@
# Admin Toolbar Tools
Admin Toolbar Extra Tools provides menu links to administration pages or actions
(eg. Flushing caches) that are not generated by Drupal core. It adds menu items
that are not generated by Drupal core. For example there are no menu items for
each content type by default or menu links to Manage fields on each entity
types. That's the purpose of Admin Toolbar Extra Tools to add them.
For a full description of the module, visit the
[project page](https://www.drupal.org/project/admin_toolbar).
Submit bug reports and feature suggestions, or track changes in the
[issue queue](https://www.drupal.org/project/issues/search/admin_toolbar).
## Table of contents
- Requirements
- Installation
- Configuration
- Maintainers
## Requirements
This module requires the following modules:
- [Admin Toolbar](https://www.drupal.org/project/admin_toolbar)
## Installation
Install as you would normally install a contributed Drupal module. For further
information, see
[Installing Drupal Modules](https://www.drupal.org/docs/extending-drupal/installing-drupal-modules).
## Configuration
No configuration is needed.
## Maintainers
Current maintainers:
- [Romain Jarraud (romainj)](https://www.drupal.org/u/romainj)
- [Adrian Cid Almaguer (adriancid)](https://www.drupal.org/u/adriancid)
- [Wilfrid Roze (eme)](https://www.drupal.org/u/eme)
- [bilel khalil (bolbol)](https://www.drupal.org/u/bolbol)
- [fethi.krout (fethi.krout)](https://www.drupal.org/u/fethi.krout)
- [Mohamed Anis Taktak (matio89)](https://www.drupal.org/u/matio89)
- [Thomas MUSA (Musa.thomas)](https://www.drupal.org/u/musathomas)
Supporting organizations:
- [emerya](https://www.drupal.org/emerya) Created this module for you!
- [Trained People](https://www.drupal.org/trained-people) Sponsored the module development
- [Drupiter](https://www.drupal.org/drupiter) Sponsored the module development
- [Dropteam](https://www.drupal.org/dropteam) Sponsored the module development
- [Alliance of Digital Builders (AODB)](https://www.drupal.org/alliance-of-digital-builders-aodb) Sponsored the module development

View File

@@ -0,0 +1,13 @@
name: Admin Toolbar Extra Tools
description: Adds menu links like Flush cache, Run cron, Run updates, and Logout under Drupal icon.
package: Administration
configure: admin_toolbar_tools.settings
type: module
core_version_requirement: ^9.2 || ^10
dependencies:
- admin_toolbar:admin_toolbar
# Information added by Drupal.org packaging script on 2023-09-29
version: '3.4.2'
project: 'admin_toolbar'
datestamp: 1696006156

View File

@@ -0,0 +1,34 @@
<?php
/**
* @file
* Install, update and uninstall functions for the Admin Toolbar Tools module.
*/
/**
* Install the Admin Toolbar Search module.
*/
function admin_toolbar_tools_update_8001() {
// Installing the Admin Toolbar Search module.
\Drupal::service('module_installer')->install(['admin_toolbar_search']);
}
/**
* Default setting for maximum number of bundles per entity type to display.
*/
function admin_toolbar_tools_update_8201() {
\Drupal::service('config.factory')
->getEditable('admin_toolbar_tools.settings')
->set('max_bundle_number', 20)
->save(TRUE);
}
/**
* Default setting for enable hoverintent.
*/
function admin_toolbar_tools_update_8202() {
\Drupal::service('config.factory')
->getEditable('admin_toolbar_tools.settings')
->set('hoverintent_functionality', TRUE)
->save(TRUE);
}

View File

@@ -0,0 +1,4 @@
toolbar.icon:
css:
theme:
css/tools.css: {}

View File

@@ -0,0 +1,92 @@
admin_toolbar_tools.help:
title: 'Tools'
route_name: <front>
menu_name: admin
parent: system.admin
weight: -100
system.admin_index:
title: 'Index'
route_name: system.admin_index
menu_name: admin
parent: admin_toolbar_tools.help
weight: -100
system.run_cron:
title: 'Run cron'
route_name: admin_toolbar.run.cron
menu_name: admin
parent: admin_toolbar_tools.help
weight: -8
system.db_update:
title: 'Run updates'
route_name: system.db_update
menu_name: admin
parent: admin_toolbar_tools.help
weight: -6
system.modules_uninstall:
title: 'Uninstall module'
route_name: system.modules_uninstall
menu_name: admin
parent: system.modules_list
admin_toolbar_tools.flush:
title: 'Flush all caches'
route_name: admin_toolbar_tools.flush
weight: -9
parent: admin_toolbar_tools.help
menu_name: admin
admin_toolbar_tools.cssjs:
title: 'Flush CSS and JavaScript'
route_name: admin_toolbar_tools.cssjs
parent: admin_toolbar_tools.flush
menu_name: admin
admin_toolbar_tools.plugin:
title: 'Flush plugins cache'
route_name: admin_toolbar_tools.plugin
parent: admin_toolbar_tools.flush
menu_name: admin
admin_toolbar_tools.flush_static:
title: 'Flush static cache'
route_name: admin_toolbar_tools.flush_static
parent: admin_toolbar_tools.flush
menu_name: admin
admin_toolbar_tools.flush_menu:
title: 'Flush routing and links cache'
route_name: admin_toolbar_tools.flush_menu
parent: admin_toolbar_tools.flush
menu_name: admin
admin_toolbar_tools.flush_twig:
title: 'Flush twig cache'
route_name: admin_toolbar_tools.flush_twig
parent: admin_toolbar_tools.flush
menu_name: admin
admin_toolbar_tools.flush_rendercache:
title: 'Flush render cache'
route_name: admin_toolbar_tools.flush_rendercache
parent: admin_toolbar_tools.flush
menu_name: admin
admin_toolbar_tools.theme_rebuild:
title: 'Rebuild theme registry'
route_name: admin_toolbar_tools.theme_rebuild
parent: admin_toolbar_tools.flush
menu_name: admin
admin_toolbar_tools.extra_links:
deriver: \Drupal\admin_toolbar_tools\Plugin\Derivative\ExtraLinks
menu_name: admin
admin_toolbar_tools.settings:
title: 'Admin Toolbar Tools'
description: 'Configure the Admin Toolbar Tools module.'
route_name: admin_toolbar_tools.settings
parent: system.admin_config_ui

View File

@@ -0,0 +1,99 @@
<?php
/**
* @file
* Provides extra menu links for the core drupal toolbar.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* Implements hook_toolbar().
*/
function admin_toolbar_tools_toolbar() {
$items = [];
$items['admin_toolbar_tools'] = [
'#type' => 'toolbar_item',
'tab' => [
'#type' => 'link',
'#attributes' => [
'class' => ['toolbar-icon', 'toolbar-icon-admin-toolbar-tools-help'],
],
],
'#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']],
];
// Toolbar item for primary local tasks.
$items['admin_toolbar_local_tasks'] = \Drupal::service('admin_toolbar_tools.helper')->buildLocalTasksToolbar();
return $items;
}
/**
* Implements hook_preprocess_html().
*/
function admin_toolbar_tools_preprocess_html(&$variables) {
if (\Drupal::currentUser()->hasPermission('access toolbar')) {
$variables['attributes']['class'][] = 'toolbar-icon-' . intval(\Drupal::VERSION);
}
}
/**
* Implements hook_help().
*/
function admin_toolbar_tools_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.admin_toolbar_tools':
$output = '';
$output .= '<p>';
$output .= t('The Admin Toolbar Extra Tools module comes packaged with the <a href=":admin-toolbar">Admin Toolbar</a> module and adds functionality to it. The additional functionality is accessed through extra links on the main administration Toolbar. Some links to Admin Toolbar Extra Tools administration pages are located at the bottom of this page.</a>', [':admin-toolbar' => Url::fromRoute('help.page', ['name' => 'admin_toolbar'])->toString()]);
$output .= '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<p>' . t('To use Admin Toolbar Extra Tools just install it like any other module. There is no other configuration required.') . '</p>';
return $output;
}
}
/**
* Implements hook_entity_insert().
*/
function admin_toolbar_tools_entity_insert(EntityInterface $entity) {
// Skip rebuild during config sync because rebuild should
// always be a post-sync step.
if (!\Drupal::isConfigSyncing()) {
$entities = \Drupal::service('admin_toolbar_tools.helper')->getRebuildEntityTypes();
if (in_array($entity->getEntityTypeId(), $entities)) {
\Drupal::service('plugin.manager.menu.link')->rebuild();
}
}
}
/**
* Implements hook_entity_update().
*/
function admin_toolbar_tools_entity_update(EntityInterface $entity) {
// Skip rebuild during config sync because rebuild should
// always be a post-sync step.
if (!\Drupal::isConfigSyncing()) {
$entities = \Drupal::service('admin_toolbar_tools.helper')->getRebuildEntityTypes();
if (in_array($entity->getEntityTypeId(), $entities)) {
\Drupal::service('plugin.manager.menu.link')->rebuild();
}
}
}
/**
* Implements hook_entity_delete().
*/
function admin_toolbar_tools_entity_delete(EntityInterface $entity) {
// Skip rebuild during config sync because rebuild should
// always be a post-sync step.
if (!\Drupal::isConfigSyncing()) {
$entities = \Drupal::service('admin_toolbar_tools.helper')->getRebuildEntityTypes();
if (in_array($entity->getEntityTypeId(), $entities)) {
\Drupal::service('plugin.manager.menu.link')->rebuild();
}
}
}

View File

@@ -0,0 +1,13 @@
<?php
/**
* @file
* Post-update functions for the Admin Toolbar Tools module.
*/
/**
* Update container for admin_toolbar_tools.helper arguments.
*/
function admin_toolbar_tools_post_update_helper_added_config_factory() {
// Intentionally empty to trigger a service container rebuild.
}

View File

@@ -0,0 +1,97 @@
admin_toolbar_tools.flush:
path: '/admin/flush'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::flushAll'
_title: 'Flush all caches'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.cssjs:
path: '/admin/flush/cssjs'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::flushJsCss'
_title: 'Flush CSS and JavaScript'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.plugin:
path: '/admin/flush/plugin'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::flushPlugins'
_title: 'Plugin'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.flush_static:
path: '/admin/flush/static-caches'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::flushStatic'
_title: 'Static caches'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.flush_menu:
path: '/admin/flush/menu'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::flushMenu'
_title: 'Menu'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.flush_rendercache:
path: '/admin/flush/rendercache'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::cacheRender'
_title: 'Render cache'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.flush_views:
path: '/admin/flush/views'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::flushViews'
_title: 'Views'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.flush_twig:
path: '/admin/flush/twig'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::flushTwig'
_title: 'Twig'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.theme_rebuild:
path: '/admin/flush/theme_rebuild'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::themeRebuild'
_title: 'Theme Rebuild'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar.run.cron:
path: '/run-cron'
defaults:
_controller: '\Drupal\admin_toolbar_tools\Controller\ToolbarController::runCron'
_title: 'Run cron'
requirements:
_permission: 'administer site configuration'
_csrf_token: 'TRUE'
admin_toolbar_tools.settings:
path: '/admin/config/user-interface/admin-toolbar-tools'
defaults:
_form: '\Drupal\admin_toolbar_tools\Form\AdminToolbarToolsSettingsForm'
_title: 'Admin Toolbar Tools settings'
requirements:
_permission: 'administer site configuration'

View File

@@ -0,0 +1,8 @@
services:
admin_toolbar_tools.helper:
class: Drupal\admin_toolbar_tools\AdminToolbarToolsHelper
arguments:
- '@entity_type.manager'
- '@plugin.manager.menu.local_task'
- '@current_route_match'
- '@config.factory'

View File

@@ -0,0 +1,47 @@
.toolbar-icon-admin-toolbar-tools-help {
text-indent: -9999px;
}
.toolbar-icon-9 .toolbar-icon-admin-toolbar-tools-help:before,
.toolbar-icon-10 .toolbar-icon-admin-toolbar-tools-help:before {
box-sizing: content-box;
background-image: url(../misc/icons/ffffff/drupal-9-logo.svg);
padding-bottom: 0;
padding-left: 2px;
padding-right: 2px;
padding-top: 2px;
margin-left: 4px;
}
.toolbar-icon-9 .toolbar-icon-admin-toolbar-tools-help:active:before,
.toolbar-icon-9 .toolbar-icon-admin-toolbar-tools-help.active:before,
.toolbar-icon-10 .toolbar-icon-admin-toolbar-tools-help:active:before,
.toolbar-icon-10 .toolbar-icon-admin-toolbar-tools-help.active:before {
background-image: url(../misc/icons/ffffff/drupal-9-logo.svg);
}
.toolbar-icon-8 .toolbar-icon-admin-toolbar-tools-help:before {
box-sizing: content-box;
background-image: url(../misc/icons/ffffff/drupal-8-logo.svg);
padding-bottom: 0;
padding-left: 4px;
padding-right: 8px;
padding-top: 2px;
}
.toolbar-icon-8 .toolbar-icon-admin-toolbar-tools-help:active:before,
.toolbar-icon-8 .toolbar-icon-admin-toolbar-tools-help.active:before {
background-image: url(../misc/icons/ffffff/drupal-8-logo.svg);
}
.toolbar-oriented .toolbar-bar .local-tasks-toolbar-tab {
float: right;
}
.toolbar-horizontal .local-tasks-toolbar-tab .toolbar-menu {
float: right;
}
.toolbar-bar .toolbar-icon-local-tasks:before {
background-image: url(../misc/icons/bebebe/tasks.svg);
}

View File

@@ -0,0 +1,4 @@
<svg fill="#bebebe" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M4 10.5c-.83 0-1.5.67-1.5 1.5s.67 1.5 1.5 1.5 1.5-.67 1.5-1.5-.67-1.5-1.5-1.5zm0-6c-.83 0-1.5.67-1.5 1.5S3.17 7.5 4 7.5 5.5 6.83 5.5 6 4.83 4.5 4 4.5zm0 12c-.83 0-1.5.68-1.5 1.5s.68 1.5 1.5 1.5 1.5-.68 1.5-1.5-.67-1.5-1.5-1.5zM7 19h14v-2H7v2zm0-6h14v-2H7v2zm0-8v2h14V5H7z"/>
<path d="M0 0h24v24H0V0z" fill="none"/>
</svg>

After

Width:  |  Height:  |  Size: 434 B

View File

@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" preserveAspectRatio="xMinYMin" viewBox="0 0 611 646"><path fill="#2ba9e0" d="M 161.14248,607.07981 C 135.931,577.51327 120.7662,539.33875 120.7662,497.60876 c 0,-87.01545 66.15644,-158.68624 151.648,-168.60413 -14.59612,-20.95856 -23.31588,-46.40824 -23.31588,-73.72922 0,-71.85792 58.95316,-129.86822 131.7442,-129.86822 6.06592,0 11.94228,0.37426 17.62908,1.12278 C 355.44148,89.85249 312.41136,52.61362 278.48012,12.00641 295.73008,190.15417 114.1316,125.40719 46.8378,289.70733 1.91208,399.73977 42.47792,535.78328 161.14248,607.07981 Z m 143.1178,-351.8044 c 0,41.72999 34.31036,75.41339 76.39268,75.41339 42.08232,0 76.58224,-33.87053 76.58224,-75.41339 0,-41.72999 -34.31036,-75.41339 -76.39268,-75.41339 -42.08232,0 -76.58224,33.6834 -76.58224,75.41339 z m 112.97776,124.81571 c 29.57136,30.50219 47.76912,71.85792 47.76912,117.51764 0,57.82317 -29.19224,108.72253 -73.73884,139.41185 82.4586,-25.07542 150.7002,-86.26693 181.21936,-160.37041 42.27188,-102.54724 2.8434,-179.6448 -63.12348,-249.63142 2.08516,8.98224 3.22252,18.52587 3.22252,28.0695 -0.18956,59.50734 -40.37628,109.47105 -95.34868,125.00284 z m -124.35136,18.90013 c -55.16196,0 -99.89812,44.16268 -99.89812,98.61751 0,54.45483 44.73616,98.61751 99.89812,98.61751 55.16196,0 99.89812,-44.16268 99.89812,-98.61751 0,-54.45483 -44.73616,-98.61751 -99.89812,-98.61751 z" /></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 42.15 55.08"><defs><style>.cls-1{fill:#009cde;}</style></defs><title>Risorsa 23</title><g id="Livello_2" data-name="Livello 2"><g id="Livello_1-2" data-name="Livello 1"><path class="cls-1" d="M29.75,11.73C25.87,7.86,22.18,4.16,21.08,0,20,4.16,16.28,7.86,12.4,11.73,6.59,17.54,0,24.12,0,34a21.08,21.08,0,1,0,42.15,0C42.15,24.12,35.56,17.54,29.75,11.73ZM10.84,35.92a14.13,14.13,0,0,0-1.65,2.62.54.54,0,0,1-.36.3H8.65c-.47,0-1-.92-1-.92h0c-.14-.22-.27-.45-.4-.69l-.09-.19C5.94,34.25,7,30.28,7,30.28h0a17.42,17.42,0,0,1,2.52-5.41,31.53,31.53,0,0,1,2.28-3l1,1,4.72,4.82a.54.54,0,0,1,0,.72l-4.93,5.47h0ZM21.32,49.73a7.29,7.29,0,0,1-5.4-12.14c1.54-1.83,3.42-3.63,5.46-6,2.42,2.58,4,4.35,5.55,6.29a3.08,3.08,0,0,1,.32.48,7.15,7.15,0,0,1,1.3,4.12A7.23,7.23,0,0,1,21.32,49.73ZM35,38.14v0a.84.84,0,0,1-.67.58h-.14a1.22,1.22,0,0,1-.68-.55h0a37.77,37.77,0,0,0-4.28-5.31l-1.93-2-6.41-6.65a54,54,0,0,1-3.84-3.94,1.3,1.3,0,0,0-.1-.15,3.84,3.84,0,0,1-.51-1c0-.06,0-.13,0-.19a3.4,3.4,0,0,1,1-3c1.24-1.24,2.49-2.49,3.67-3.79,1.3,1.44,2.69,2.82,4.06,4.19v0a57.6,57.6,0,0,1,7.55,8.58A16,16,0,0,1,35.65,34,14.55,14.55,0,0,1,35,38.14Z"/></g></g></svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@@ -0,0 +1,174 @@
<?php
namespace Drupal\admin_toolbar_tools;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\LocalTaskManagerInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
/**
* Admin Toolbar Tools helper service.
*/
class AdminToolbarToolsHelper {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The local task manger.
*
* @var \Drupal\Core\Menu\LocalTaskManagerInterface
* The local task manager menu.
*/
protected $localTaskManager;
/**
* The route match interface.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
* The route match.
*/
protected $routeMatch;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Create an AdminToolbarToolsHelper object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Menu\LocalTaskManagerInterface $local_task_manager
* The local task manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LocalTaskManagerInterface $local_task_manager, RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->localTaskManager = $local_task_manager;
$this->routeMatch = $route_match;
$this->configFactory = $config_factory;
}
/**
* Generate the toolbar tab and item for primary local tasks.
*
* @return array
* The toolbar render array.
*/
public function buildLocalTasksToolbar() {
$build = [];
$config = $this->configFactory->get('admin_toolbar_tools.settings');
$cacheability = CacheableMetadata::createFromObject($config);
if ($config->get('show_local_tasks')) {
$local_tasks = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName());
$cacheability = $cacheability->merge($local_tasks['cacheability']);
$cacheability = $cacheability->merge(CacheableMetadata::createFromObject($this->localTaskManager));
if (!empty($local_tasks['tabs'])) {
$local_task_links = [
'#theme' => 'links',
'#links' => [],
'#attributes' => [
'class' => ['toolbar-menu'],
],
];
// Sort the links by weight.
Element::children($local_tasks['tabs'], TRUE);
// Only show the accessible local tasks.
foreach (Element::getVisibleChildren($local_tasks['tabs']) as $task) {
$local_task_links['#links'][$task] = $local_tasks['tabs'][$task]['#link'];
if ($local_tasks['tabs'][$task]['#active']) {
$local_task_links['#links'][$task]['attributes']['class'][] = 'is-active';
}
}
$build = [
'#type' => 'toolbar_item',
'#wrapper_attributes' => [
'class' => ['local-tasks-toolbar-tab'],
],
// Put it after contextual toolbar item so when float right is applied
// local tasks item will be first.
'#weight' => 10,
'tab' => [
// We can't use #lazy_builder here because
// ToolbarItem::preRenderToolbarItem will insert #attributes before
// lazy_builder callback and this will produce Exception.
// This means that for now we always render Local Tasks item even
// when the tray is empty.
'#type' => 'link',
'#title' => $this->t('Local Tasks'),
'#url' => Url::fromRoute('<none>'),
'#attributes' => [
'class' => [
'toolbar-icon',
'toolbar-icon-local-tasks',
],
],
],
'tray' => [
'local_tasks' => $local_task_links,
],
'#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']],
];
}
}
$cacheability->applyTo($build);
return $build;
}
/**
* Gets a list of content entities.
*
* @return array
* An array of metadata about content entities.
*/
public function getBundleableEntitiesList() {
$entity_types = $this->entityTypeManager->getDefinitions();
$content_entities = [];
foreach ($entity_types as $key => $entity_type) {
if ($entity_type->getBundleEntityType() && ($entity_type->get('field_ui_base_route') != '')) {
$content_entities[$key] = [
'content_entity' => $key,
'content_entity_bundle' => $entity_type->getBundleEntityType(),
];
}
}
return $content_entities;
}
/**
* Gets an array of entity types that should trigger a menu rebuild.
*
* @return array
* An array of entity machine names.
*/
public function getRebuildEntityTypes() {
$types = ['menu'];
$content_entities = $this->getBundleableEntitiesList();
$types = array_merge($types, array_column($content_entities, 'content_entity_bundle'));
return $types;
}
}

View File

@@ -0,0 +1,295 @@
<?php
namespace Drupal\admin_toolbar_tools\Controller;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\CronInterface;
use Drupal\Core\Menu\ContextualLinkManager;
use Drupal\Core\Menu\LocalActionManager;
use Drupal\Core\Menu\LocalTaskManager;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\Core\Theme\Registry;
/**
* Controller for AdminToolbar Tools.
*
* @package Drupal\admin_toolbar_tools\Controller
*/
class ToolbarController extends ControllerBase {
/**
* A cron instance.
*
* @var \Drupal\Core\CronInterface
*/
protected $cron;
/**
* A menu link manager instance.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuLinkManager;
/**
* A context link manager instance.
*
* @var \Drupal\Core\Menu\ContextualLinkManager
*/
protected $contextualLinkManager;
/**
* A local task manager instance.
*
* @var \Drupal\Core\Menu\LocalTaskManager
*/
protected $localTaskLinkManager;
/**
* A local action manager instance.
*
* @var \Drupal\Core\Menu\LocalActionManager
*/
protected $localActionLinkManager;
/**
* A cache backend interface instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheRender;
/**
* A date time instance.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* A request stack symfony instance.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* A plugin cache clear instance.
*
* @var \Drupal\Core\Plugin\CachedDiscoveryClearerInterface
*/
protected $pluginCacheClearer;
/**
* The cache menu instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheMenu;
/**
* A TwigEnvironment instance.
*
* @var \Drupal\Core\Template\TwigEnvironment
*/
protected $twig;
/**
* The search theme.registry service.
*
* @var \Drupal\Core\Theme\Registry
*/
protected $themeRegistry;
/**
* Constructs a ToolbarController object.
*
* @param \Drupal\Core\CronInterface $cron
* A cron instance.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
* A menu link manager instance.
* @param \Drupal\Core\Menu\ContextualLinkManager $contextualLinkManager
* A context link manager instance.
* @param \Drupal\Core\Menu\LocalTaskManager $localTaskLinkManager
* A local task manager instance.
* @param \Drupal\Core\Menu\LocalActionManager $localActionLinkManager
* A local action manager instance.
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheRender
* A cache backend interface instance.
* @param \Drupal\Component\Datetime\TimeInterface $time
* A date time instance.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* A request stack symfony instance.
* @param \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $plugin_cache_clearer
* A plugin cache clear instance.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_menu
* A cache menu instance.
* @param \Drupal\Core\Template\TwigEnvironment $twig
* A TwigEnvironment instance.
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme.registry service.
*/
public function __construct(
CronInterface $cron,
MenuLinkManagerInterface $menuLinkManager,
ContextualLinkManager $contextualLinkManager,
LocalTaskManager $localTaskLinkManager,
LocalActionManager $localActionLinkManager,
CacheBackendInterface $cacheRender,
TimeInterface $time,
RequestStack $request_stack,
CachedDiscoveryClearerInterface $plugin_cache_clearer,
CacheBackendInterface $cache_menu,
TwigEnvironment $twig,
Registry $theme_registry
) {
$this->cron = $cron;
$this->menuLinkManager = $menuLinkManager;
$this->contextualLinkManager = $contextualLinkManager;
$this->localTaskLinkManager = $localTaskLinkManager;
$this->localActionLinkManager = $localActionLinkManager;
$this->cacheRender = $cacheRender;
$this->time = $time;
$this->requestStack = $request_stack;
$this->pluginCacheClearer = $plugin_cache_clearer;
$this->cacheMenu = $cache_menu;
$this->twig = $twig;
$this->themeRegistry = $theme_registry;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('cron'),
$container->get('plugin.manager.menu.link'),
$container->get('plugin.manager.menu.contextual_link'),
$container->get('plugin.manager.menu.local_task'),
$container->get('plugin.manager.menu.local_action'),
$container->get('cache.render'),
$container->get('datetime.time'),
$container->get('request_stack'),
$container->get('plugin.cache_clearer'),
$container->get('cache.menu'),
$container->get('twig'),
$container->get('theme.registry')
);
}
/**
* Reload the previous page.
*/
public function reloadPage() {
$request = $this->requestStack->getCurrentRequest();
if ($request->server->get('HTTP_REFERER')) {
return $request->server->get('HTTP_REFERER');
}
else {
return base_path();
}
}
/**
* Flushes all caches.
*/
public function flushAll() {
$this->messenger()->addMessage($this->t('All caches cleared.'));
drupal_flush_all_caches();
return new RedirectResponse($this->reloadPage());
}
/**
* Flushes css and javascript caches.
*/
public function flushJsCss() {
$this->state()
->set('system.css_js_query_string', base_convert($this->time->getCurrentTime(), 10, 36));
$this->messenger()->addMessage($this->t('CSS and JavaScript cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Flushes plugins caches.
*/
public function flushPlugins() {
$this->pluginCacheClearer->clearCachedDefinitions();
$this->messenger()->addMessage($this->t('Plugins cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Resets all static caches.
*/
public function flushStatic() {
drupal_static_reset();
$this->messenger()->addMessage($this->t('Static cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clears all cached menu data.
*/
public function flushMenu() {
$this->cacheMenu->invalidateAll();
$this->menuLinkManager->rebuild();
$this->contextualLinkManager->clearCachedDefinitions();
$this->localTaskLinkManager->clearCachedDefinitions();
$this->localActionLinkManager->clearCachedDefinitions();
$this->messenger()->addMessage($this->t('Routing and links cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clears all cached views data.
*/
public function flushViews() {
views_invalidate_cache();
$this->messenger()->addMessage($this->t('Views cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clears the twig cache.
*/
public function flushTwig() {
$this->twig->invalidate();
$this->messenger()->addMessage($this->t('Twig cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Run the cron.
*/
public function runCron() {
$this->cron->run();
$this->messenger()->addMessage($this->t('Cron ran successfully.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clear the rendered cache.
*/
public function cacheRender() {
$this->cacheRender->invalidateAll();
$this->messenger()->addMessage($this->t('Render cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Rebuild the theme registry.
*/
public function themeRebuild() {
$this->themeRegistry->reset();
$this->messenger()->addMessage($this->t('Theme registry rebuilt.'));
return new RedirectResponse($this->reloadPage());
}
}

View File

@@ -0,0 +1,119 @@
<?php
namespace Drupal\admin_toolbar_tools\Form;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Settings form for AdminToolbar Tools.
*
* @package Drupal\admin_toolbar_tools\Form
*/
class AdminToolbarToolsSettingsForm extends ConfigFormBase {
/**
* The cache menu instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheMenu;
/**
* The menu link manager instance.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuLinkManager;
/**
* AdminToolbarToolsSettingsForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The factory for configuration objects.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
* A menu link manager instance.
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheMenu
* A cache menu instance.
*/
public function __construct(ConfigFactoryInterface $configFactory, MenuLinkManagerInterface $menuLinkManager, CacheBackendInterface $cacheMenu) {
parent::__construct($configFactory);
$this->cacheMenu = $cacheMenu;
$this->menuLinkManager = $menuLinkManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('plugin.manager.menu.link'),
$container->get('cache.menu')
);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'admin_toolbar_tools.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'admin_toolbar_tools_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('admin_toolbar_tools.settings');
$form['max_bundle_number'] = [
'#type' => 'number',
'#title' => $this->t('Maximum number of bundle sub-menus to display'),
'#description' => $this->t('Loading a large number of items can cause performance issues.'),
'#default_value' => $config->get('max_bundle_number'),
];
$form['hoverintent_functionality'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable/Disable the hoverintent functionality'),
'#description' => $this->t('Check it if you want to enable the hoverintent feature.'),
'#default_value' => $config->get('hoverintent_functionality'),
];
$form['show_local_tasks'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable/Disable local tasks display'),
'#description' => $this->t('Local tasks such as node edit and delete.'),
'#default_value' => $config->get('show_local_tasks'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('admin_toolbar_tools.settings')
->set('max_bundle_number', $form_state->getValue('max_bundle_number'))
->set('hoverintent_functionality', $form_state->getValue('hoverintent_functionality'))
->set('show_local_tasks', $form_state->getValue('show_local_tasks'))
->save();
parent::submitForm($form, $form_state);
$this->cacheMenu->invalidateAll();
$this->menuLinkManager->rebuild();
}
}

View File

@@ -0,0 +1,723 @@
<?php
namespace Drupal\admin_toolbar_tools\Plugin\Derivative;
use Drupal\system\Entity\Menu;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a default implementation for menu link plugins.
*/
class ExtraLinks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The admin toolbar tools configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RouteProviderInterface $route_provider, ThemeHandlerInterface $theme_handler, ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->routeProvider = $route_provider;
$this->themeHandler = $theme_handler;
$this->config = $config_factory->get('admin_toolbar_tools.settings');
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('router.route_provider'),
$container->get('theme_handler'),
$container->get('config.factory'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$links = [];
$max_bundle_number = $this->config->get('max_bundle_number');
$entity_types = $this->entityTypeManager->getDefinitions();
$content_entities = [];
foreach ($entity_types as $key => $entity_type) {
if ($entity_type->getBundleEntityType() && ($entity_type->get('field_ui_base_route') != '')) {
$content_entities[$key] = [
'content_entity' => $key,
'content_entity_bundle' => $entity_type->getBundleEntityType(),
];
}
}
// Adds common links to entities.
foreach ($content_entities as $entities) {
$content_entity_bundle = $entities['content_entity_bundle'];
$content_entity = $entities['content_entity'];
$content_entity_bundle_storage = $this->entityTypeManager->getStorage($content_entity_bundle);
$bundles_ids = $content_entity_bundle_storage->getQuery()->sort('weight')->pager($max_bundle_number)->execute();
$bundles = $this->entityTypeManager->getStorage($content_entity_bundle)->loadMultiple($bundles_ids);
if (count($bundles) == $max_bundle_number && $this->routeExists('entity.' . $content_entity_bundle . '.collection')) {
$links[$content_entity_bundle . '.collection'] = [
'title' => $this->t('All types'),
'route_name' => 'entity.' . $content_entity_bundle . '.collection',
'parent' => 'entity.' . $content_entity_bundle . '.collection',
'weight' => -999,
] + $base_plugin_definition;
}
foreach ($bundles as $machine_name => $bundle) {
// Normally, the edit form for the bundle would be its root link.
$content_entity_bundle_root = NULL;
if ($this->routeExists('entity.' . $content_entity_bundle . '.overview_form')) {
// Some bundles have an overview/list form that make a better root
// link.
$content_entity_bundle_root = 'entity.' . $content_entity_bundle . '.overview_form.' . $machine_name;
$links[$content_entity_bundle_root] = [
'route_name' => 'entity.' . $content_entity_bundle . '.overview_form',
'parent' => 'entity.' . $content_entity_bundle . '.collection',
'route_parameters' => [$content_entity_bundle => $machine_name],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $bundle->getEntityTypeId(),
'entity_id' => $bundle->id(),
],
] + $base_plugin_definition;
$weight = $bundles[$machine_name]->get('weight');
if (isset($weight) && is_numeric($weight)) {
$links[$content_entity_bundle_root]['weight'] = $weight;
}
}
if ($this->routeExists('entity.' . $content_entity_bundle . '.edit_form')) {
$key = 'entity.' . $content_entity_bundle . '.edit_form.' . $machine_name;
$links[$key] = [
'route_name' => 'entity.' . $content_entity_bundle . '.edit_form',
'parent' => 'entity.' . $content_entity_bundle . '.collection',
'route_parameters' => [$content_entity_bundle => $machine_name],
] + $base_plugin_definition;
if (empty($content_entity_bundle_root)) {
$content_entity_bundle_root = $key;
$links[$key]['parent'] = 'entity.' . $content_entity_bundle . '.collection';
// When not grouped by bundle, use bundle name as title.
$links[$key]['class'] = 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity';
$links[$key]['metadata'] = [
'entity_type' => $bundle->getEntityTypeId(),
'entity_id' => $bundle->id(),
];
}
else {
$links[$key]['parent'] = $base_plugin_definition['id'] . ':' . $content_entity_bundle_root;
$links[$key]['title'] = $this->t('Edit');
}
}
if ($this->moduleHandler->moduleExists('field_ui')) {
if ($this->routeExists('entity.' . $content_entity . '.field_ui_fields')) {
$links['entity.' . $content_entity . '.field_ui_fields' . $machine_name] = [
'title' => $this->t('Manage fields'),
'route_name' => 'entity.' . $content_entity . '.field_ui_fields',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 1,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.entity_form_display.' . $content_entity . '.default')) {
$links['entity.entity_form_display.' . $content_entity . '.default' . $machine_name] = [
'title' => $this->t('Manage form display'),
'route_name' => 'entity.entity_form_display.' . $content_entity . '.default',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 2,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.entity_view_display.' . $content_entity . '.default')) {
$links['entity.entity_view_display.' . $content_entity . '.default.' . $machine_name] = [
'title' => $this->t('Manage display'),
'route_name' => 'entity.entity_view_display.' . $content_entity . '.default',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 3,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.' . $bundle->getEntityTypeId() . '.entity_permissions_form')) {
$links['entity.entity_permissions_form.' . $content_entity . '.default.' . $machine_name] = [
'title' => $this->t('Manage permissions'),
'route_name' => 'entity.' . $bundle->getEntityTypeId() . '.entity_permissions_form',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [
$bundle->getEntityTypeId() => $machine_name,
],
'weight' => 3,
] + $base_plugin_definition;
}
}
if ($this->moduleHandler->moduleExists('devel') && $this->routeExists('entity.' . $content_entity_bundle . '.devel_load')) {
$links['entity.' . $content_entity_bundle . '.devel_load.' . $machine_name] = [
'title' => $this->t('Devel'),
'route_name' => 'entity.' . $content_entity_bundle . '.devel_load',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 4,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.' . $content_entity_bundle . '.delete_form')) {
$links['entity.' . $content_entity_bundle . '.delete_form.' . $machine_name] = [
'title' => $this->t('Delete'),
'route_name' => 'entity.' . $content_entity_bundle . '.delete_form',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 5,
] + $base_plugin_definition;
}
}
}
// Adds user links.
$links['user.admin_create'] = [
'title' => $this->t('Add user'),
'route_name' => 'user.admin_create',
'parent' => 'entity.user.collection',
] + $base_plugin_definition;
$links['user.admin_permissions'] = [
'title' => $this->t('Permissions'),
'route_name' => 'user.admin_permissions',
'parent' => 'entity.user.collection',
] + $base_plugin_definition;
$links['entity.user_role.collection'] = [
'title' => $this->t('Roles'),
'route_name' => 'entity.user_role.collection',
'parent' => 'entity.user.collection',
] + $base_plugin_definition;
$links['user.logout'] = [
'title' => $this->t('Logout'),
'route_name' => 'user.logout',
'parent' => 'admin_toolbar_tools.help',
'weight' => 10,
] + $base_plugin_definition;
$links['user.role_add'] = [
'title' => $this->t('Add role'),
'route_name' => 'user.role_add',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.collection',
'weight' => -50,
] + $base_plugin_definition;
// Adds sub-links to Account settings link.
if ($this->moduleHandler->moduleExists('field_ui')) {
$links['entity.user.field_ui_fields_'] = [
'title' => $this->t('Manage fields'),
'route_name' => 'entity.user.field_ui_fields',
'parent' => 'entity.user.admin_form',
'weight' => 1,
] + $base_plugin_definition;
$links['entity.entity_form_display.user.default_'] = [
'title' => $this->t('Manage form display'),
'route_name' => 'entity.entity_form_display.user.default',
'parent' => 'entity.user.admin_form',
'weight' => 2,
] + $base_plugin_definition;
$links['entity.entity_view_display.user.default_'] = [
'title' => $this->t('Manage display'),
'route_name' => 'entity.entity_view_display.user.default',
'parent' => 'entity.user.admin_form',
'weight' => 3,
] + $base_plugin_definition;
}
foreach ($this->entityTypeManager->getStorage('user_role')->loadMultiple() as $role) {
$links['entity.user_role.edit_form.' . $role->id()] = [
'route_name' => 'entity.user_role.edit_form',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.collection',
'weight' => $role->getWeight(),
'route_parameters' => ['user_role' => $role->id()],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $role->getEntityTypeId(),
'entity_id' => $role->id(),
],
] + $base_plugin_definition;
$links['entity.user_role.edit_permissions_form.' . $role->id()] = [
'title' => $this->t('Edit permissions'),
'route_name' => 'entity.user_role.edit_permissions_form',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.edit_form.' . $role->id(),
'route_parameters' => ['user_role' => $role->id()],
] + $base_plugin_definition;
if ($role->id() != 'anonymous' && $role->id() != 'authenticated') {
$links['entity.user_role.delete_form.' . $role->id()] = [
'title' => $this->t('Delete'),
'route_name' => 'entity.user_role.delete_form',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.edit_form.' . $role->id(),
'route_parameters' => ['user_role' => $role->id()],
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('devel')) {
$links['entity.user_role.devel_load.' . $role->id()] = [
'title' => $this->t('Devel'),
'route_name' => 'entity.user_role.devel_load',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.edit_form.' . $role->id(),
'route_parameters' => ['user_role' => $role->id()],
] + $base_plugin_definition;
}
}
if ($this->moduleHandler->moduleExists('node')) {
$links['node.type_add'] = [
'title' => $this->t('Add content type'),
'route_name' => 'node.type_add',
'parent' => 'entity.node_type.collection',
'weight' => -2,
] + $base_plugin_definition;
$links['node.add'] = [
'title' => $this->t('Add content'),
'route_name' => 'node.add_page',
'parent' => 'system.admin_content',
] + $base_plugin_definition;
// Adds node links for each content type.
foreach ($this->entityTypeManager->getStorage('node_type')->loadMultiple() as $type) {
$links['node.add.' . $type->id()] = [
'route_name' => 'node.add',
'parent' => $base_plugin_definition['id'] . ':node.add',
'route_parameters' => ['node_type' => $type->id()],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $type->getEntityTypeId(),
'entity_id' => $type->id(),
],
] + $base_plugin_definition;
}
}
if ($this->moduleHandler->moduleExists('field_ui')) {
$links['field_ui.entity_form_mode_add'] = [
'title' => $this->t('Add form mode'),
'route_name' => 'field_ui.entity_form_mode_add',
'parent' => 'entity.entity_form_mode.collection',
] + $base_plugin_definition;
$links['field_ui.entity_view_mode_add'] = [
'title' => $this->t('Add view mode'),
'route_name' => 'field_ui.entity_view_mode_add',
'parent' => 'entity.entity_view_mode.collection',
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('taxonomy')) {
$links['entity.taxonomy_vocabulary.add_form'] = [
'title' => $this->t('Add vocabulary'),
'route_name' => 'entity.taxonomy_vocabulary.add_form',
'parent' => 'entity.taxonomy_vocabulary.collection',
'weight' => -998,
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('menu_ui')) {
$links['entity.menu.add_form'] = [
'title' => $this->t('Add menu'),
'route_name' => 'entity.menu.add_form',
'parent' => 'entity.menu.collection',
'weight' => -2,
] + $base_plugin_definition;
// Adds links to /admin/structure/menu.
$menus = $this->entityTypeManager->getStorage('menu')->loadMultiple();
uasort($menus, [Menu::class, 'sort']);
$menus = array_slice($menus, 0, $max_bundle_number);
if (count($menus) == $max_bundle_number) {
$links['entity.menu.collection'] = [
'title' => $this->t('All menus'),
'route_name' => 'entity.menu.collection',
'parent' => 'entity.menu.collection',
'weight' => -1,
] + $base_plugin_definition;
}
$weight = 0;
foreach ($menus as $menu_id => $menu) {
$links['entity.menu.edit_form.' . $menu_id] = [
'route_name' => 'entity.menu.edit_form',
'parent' => 'entity.menu.collection',
'route_parameters' => ['menu' => $menu_id],
'weight' => $weight,
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $menu->getEntityTypeId(),
'entity_id' => $menu->id(),
],
] + $base_plugin_definition;
$links['entity.menu.add_link_form.' . $menu_id] = [
'title' => $this->t('Add link'),
'route_name' => 'entity.menu.add_link_form',
'parent' => $base_plugin_definition['id'] . ':entity.menu.edit_form.' . $menu_id,
'route_parameters' => ['menu' => $menu_id],
] + $base_plugin_definition;
// Un-deletable menus.
$un_deletable_menus = [
'admin',
'devel',
'footer',
'main',
'tools',
'account',
];
if (!in_array($menu_id, $un_deletable_menus)) {
$links['entity.menu.delete_form.' . $menu_id] = [
'title' => $this->t('Delete'),
'route_name' => 'entity.menu.delete_form',
'parent' => $base_plugin_definition['id'] . ':entity.menu.edit_form.' . $menu_id,
'route_parameters' => ['menu' => $menu_id],
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('devel') && $this->routeExists('entity.menu.devel_load')) {
$links['entity.menu.devel_load.' . $menu_id] = [
'title' => $this->t('Devel'),
'route_name' => 'entity.menu.devel_load',
'parent' => $base_plugin_definition['id'] . ':entity.menu.edit_form.' . $menu_id,
'route_parameters' => ['menu' => $menu_id],
] + $base_plugin_definition;
}
$weight++;
}
}
// If module block_content is enabled.
if ($this->moduleHandler->moduleExists('block_content')) {
$links['block_content.add_page'] = [
'title' => $this->t('Add custom block'),
'route_name' => 'block_content.add_page',
'parent' => 'block.admin_display',
] + $base_plugin_definition;
$links['entity.block_content.collection'] = [
'title' => $this->t('Custom block library'),
'route_name' => 'entity.block_content.collection',
'parent' => 'block.admin_display',
] + $base_plugin_definition;
$links['entity.block_content_type.collection'] = [
'title' => $this->t('Block types'),
'route_name' => 'entity.block_content_type.collection',
'parent' => 'block.admin_display',
] + $base_plugin_definition;
}
// If module Contact is enabled.
if ($this->moduleHandler->moduleExists('contact')) {
$links['contact.form_add'] = [
'title' => $this->t('Add contact form'),
'route_name' => 'contact.form_add',
'parent' => 'entity.contact_form.collection',
'weight' => -5,
] + $base_plugin_definition;
}
// If module Update Manager is enabled.
if ($this->moduleHandler->moduleExists('update')) {
$links['update.module_install'] = [
'title' => $this->t('Install new module'),
'route_name' => 'update.module_install',
'parent' => 'system.modules_list',
] + $base_plugin_definition;
$links['update.module_update'] = [
'title' => $this->t('Update'),
'route_name' => 'update.module_update',
'parent' => 'system.modules_list',
] + $base_plugin_definition;
$links['update.theme_install'] = [
'title' => $this->t('Install new theme'),
'route_name' => 'update.theme_install',
'parent' => 'system.themes_page',
] + $base_plugin_definition;
$links['update.theme_update'] = [
'title' => $this->t('Update'),
'route_name' => 'update.theme_update',
'parent' => 'system.themes_page',
] + $base_plugin_definition;
}
// If module Devel is enabled.
if ($this->moduleHandler->moduleExists('devel')) {
$links['devel'] = [
'title' => $this->t('Development'),
'route_name' => 'system.admin_config_development',
'parent' => 'admin_toolbar_tools.help',
'weight' => '-8',
] + $base_plugin_definition;
$links['devel.admin_settings'] = [
'title' => $this->t('Devel settings'),
'route_name' => 'devel.admin_settings',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.configs_list'] = [
'title' => $this->t('Config editor'),
'route_name' => 'devel.configs_list',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.reinstall'] = [
'title' => $this->t('Reinstall modules'),
'route_name' => 'devel.reinstall',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.menu_rebuild'] = [
'title' => $this->t('Rebuild menu'),
'route_name' => 'devel.menu_rebuild',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.state_system_page'] = [
'title' => $this->t('State editor'),
'route_name' => 'devel.state_system_page',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.theme_registry'] = [
'title' => $this->t('Theme registry'),
'route_name' => 'devel.theme_registry',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.entity_info_page'] = [
'title' => $this->t('Entity info'),
'route_name' => 'devel.entity_info_page',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.session'] = [
'title' => $this->t('Session viewer'),
'route_name' => 'devel.session',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.element_info'] = [
'title' => $this->t('Element Info'),
'route_name' => 'devel.elements_page',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
// Menu link for the Toolbar module.
$links['devel.toolbar.settings'] = [
'title' => $this->t('Devel Toolbar Settings'),
'route_name' => 'devel.toolbar.settings_form',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
if ($this->moduleHandler->moduleExists('webprofiler')) {
$links['devel.webprofiler'] = [
'title' => $this->t('Webprofiler settings'),
'route_name' => 'webprofiler.settings',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
}
// If module Devel PHP is enabled.
if ($this->moduleHandler->moduleExists('devel_php') && $this->routeExists('devel_php.execute_php')) {
$links['devel.devel_php.execute_php'] = [
'title' => $this->t('Execute PHP Code'),
'route_name' => 'devel_php.execute_php',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
}
}
// If module Views Ui enabled.
if ($this->moduleHandler->moduleExists('views_ui')) {
$links['views_ui.add'] = [
'title' => $this->t('Add view'),
'route_name' => 'views_ui.add',
'parent' => 'entity.view.collection',
'weight' => -5,
] + $base_plugin_definition;
$views = $this->entityTypeManager->getStorage('view')->loadMultiple();
foreach ($views as $view) {
$links['views_ui.' . $view->id()] = [
'title' => $view->label(),
'route_name' => 'entity.view.edit_form',
'route_parameters' => ['view' => $view->id()],
'parent' => 'entity.view.collection',
] + $base_plugin_definition;
}
$links['views_ui.field_list'] = [
'title' => $this->t('Used in views'),
'route_name' => 'views_ui.reports_fields',
'parent' => 'entity.field_storage_config.collection',
] + $base_plugin_definition;
}
// Adds theme management links.
$links['system.theme_settings'] = [
'title' => $this->t('Settings'),
'route_name' => 'system.theme_settings',
'parent' => 'system.themes_page',
] + $base_plugin_definition;
$installed_themes = $this->installedThemes();
foreach ($installed_themes as $key_theme => $label_theme) {
$links['system.theme_settings_theme.' . $key_theme] = [
'title' => $label_theme,
'route_name' => 'system.theme_settings_theme',
'parent' => $base_plugin_definition['id'] . ':system.theme_settings',
'route_parameters' => ['theme' => $key_theme],
] + $base_plugin_definition;
}
// If module Language enabled.
if ($this->moduleHandler->moduleExists('language')) {
$links['language.negotiation'] = [
'title' => $this->t('Detection and selection'),
'route_name' => 'language.negotiation',
'parent' => 'entity.configurable_language.collection',
] + $base_plugin_definition;
$links['language.add'] = [
'title' => $this->t('Add language'),
'route_name' => 'language.add',
'parent' => 'entity.configurable_language.collection',
] + $base_plugin_definition;
}
// If module Media enabled.
if ($this->moduleHandler->moduleExists('media')) {
$links['media.type_add'] = [
'title' => $this->t('Add media type'),
'route_name' => 'entity.media_type.add_form',
'parent' => 'entity.media_type.collection',
'weight' => -2,
] + $base_plugin_definition;
// Displays media link in toolbar.
$links['media_page'] = [
'title' => $this->t('Media'),
'route_name' => 'entity.media.collection',
'parent' => 'system.admin_content',
] + $base_plugin_definition;
if ($this->moduleHandler->moduleExists('media_library') && $this->routeExists('view.media_library.page')) {
$links['media_library'] = [
'title' => $this->t('Media library'),
'route_name' => 'view.media_library.page',
'parent' => $base_plugin_definition['id'] . ':media_page',
] + $base_plugin_definition;
}
$links['add_media'] = [
'title' => $this->t('Add media'),
'route_name' => 'entity.media.add_page',
'parent' => $base_plugin_definition['id'] . ':media_page',
] + $base_plugin_definition;
// Adds links for each media type.
foreach ($this->entityTypeManager->getStorage('media_type')->loadMultiple() as $type) {
$links['media.add.' . $type->id()] = [
'route_name' => 'entity.media.add_form',
'parent' => $base_plugin_definition['id'] . ':add_media',
'route_parameters' => ['media_type' => $type->id()],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $type->getEntityTypeId(),
'entity_id' => $type->id(),
],
] + $base_plugin_definition;
}
}
// If module Config enabled.
if ($this->moduleHandler->moduleExists('config')) {
$links['config.import'] = [
'title' => $this->t('Import'),
'route_name' => 'config.import_full',
'parent' => 'config.sync',
'weight' => 1,
] + $base_plugin_definition;
$links['config.export'] = [
'title' => $this->t('Export'),
'route_name' => 'config.export_full',
'parent' => 'config.sync',
'weight' => 2,
] + $base_plugin_definition;
}
// Adds a menu link to clear Views cache.
if ($this->moduleHandler->moduleExists('views')) {
$links['flush_views'] = [
'title' => $this->t('Flush views cache'),
'route_name' => 'admin_toolbar_tools.flush_views',
'parent' => 'admin_toolbar_tools.flush',
] + $base_plugin_definition;
// Adding a menu link to Files.
if ($this->moduleHandler->moduleExists('file') && $this->routeExists('view.files.page_1')) {
$links['view.files'] = [
'title' => $this->t('Files'),
'route_name' => 'view.files.page_1',
'parent' => 'system.admin_content',
] + $base_plugin_definition;
}
}
return $links;
}
/**
* Determine if a route exists by name.
*
* @param string $route_name
* The name of the route to check.
*
* @return bool
* Whether a route with that route name exists.
*/
public function routeExists($route_name) {
return (count($this->routeProvider->getRoutesByNames([$route_name])) === 1);
}
/**
* Lists all installed themes.
*
* @return array
* The list of installed themes.
*/
public function installedThemes() {
$themeHandler = $this->themeHandler;
$all_themes = $themeHandler->listInfo();
$themes_installed = [];
foreach ($all_themes as $key_theme => $theme) {
if ($themeHandler->hasUi($key_theme)) {
$themes_installed[$key_theme] = $themeHandler->getName($key_theme);
}
}
return $themes_installed;
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace Drupal\admin_toolbar_tools\Plugin\Menu;
use Drupal\Core\Entity\EntityDescriptionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Drupal\node\NodeTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a menu link plugins for configuration entities.
*/
class MenuLinkEntity extends MenuLinkDefault {
/**
* The entity represented in the menu link.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* Constructs a new MenuLinkEntity.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
* The static override storage.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);
$this->entity = $entity_type_manager->getStorage($this->pluginDefinition['metadata']['entity_type'])->load($this->pluginDefinition['metadata']['entity_id']);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('menu_link.static.overrides'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getTitle() {
if ($this->entity) {
return (string) $this->entity->label();
}
return $this->pluginDefinition['title'] ?: $this->t('Missing');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
// @todo Remove node_type special handling.
if ($this->entity instanceof EntityDescriptionInterface || $this->entity instanceof NodeTypeInterface) {
return $this->entity->getDescription();
}
return parent::getDescription();
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
if ($this->entity) {
return $this->entity->getCacheContexts();
}
return parent::getCacheContexts();
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
if ($this->entity) {
return $this->entity->getCacheTags();
}
return parent::getCacheTags();
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
if ($this->entity) {
return $this->entity->getCacheMaxAge();
}
return parent::getCacheMaxAge();
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Drupal\Tests\admin_toolbar_tools\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Tests for the existence of Admin Toolbar tools new links.
*
* @group admin_toolbar
*/
class AdminToolbarToolsAlterTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'toolbar',
'admin_toolbar',
'admin_toolbar_tools',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A test user with permission to access the administrative toolbar.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create and log in an administrative user.
$this->adminUser = $this->drupalCreateUser([
'access toolbar',
'access administration pages',
'administer site configuration',
]);
$this->drupalLogin($this->adminUser);
}
/**
* Tests for the hover of sub menus.
*/
public function testAdminToolbarTools() {
// Assert that special menu items are present in the HTML.
$this->assertSession()->responseContains('class="toolbar-icon toolbar-icon-admin-toolbar-tools-flush"');
}
}

View File

@@ -0,0 +1,38 @@
{
"name": "drupal/admin_toolbar",
"description": "Provides a drop-down menu interface to the core Drupal Toolbar.",
"type": "drupal-module",
"keywords": [
"Drupal",
"Toolbar"
],
"homepage": "http://drupal.org/project/admin_toolbar",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Wilfrid Roze (eme)",
"homepage": "https://www.drupal.org/u/eme",
"role": "Maintainer"
},
{
"name": "Romain Jarraud (romainj)",
"homepage": "https://www.drupal.org/u/romainj",
"role": "Maintainer"
},
{
"name": "Adrian Cid Almaguer (adriancid)",
"email": "adriancid@gmail.com",
"homepage": "https://www.drupal.org/u/adriancid",
"role": "Maintainer"
},
{
"name": "Mohamed Anis Taktak (matio89)",
"homepage": "https://www.drupal.org/u/matio89",
"role": "Maintainer"
}
],
"support": {
"issues": "https://www.drupal.org/project/issues/admin_toolbar",
"source": "https://git.drupalcode.org/project/admin_toolbar"
}
}

View File

@@ -0,0 +1,129 @@
.toolbar-tray-horizontal .menu-item:hover {
background: #fff;
}
.toolbar-tray-horizontal .menu-item a:focus {
background: #abeae4;
}
.toolbar-tray-horizontal .toolbar-menu:not(:first-child) li.menu-item--expanded > a:focus {
background-position: center right;
background-image: url(../misc/icons/0074bd/chevron-right.svg);
background-repeat: no-repeat;
}
.toolbar-tray-horizontal .menu-item--expanded .menu {
background: #fff;
width: auto;
height: auto;
}
.toolbar-tray-horizontal .menu-item--expanded {
background-color: #f5f5f2;
}
.toolbar-tray-horizontal ul li li.menu-item {
border-top: none transparent;
border-right: 1px solid #ddd;
border-bottom: 1px solid #ddd;
border-left: 1px solid #ddd;
}
.toolbar .toolbar-tray-horizontal .menu-item:last-child {
border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
}
.toolbar .toolbar-tray-horizontal ul ul li.menu-item:first-child {
border-top: 1px solid #ddd;
}
.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul,
.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul ul,
.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul ul ul,
.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul ul ul ul ul {
display: none;
left: -999em; /* LTR */
}
/* Lists nested under hovered list items */
.toolbar-tray-horizontal li.menu-item--expanded.hover-intent ul,
.toolbar-tray-horizontal li li.menu-item--expanded.hover-intent ul,
.toolbar-tray-horizontal li li li.menu-item--expanded.hover-intent ul,
.toolbar-tray-horizontal li li li li.menu-item--expanded.hover-intent ul,
.toolbar-tray-horizontal li li li li li.menu-item--expanded.hover-intent ul {
display: block;
left: auto; /* LTR */
}
.toolbar-tray-horizontal .menu ul li a,
.toolbar-tray-horizontal .menu ul .toolbar-icon {
padding: 12px 15px 12px 12px;
}
.toolbar-tray-horizontal ul li.menu-item--expanded.hover-intent ul {
display: block;
position: absolute;
width: 200px;
box-shadow: 2px 2px 3px hsla(0, 0%, 0%, 0.4);
z-index: 1;
}
.toolbar-tray-horizontal ul li.menu-item--expanded .menu-item > ul {
display: none;
}
.toolbar-tray-horizontal ul li.menu-item--expanded ul li.menu-item--expanded {
background-position: center right;
background-image: url(../misc/icons/0074bd/chevron-right.svg);
background-repeat: no-repeat;
}
.toolbar-tray-horizontal ul li.menu-item--expanded .menu-item.hover-intent ul {
display: block;
margin: -40px 0 0 197px;
}
.toolbar-tray-horizontal li:hover ul li {
float: none;
}
.toolbar-tray-horizontal li.hover-intent ul li {
float: none;
}
.toolbar-tray-horizontal .toolbar .level-2 > ul {
position: absolute;
padding-top: 0;
top: 0;
left: 200px;
width: 200px;
}
.toolbar .toolbar-tray-vertical li.open > ul.toolbar-menu.clearfix {
display: block;
}
.toolbar-menu .menu-item > span {
padding: 1em 1.3333em;
display: block;
color: #434343;
cursor: pointer;
}
[dir="rtl"] .toolbar-tray-horizontal ul li.menu-item--expanded ul li.menu-item--expanded {
background-position: center left;
background-image: url(../misc/icons/0074bd/chevron-left.svg);
}
[dir="rtl"] .toolbar-tray-horizontal ul li.menu-item--expanded .menu-item.hover-intent ul {
margin: -40px 197px 0 0;
}
[dir="rtl"] .toolbar-tray-horizontal li:hover ul li {
float: none;
}
[dir="rtl"] .toolbar-tray-horizontal li.hover-intent ul li {
float: none;
}

View File

@@ -0,0 +1,33 @@
#toolbar-item-administration-search-tray {
padding-left: 1em;
}
#admin-toolbar-search-tab .toolbar-item:before {
background-image: url(../misc/icons/bebebe/loupe.svg);
}
#admin-toolbar-search-tab .toolbar-item:active:before,
#admin-toolbar-search-tab .toolbar-item.is-active:before {
background-image: url(../misc/icons/ffffff/loupe.svg);
}
#toolbar-item-administration-search-tray label {
display: inline-block;
color: #000;
margin-right: .5em;
font-weight: bold;
}
#toolbar-item-administration-search-tray div.form-item {
margin: 0.75em 0;
}
#toolbar-item-administration-search-tray input {
display: inline-block;
padding: 0.3em 0.4em 0.3em 0.5em;
font-size: 1em;
}
.ui-autocomplete .ui-menu-item span.admin-toolbar-search-url {
display: none;
}

View File

@@ -0,0 +1,13 @@
(function ($) {
$(document).ready(function () {
$('.toolbar-tray.toolbar-tray-horizontal .menu-item.menu-item--expanded').hover(function () {
// At the current depth, we should delete all "hover-intent" classes.
// Other wise we get unwanted behaviour where menu items are expanded while already in hovering other ones.
$(this).parent().find('li').removeClass('hover-intent');
$(this).addClass('hover-intent');
},
function () {
$(this).removeClass('hover-intent');
});
});
})(jQuery);

View File

@@ -0,0 +1,16 @@
(function ($) {
$(document).ready(function () {
$('.toolbar-tray-horizontal li.menu-item--expanded, .toolbar-tray-horizontal ul li.menu-item--expanded .menu-item').hoverIntent({
over: function () {
// At the current depth, we should delete all "hover-intent" classes.
// Other wise we get unwanted behaviour where menu items are expanded while already in hovering other ones.
$(this).parent().find('li').removeClass('hover-intent');
$(this).addClass('hover-intent');
},
out: function () {
$(this).removeClass('hover-intent');
},
timeout: 250
});
});
})(jQuery);

View File

@@ -0,0 +1,47 @@
(function ($, Drupal) {
Drupal.behaviors.adminToolbar = {
attach: function (context, settings) {
$('a.toolbar-icon', context).removeAttr('title');
// Make the toolbar menu navigable with keyboard.
$('ul.toolbar-menu li.menu-item--expanded a', context).on('focusin', function () {
$('li.menu-item--expanded', context).removeClass('hover-intent');
$(this).parents('li.menu-item--expanded').addClass('hover-intent');
});
$('ul.toolbar-menu li.menu-item a', context).keydown(function (e) {
if ((e.shiftKey && (e.keyCode || e.which) == 9)) {
if ($(this).parent('.menu-item').prev().hasClass('menu-item--expanded')) {
$(this).parent('.menu-item').prev().addClass('hover-intent');
}
}
});
$('.toolbar-menu:first-child > .menu-item:not(.menu-item--expanded) a, .toolbar-tab > a', context).on('focusin', function () {
$('.menu-item--expanded').removeClass('hover-intent');
});
$('.toolbar-menu:first-child > .menu-item', context).on('hover', function () {
$(this, 'a').css("background: #fff;");
});
$('ul:not(.toolbar-menu)', context).on({
mousemove: function () {
$('li.menu-item--expanded').removeClass('hover-intent');
},
hover: function () {
$('li.menu-item--expanded').removeClass('hover-intent');
}
});
// Always hide the dropdown menu on mobile.
if (window.matchMedia("(max-width: 767px)").matches && $('body').hasClass('toolbar-tray-open')) {
$('body').removeClass('toolbar-tray-open');
$('#toolbar-item-administration').removeClass('is-active');
$('#toolbar-item-administration-tray').removeClass('is-active');
};
}
};
})(jQuery, Drupal);

View File

@@ -0,0 +1,156 @@
;/*!
* hoverIntent v1.8.1 // 2014.08.11 // jQuery v1.9.1+
* http://briancherne.github.io/jquery-hoverIntent/
*
* You may use hoverIntent under the terms of the MIT license. Basically that
* means you are free to use hoverIntent as long as this header is left intact.
* Copyright 2007, 2014 Brian Cherne
*/
/* hoverIntent is similar to jQuery's built-in "hover" method except that
* instead of firing the handlerIn function immediately, hoverIntent checks
* to see if the user's mouse has slowed down (beneath the sensitivity
* threshold) before firing the event. The handlerOut function is only
* called after a matching handlerIn.
*
* // basic usage ... just like .hover()
* .hoverIntent( handlerIn, handlerOut )
* .hoverIntent( handlerInOut )
*
* // basic usage ... with event delegation!
* .hoverIntent( handlerIn, handlerOut, selector )
* .hoverIntent( handlerInOut, selector )
*
* // using a basic configuration object
* .hoverIntent( config )
*
* @param handlerIn function OR configuration object
* @param handlerOut function OR selector for delegation OR undefined
* @param selector selector OR undefined
* @author Brian Cherne <brian(at)cherne(dot)net>
*/(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (jQuery && !jQuery.fn.hoverIntent) {
factory(jQuery);
}
})(function ($) {
'use strict';
// default configuration values
var _cfg = {
interval: 100,
sensitivity: 6,
timeout: 0
};
// counter used to generate an ID for each instance
var INSTANCE_COUNT = 0;
// current X and Y position of mouse, updated during mousemove tracking (shared across instances)
var cX, cY;
// saves the current pointer position coordinates based on the given mousemove event
var track = function (ev) {
cX = ev.pageX;
cY = ev.pageY;
};
// compares current and previous mouse positions
var compare = function (ev,$el,s,cfg) {
// compare mouse positions to see if pointer has slowed enough to trigger `over` function
if ( Math.sqrt( (s.pX - cX) * (s.pX - cX) + (s.pY - cY) * (s.pY - cY) ) < cfg.sensitivity ) {
$el.off(s.event,track);
delete s.timeoutId;
// set hoverIntent state as active for this element (permits `out` handler to trigger)
s.isActive = true;
// overwrite old mouseenter event coordinates with most recent pointer position
ev.pageX = cX; ev.pageY = cY;
// clear coordinate data from state object
delete s.pX; delete s.pY;
return cfg.over.apply($el[0],[ev]);
} else {
// set previous coordinates for next comparison
s.pX = cX; s.pY = cY;
// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
s.timeoutId = setTimeout( function () {compare(ev, $el, s, cfg);} , cfg.interval );
}
};
// triggers given `out` function at configured `timeout` after a mouseleave and clears state
var delay = function (ev,$el,s,out) {
delete $el.data('hoverIntent')[s.id];
return out.apply($el[0],[ev]);
};
$.fn.hoverIntent = function (handlerIn,handlerOut,selector) {
// instance ID, used as a key to store and retrieve state information on an element
var instanceId = INSTANCE_COUNT++;
// extend the default configuration and parse parameters
var cfg = $.extend({}, _cfg);
if ( $.isPlainObject(handlerIn) ) {
cfg = $.extend(cfg, handlerIn);
if ( !$.isFunction(cfg.out) ) {
cfg.out = cfg.over;
}
} else if ( $.isFunction(handlerOut) ) {
cfg = $.extend(cfg, { over: handlerIn, out: handlerOut, selector: selector } );
} else {
cfg = $.extend(cfg, { over: handlerIn, out: handlerIn, selector: handlerOut } );
}
// A private function for handling mouse 'hovering'
var handleHover = function (e) {
// cloned event to pass to handlers (copy required for event object to be passed in IE)
var ev = $.extend({},e);
// the current target of the mouse event, wrapped in a jQuery object
var $el = $(this);
// read hoverIntent data from element (or initialize if not present)
var hoverIntentData = $el.data('hoverIntent');
if (!hoverIntentData) { $el.data('hoverIntent', (hoverIntentData = {})); }
// read per-instance state from element (or initialize if not present)
var state = hoverIntentData[instanceId];
if (!state) { hoverIntentData[instanceId] = state = { id: instanceId }; }
// state properties:
// id = instance ID, used to clean up data
// timeoutId = timeout ID, reused for tracking mouse position and delaying "out" handler
// isActive = plugin state, true after `over` is called just until `out` is called
// pX, pY = previously-measured pointer coordinates, updated at each polling interval
// event = string representing the namespaced event used for mouse tracking
// clear any existing timeout
if (state.timeoutId) { state.timeoutId = clearTimeout(state.timeoutId); }
// namespaced event used to register and unregister mousemove tracking
var mousemove = state.event = 'mousemove.hoverIntent.hoverIntent' + instanceId;
// handle the event, based on its type
if (e.type === 'mouseenter') {
// do nothing if already active
if (state.isActive) { return; }
// set "previous" X and Y position based on initial entry point
state.pX = ev.pageX; state.pY = ev.pageY;
// update "current" X and Y position based on mousemove
$el.off(mousemove,track).on(mousemove,track);
// start polling interval (self-calling timeout) to compare mouse coordinates over time
state.timeoutId = setTimeout( function () {compare(ev,$el,state,cfg);} , cfg.interval );
} else { // "mouseleave"
// do nothing if not already active
if (!state.isActive) { return; }
// unbind expensive mousemove event
$el.off(mousemove,track);
// if hoverIntent state is true, then call the mouseOut function after the specified delay
state.timeoutId = setTimeout( function () {delay(ev,$el,state,cfg.out);} , cfg.timeout );
}
};
// listen for mouseenter and mouseleave
return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector);
};
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#0074bd" d="M7.951 7.645c-.193.196-.193.516 0 .71l3.258 3.29c.193.193.191.519-.002.709l-1.371 1.371c-.193.192-.512.191-.707 0l-5.335-5.371c-.194-.194-.194-.514 0-.708l5.335-5.369c.195-.195.514-.195.707-.001l1.371 1.371c.193.194.195.513.002.709l-3.258 3.289z"/></svg>

After

Width:  |  Height:  |  Size: 342 B

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#0074bd" d="M8.053 8.355c.193-.195.193-.517 0-.711l-3.26-3.289c-.193-.195-.192-.514.002-.709l1.371-1.371c.194-.194.512-.193.706.001l5.335 5.369c.195.195.195.515 0 .708l-5.335 5.37c-.194.192-.512.193-.706.002l-1.371-1.371c-.194-.195-.195-.514-.002-.709l3.26-3.29z"/></svg>

After

Width:  |  Height:  |  Size: 347 B

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#BEBEBE" d="M14.648 12.788l-4.23-4.228c.525-.855.834-1.858.834-2.938 0-3.105-2.52-5.624-5.627-5.624-3.106.002-5.625 2.521-5.625 5.627 0 3.105 2.519 5.625 5.625 5.625 1.076 0 2.08-.309 2.936-.832l4.229 4.229c.194.195.515.195.707 0l1.151-1.146c.194-.2.194-.519 0-.713zm-13.35-7.163c0-2.39 1.938-4.327 4.327-4.327 2.391 0 4.328 1.937 4.328 4.327 0 2.391-1.936 4.327-4.328 4.327-2.39 0-4.327-1.936-4.327-4.327z"/></svg>

After

Width:  |  Height:  |  Size: 491 B

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#0074bd" d="M8.053 8.355c.193-.195.193-.517 0-.711l-3.26-3.289c-.193-.195-.192-.514.002-.709l1.371-1.371c.194-.194.512-.193.706.001l5.335 5.369c.195.195.195.515 0 .708l-5.335 5.37c-.194.192-.512.193-.706.002l-1.371-1.371c-.194-.195-.195-.514-.002-.709l3.26-3.29z"/></svg>

After

Width:  |  Height:  |  Size: 347 B

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#FFFFFF" d="M14.648 12.788l-4.23-4.228c.525-.855.834-1.858.834-2.938 0-3.105-2.52-5.624-5.627-5.624-3.106.002-5.625 2.521-5.625 5.627 0 3.105 2.519 5.625 5.625 5.625 1.076 0 2.08-.309 2.936-.832l4.229 4.229c.194.195.515.195.707 0l1.151-1.146c.194-.2.194-.519 0-.713zm-13.35-7.163c0-2.39 1.938-4.327 4.327-4.327 2.391 0 4.328 1.937 4.328 4.327 0 2.391-1.936 4.327-4.328 4.327-2.39 0-4.327-1.936-4.327-4.327z"/></svg>

After

Width:  |  Height:  |  Size: 491 B

View File

@@ -0,0 +1,105 @@
<?php
namespace Drupal\admin_toolbar\Form;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class AdminToolbarSettingsForm. The config form for the admin_toolbar module.
*
* @package Drupal\admin_toolbar\Form
*/
class AdminToolbarSettingsForm extends ConfigFormBase {
/**
* The cache menu instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheMenu;
/**
* The menu link manager instance.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuLinkManager;
/**
* AdminToolbarSettingsForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory for the form.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
* A menu link manager instance.
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheMenu
* A cache menu instance.
*/
public function __construct(ConfigFactoryInterface $config_factory, MenuLinkManagerInterface $menuLinkManager, CacheBackendInterface $cacheMenu) {
parent::__construct($config_factory);
$this->cacheMenu = $cacheMenu;
$this->menuLinkManager = $menuLinkManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('plugin.manager.menu.link'),
$container->get('cache.menu')
);
}
/**
* {@inheritDoc}
*/
protected function getEditableConfigNames() {
return [
'admin_toolbar.settings',
];
}
/**
* {@inheritDoc}
*/
public function getFormId() {
return 'admin_toolbar_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('admin_toolbar.settings');
$depth_values = range(1, 9);
$form['menu_depth'] = [
'#type' => 'select',
'#title' => $this->t('Menu depth'),
'#description' => $this->t('Maximal depth of displayed menu.'),
'#default_value' => $config->get('menu_depth'),
'#options' => array_combine($depth_values, $depth_values),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('admin_toolbar.settings')
->set('menu_depth', $form_state->getValue('menu_depth'))
->save();
parent::submitForm($form, $form_state);
$this->cacheMenu->invalidateAll();
$this->menuLinkManager->rebuild();
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Drupal\admin_toolbar\Render\Element;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Security\TrustedCallbackInterface;
/**
* Render element element for AdminToolbar.
*
* @package Drupal\admin_toolbar\Render\Element
*/
class AdminToolbar implements TrustedCallbackInterface {
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['preRenderTray'];
}
/**
* Renders the toolbar's administration tray.
*
* This is a clone of core's toolbar_prerender_toolbar_administration_tray()
* function, which uses setMaxDepth(4) instead of setTopLevelOnly().
*
* @param array $build
* A renderable array.
*
* @return array
* The updated renderable array.
*
* @see toolbar_prerender_toolbar_administration_tray()
*/
public static function preRenderTray(array $build) {
$menu_tree = \Drupal::service('toolbar.menu_tree');
$parameters = new MenuTreeParameters();
$max_depth = \Drupal::config('admin_toolbar.settings')->get('menu_depth');
$parameters->setRoot('system.admin')->excludeRoot()->setMaxDepth($max_depth)->onlyEnabledLinks();
$tree = $menu_tree->load('admin', $parameters);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
['callable' => 'toolbar_tools_menu_navigation_links'],
];
$tree = $menu_tree->transform($tree, $manipulators);
$build['administration_menu'] = $menu_tree->build($tree);
return $build;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Drupal\Tests\admin_toolbar\Functional;
use Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest;
/**
* Tests the caching of the admin menu subtree items.
*
* @group admin_toolbar
*/
class AdminToolbarAdminMenuTest extends ToolbarAdminMenuTest {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'admin_toolbar',
];
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Drupal\Tests\admin_toolbar\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Test the existence of Admin Toolbar module.
*
* @group admin_toolbar
*/
class AdminToolbarAlterTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'toolbar',
'breakpoint',
'admin_toolbar',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A test user with permission to access the administrative toolbar.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create and log in an administrative user.
$this->adminUser = $this->drupalCreateUser([
'access toolbar',
'access administration pages',
]);
$this->drupalLogin($this->adminUser);
}
/**
* Tests for a the hover of sub menus.
*/
public function testAdminToolbar() {
// Assert that expanded links are present in the HTML.
$this->assertSession()->responseContains('class="toolbar-icon toolbar-icon-user-admin-index"');
}
}

View File

@@ -0,0 +1,254 @@
<?php
namespace Drupal\Tests\admin_toolbar\Functional;
use Drupal\media\Entity\MediaType;
use Drupal\system\Entity\Menu;
use Drupal\Tests\BrowserTestBase;
/**
* Tests Admin Toolbar tools functionality.
*
* @group admin_toolbar
*/
class AdminToolbarToolsSortTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'toolbar',
'breakpoint',
'admin_toolbar',
'admin_toolbar_tools',
'menu_ui',
'media',
'field_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A test user with permission to access the administrative toolbar.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Tests that menu updates on entity add/update/delete.
*/
public function testMenuUpdate() {
// Create and log in an administrative user.
$this->adminUser = $this->drupalCreateUser([
'access toolbar',
'access administration pages',
'administer site configuration',
'administer menu',
'access media overview',
'administer media',
'administer media fields',
'administer media form display',
'administer media display',
'administer media types',
]);
$this->drupalLogin($this->adminUser);
$menu = Menu::create([
'id' => 'armadillo',
'label' => 'Armadillo',
]);
$menu->save();
$this->container->get('plugin.manager.menu.link')->rebuild();
$this->drupalGet('/admin');
// Assert that special menu items are present in the HTML.
$this->assertSession()->responseContains('class="toolbar-icon toolbar-icon-admin-toolbar-tools-flush"');
// Assert that adding a media type adds it to the admin toolbar.
$chinchilla_media_type = MediaType::create([
'id' => 'chinchilla',
'label' => 'Chinchilla',
'source' => 'image',
]);
$chinchilla_media_type->save();
$this->drupalGet('/admin');
$this->assertMenuHasHref('/admin/structure/media/manage/chinchilla');
// Assert that adding a menu adds it to the admin toolbar.
$menu = Menu::create([
'id' => 'chupacabra',
'label' => 'Chupacabra',
]);
$menu->save();
$this->drupalGet('/admin');
$this->assertMenuHasHref('/admin/structure/menu/manage/chupacabra');
// Assert that deleting a menu removes it from the admin toolbar.
$this->assertMenuHasHref('/admin/structure/menu/manage/armadillo');
$menu = Menu::load('armadillo');
$menu->delete();
$this->drupalGet('/admin');
$this->assertMenuDoesNotHaveHref('/admin/structure/menu/manage/armadillo');
// Assert that deleting a content entity bundle removes it from admin menu.
$this->assertMenuHasHref('/admin/structure/media/manage/chinchilla');
$chinchilla_media_type = MediaType::load('chinchilla');
$chinchilla_media_type->delete();
$this->drupalGet('/admin');
$this->assertMenuDoesNotHaveHref('/admin/structure/media/manage/chinchilla');
}
/**
* Tests sorting of menus by label rather than machine name.
*/
public function testMenuSorting() {
// Create and log in an administrative user.
$this->adminUser = $this->drupalCreateUser([
'access toolbar',
'access administration pages',
'administer site configuration',
'administer menu',
]);
$menus = [
'aaa' => 'qqq',
'bbb' => 'ppp',
'ccc' => 'ooo',
'ddd' => 'nnn',
'eee' => 'mmm',
'fff' => 'lll',
'ggg' => 'kkk',
'hhh' => 'jjj',
'iii' => 'iii',
'jjj' => 'hhh',
'kkk' => 'ggg',
'lll' => 'fff',
'mmm' => 'eee',
'nnn' => 'ddd',
'ooo' => 'ccc',
'ppp' => 'bbb',
'qqq' => 'aaa',
];
foreach ($menus as $machine_name => $label) {
$menu = Menu::create([
'id' => $machine_name,
'label' => $label,
]);
$menu->save();
}
$this->drupalLogin($this->adminUser);
$this->container->get('plugin.manager.menu.link')->rebuild();
$this->drupalGet('/admin');
$results = $this->getSession()->getPage()->findAll('xpath', '//a[contains(@href, "/admin/structure/menu/manage")]');
$links = [];
foreach ($results as $result) {
$links[] = $result->getAttribute('href');
}
$expected = [
0 => '/admin/structure/menu/manage/qqq',
1 => '/admin/structure/menu/manage/qqq/add',
2 => '/admin/structure/menu/manage/qqq/delete',
3 => '/admin/structure/menu/manage/admin',
4 => '/admin/structure/menu/manage/admin/add',
5 => '/admin/structure/menu/manage/ppp',
6 => '/admin/structure/menu/manage/ppp/add',
7 => '/admin/structure/menu/manage/ppp/delete',
8 => '/admin/structure/menu/manage/ooo',
9 => '/admin/structure/menu/manage/ooo/add',
10 => '/admin/structure/menu/manage/ooo/delete',
11 => '/admin/structure/menu/manage/nnn',
12 => '/admin/structure/menu/manage/nnn/add',
13 => '/admin/structure/menu/manage/nnn/delete',
14 => '/admin/structure/menu/manage/mmm',
15 => '/admin/structure/menu/manage/mmm/add',
16 => '/admin/structure/menu/manage/mmm/delete',
17 => '/admin/structure/menu/manage/lll',
18 => '/admin/structure/menu/manage/lll/add',
19 => '/admin/structure/menu/manage/lll/delete',
20 => '/admin/structure/menu/manage/footer',
21 => '/admin/structure/menu/manage/footer/add',
22 => '/admin/structure/menu/manage/kkk',
23 => '/admin/structure/menu/manage/kkk/add',
24 => '/admin/structure/menu/manage/kkk/delete',
25 => '/admin/structure/menu/manage/jjj',
26 => '/admin/structure/menu/manage/jjj/add',
27 => '/admin/structure/menu/manage/jjj/delete',
28 => '/admin/structure/menu/manage/iii',
29 => '/admin/structure/menu/manage/iii/add',
30 => '/admin/structure/menu/manage/iii/delete',
31 => '/admin/structure/menu/manage/hhh',
32 => '/admin/structure/menu/manage/hhh/add',
33 => '/admin/structure/menu/manage/hhh/delete',
34 => '/admin/structure/menu/manage/ggg',
35 => '/admin/structure/menu/manage/ggg/add',
36 => '/admin/structure/menu/manage/ggg/delete',
37 => '/admin/structure/menu/manage/fff',
38 => '/admin/structure/menu/manage/fff/add',
39 => '/admin/structure/menu/manage/fff/delete',
40 => '/admin/structure/menu/manage/main',
41 => '/admin/structure/menu/manage/main/add',
42 => '/admin/structure/menu/manage/eee',
43 => '/admin/structure/menu/manage/eee/add',
44 => '/admin/structure/menu/manage/eee/delete',
45 => '/admin/structure/menu/manage/ddd',
46 => '/admin/structure/menu/manage/ddd/add',
47 => '/admin/structure/menu/manage/ddd/delete',
48 => '/admin/structure/menu/manage/ccc',
49 => '/admin/structure/menu/manage/ccc/add',
50 => '/admin/structure/menu/manage/ccc/delete',
51 => '/admin/structure/menu/manage/bbb',
52 => '/admin/structure/menu/manage/bbb/add',
53 => '/admin/structure/menu/manage/bbb/delete',
54 => '/admin/structure/menu/manage/aaa',
55 => '/admin/structure/menu/manage/aaa/add',
56 => '/admin/structure/menu/manage/aaa/delete',
];
foreach ($links as $key => $link) {
// Using assert contains because prefaces the urls with "/subdirectory".
$this->assertStringContainsString($expected[$key], $link);
}
}
/**
* Checks that there is a link with the specified url in the admin toolbar.
*
* @param string $url
* The url to assert exists in the admin menu.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
*/
protected function assertMenuHasHref($url) {
$this->assertSession()
->elementExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]');
}
/**
* Checks that there is no link with the specified url in the admin toolbar.
*
* @param string $url
* The url to assert exists in the admin menu.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
protected function assertMenuDoesNotHaveHref($url) {
$this->assertSession()
->elementNotExists('xpath', '//div[@id="toolbar-item-administration-tray"]//a[contains(@href, "' . $url . '")]');
}
}

View File

@@ -0,0 +1,54 @@
################
# DrupalCI GitLabCI template
#
# Gitlab-ci.yml to replicate DrupalCI testing for Contrib
#
# With thanks to:
# * The GitLab Acceleration Initiative participants
# * DrupalSpoons
################
################
# Guidelines
#
# This template is designed to give any Contrib maintainer everything they need to test, without requiring modification. It is also designed to keep up to date with Core Development automatically through the use of include files that can be centrally maintained.
#
# However, you can modify this template if you have additional needs for your project.
################
################
# Includes
#
# Additional configuration can be provided through includes.
# One advantage of include files is that if they are updated upstream, the changes affect all pipelines using that include.
#
# Includes can be overridden by re-declaring anything provided in an include, here in gitlab-ci.yml
# https://docs.gitlab.com/ee/ci/yaml/includes.html#override-included-configuration-values
################
include:
################
# DrupalCI includes:
# As long as you include this, any future includes added by the Drupal Association will be accessible to your pipelines automatically.
# View these include files at https://git.drupalcode.org/project/gitlab_templates/
################
- project: $_GITLAB_TEMPLATES_REPO
ref: $_GITLAB_TEMPLATES_REF
file:
- '/includes/include.drupalci.main.yml'
- '/includes/include.drupalci.variables.yml'
- '/includes/include.drupalci.workflows.yml'
################
# Pipeline configuration variables
#
# These are the variables provided to the Run Pipeline form that a user may want to override.
#
# Docs at https://git.drupalcode.org/project/gitlab_templates/-/blob/1.0.x/includes/include.drupalci.variables.yml
################
# variables:
# SKIP_ESLINT: '1'
variables:
_PHPUNIT_CONCURRENT: 1
OPT_IN_TEST_PREVIOUS_MINOR: 1
OPT_IN_TEST_PREVIOUS_MAJOR: 1

View File

@@ -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.

View File

@@ -0,0 +1,82 @@
PROJECT
-------------
https://www.drupal.org/project/contact_storage
INSTALLATION
-------------
1. Download and extract the module to your sites/all/modules/contrib folder.
2. Enable the module on the Drupal Modules page (admin/modules) or using :
$ drush en
If you want to be able to send messages in HTML format, the Swiftmailer module
is required. To install it, follow the same instructions as above, using the
following module :
https://www.drupal.org/project/swiftmailer
The module administration page is available at : /admin/structure/contact
The module settings page is available at : /admin/structure/contact/settings
Adding a new form can be done at : /admin/structure/contact/add
A listing of sent messages is available at : /admin/structure/contact/messages
INSTRUCTIONS TO ENABLE HTML
-------------
In order to be able to send messages in HTML format, once Swiftmailer module
has been installed and enabled, follow these steps :
1. Enable Mail System and select Swiftmailer as your default mail system.
In "Configuration" -> "Mail System", choose "Swiftmailer" under "Formatter"
and "Sender".
2. HTML should not be enforced and provided e-mail format should be respected.
In "Configuration" -> "Swift Mailer", in the "Messages" tab, select "Plain
Text" under "Message Format" and check the "Respect provided e-mail
format." option.
3. Enable sending messages in HTML format within Contact Storage.
In "Structure" -> "Contact forms", in the "Contact settings" tab, check the
"Send HTML" option.
4. Customize theming.
The Contact Storage module provides a default template,
"swiftmailer--contact.html.twig", in /templates directory. This template
can be changed to conform to your needs.
OVERVIEW
-------------
Contact Storage module will provide storage for Contact messages which are
fully-fledged entities in Drupal 8. This plus core contact module aim to
provide functionality equivalent to the base-features of Webform or Entity
Form. The goal is to firm up this functionality in contrib with view to move
into core in 8.1.x or later.
FEATURES
-------------
Message storage
Edit messages
Admin listing
Views integration
REQUIREMENTS
-------------
Core Contact Module and Swiftmailer module, if sending messages in HTML format
is desired.
CREDITS
-------------
Collaboration between the following developers :
larowlan
jibran
andypost
berdir
Supporting organizations:
PreviousNext (Development time)
MD Systems (Development time)

View File

@@ -0,0 +1,9 @@
{
"name": "drupal/contact_storage",
"description": "Provides storage and edit capability for contact messages.",
"type": "drupal-module",
"require": {
"drupal/token": "^1.6",
"drupal/core": "^9.1 || ^10 || ^11"
}
}

View File

@@ -0,0 +1,16 @@
name: 'Contact storage'
type: module
description: 'Provides storage and edit capability for contact messages.'
core_version_requirement: ^9.1 || ^10 || ^11
dependencies:
- drupal:contact
- drupal:views
- drupal:options
- drupal:filter
- token:token
configure: entity.contact_message.collection
# Information added by Drupal.org packaging script on 2024-06-22
version: '8.x-1.3+5-dev'
project: 'contact_storage'
datestamp: 1719055400

View File

@@ -0,0 +1,141 @@
<?php
/**
* @file
* Contains install and update hooks.
*/
use Drupal\Core\Url;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Implements hook_module_preinstall().
*/
function contact_storage_module_preinstall($module) {
if ($module !== 'contact_storage') {
return;
}
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$original_contact_message = $entity_definition_update_manager->getEntityType('contact_message');
$original_contact_form = \Drupal::entityTypeManager()->getDefinition('contact_form');
$entity_type_contact_message = clone $original_contact_message;
$entity_definition_update_manager->uninstallEntityType($original_contact_message);
// Update the entity type definition and make it use the default SQL storage.
// @see contact_storage_entity_type_alter()
$entity_types = [
'contact_message' => $entity_type_contact_message,
'contact_form' => $original_contact_form,
];
contact_storage_entity_type_alter($entity_types);
$entity_definition_update_manager->installEntityType($entity_types['contact_message']);
}
/**
* Make sure the fields are added.
*/
function contact_storage_update_8001() {
_contact_storage_ensure_fields();
}
/**
* Ensure fields are added.
*/
function _contact_storage_ensure_fields() {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
$field_manager = \Drupal::service('entity_field.manager');
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
foreach (['id', 'created', 'uid', 'ip_address'] as $field_name) {
$field_definition = $field_manager->getFieldStorageDefinitions('contact_message')[$field_name];
$entity_definition_update_manager->installFieldStorageDefinition($field_name, 'contact_message', 'contact_storage', $field_definition);
}
}
/**
* Save the bulk delete action to config.
*/
function contact_storage_update_8002() {
$entity_type_manager = \Drupal::entityTypeManager();
$module_handler = \Drupal::moduleHandler();
// Save the bulk delete action to config.
$config_install_path = $module_handler->getModule('contact_storage')->getPath() . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
// Create action if it doesn't exist.
$action_storage = $entity_type_manager->getStorage('action');
$action = $action_storage->load('message_delete_action');
if (!$action) {
$storage = new FileStorage($config_install_path);
$entity_type_manager
->getStorage('action')
->create($storage->read('system.action.contact_message_delete_action'))
->save();
}
}
/**
* Defines fields for the user id and ip address, for the contact messages.
*/
function contact_storage_update_8003() {
$storage_definition = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID.'))
->setSetting('target_type', 'contact_form')
->setDefaultValueCallback('contact_storage_contact_message_default_uid');
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('uid', 'contact_message', 'contact_storage', $storage_definition);
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('IP address'))
->setDescription(t('The IP address of the submitter.'))
->setDefaultValueCallback('contact_storage_contact_message_default_ip_address');
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('ip_address', 'contact_message', 'contact_storage', $storage_definition);
}
/**
* Updates the redirect paths to the core property redirect path.
*/
function contact_storage_update_8200() {
$config_factory = \Drupal::configFactory();
// Iterate on all text formats config entities.
foreach ($config_factory->listAll('contact.form.') as $name) {
if ($redirect_page = $config_factory->get($name)->get('third_party_settings.contact_storage.redirect_uri')) {
$config = $config_factory->getEditable($name);
$config->clear('third_party_settings.contact_storage.redirect_uri');
try {
$url = '/' . Url::fromUri($redirect_page)->getInternalPath();
}
catch (Exception $e) {
continue;
}
if (!$config->get('redirect')) {
$config->set('redirect', $url);
}
$config->save();
}
}
}
/**
* Enables the options module as it is now a dependency.
*/
function contact_storage_update_8201() {
\Drupal::service('module_installer')->install(['options']);
}
/**
* Fix the last installed definition for the 'contact_message' entity type.
*/
function contact_storage_update_8202() {
$entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('contact_message');
$keys = $entity_type->getKeys();
if (empty($keys['langcode'])) {
$keys['langcode'] = 'langcode';
$entity_type->set('entity_keys', $keys);
\Drupal::entityDefinitionUpdateManager()->updateEntityType($entity_type);
}
}

View File

@@ -0,0 +1,20 @@
entity.contact_message.forms:
title: 'Forms'
route_name: entity.contact_form.collection
base_route: entity.contact_form.collection
entity.contact_message.collection:
title: 'List'
route_name: entity.contact_message.collection
base_route: entity.contact_form.collection
entity.contact_form.clone_form:
route_name: entity.contact_form.clone_form
base_route: entity.contact_form.edit_form
title: Clone
weight: 20
contact_storage.settings:
title: 'Contact settings'
route_name: contact_storage.settings
base_route: entity.contact_form.collection

View File

@@ -0,0 +1,550 @@
<?php
/**
* @file
* Contains main module logic.
*/
use Drupal\contact\MessageForm;
use Drupal\contact_storage\ContactRouteProvider;
use Drupal\contact_storage\Form\ContactFormDisableForm;
use Drupal\contact_storage\Form\ContactFormEnableForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\contact\ContactFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\contact\Entity\ContactForm;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\views\Views;
/**
* Implements hook_help().
*/
function contact_storage_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.contact_storage':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The <a href="https://www.drupal.org/project/contact_storage">Contact Storage</a> module allows registered <em>users</em> on your site to see messages sent by <em>visitors</em> when using personal or site-wide forms. The Contact Storage module provides the ability to store the messages so registered users can later view them thru the site.') . '</p>';
$output .= '<p>' . t('The combination of <a href="contact">Contact</a> and Contact Storage modules allows for a general means to create, read, update and delete user generated data. For more information, see the <a href=":contact_storage">online documentation for the Contact Storage module</a>.', [':contact_storage' => 'https://www.drupal.org/node/2718407']) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Using the Contact messages page') . '</dt>';
$output .= '<dd>' . t('Registered users can access stored messages on the <a href=":url">Contact messages</a> page. This page can be manually accessed from the Administration menu path "Home >> Administration >> Structure >> Contact forms". The Contact Storage module places the "List" tab on the "Contact forms" page. The "Contact messages" page is displayed when the List tab is selected. View, edit and delete operations can be performed on individual messages from this listing of contact messages.', [':url' => Url::fromRoute('entity.contact_message.collection')->toString()]) . '</dd>';
$output .= '<dt>' . t('Configuring contact message pages') . '</dt>';
$output .= '<dd>' . t('The listing of Contact messages is a <a href=":url">Views</a> page. This page can be manually accessed from the Administration menu path "Home >> Administration >> Structure >> Views" like any other view. It has a VIEW NAME of "Contact messages". Registered users can perform Edit, Duplicate, Disable and Delete Operations from the Views listing page.', [':url' => Url::fromRoute('entity.view.collection')->toString()]) . '</dd>';
return $output;
}
}
/**
* Implements hook_form_FORM_ID_alter() for contact_form_form().
*/
function contact_storage_form_contact_form_form_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\contact\ContactFormInterface $contact_form */
$form_object = $form_state->getFormObject();
if (!in_array($form_object->getOperation(), ['edit', 'add'], TRUE)) {
// Only alter the edit and add forms.
return;
}
$contact_form = $form_object->getEntity();
$form['contact_storage_submit_text'] = [
'#type' => 'textfield',
'#title' => t('Submit button text'),
'#description' => t("Override the submit button's default <em>Send message</em> text."),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage', 'submit_text', t('Send message')),
];
$form['contact_storage_url_alias'] = [
'#type' => 'textfield',
'#title' => t('Add URL alias'),
'#description' => t('Optionally add an URL alias to the contact form.'),
];
if (!$contact_form->isNew()) {
$aliases = \Drupal::entityTypeManager()
->getStorage('path_alias')
->loadByProperties([
'path' => '/' . $contact_form->toUrl('canonical')->getInternalPath(),
]);
if ($aliases) {
/** @var \Drupal\path_alias\PathAliasInterface $alias */
$alias = reset($aliases);
$form_state->set('path_alias_id', $alias->id());
$form['contact_storage_url_alias']['#default_value'] = $alias->getAlias();
}
}
$form['contact_storage_disabled_form_message'] = [
'#type' => 'textfield',
'#title' => t('Default disabled contact form message'),
'#description' => t('Default message to display if the contact form is disabled.'),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage', 'disabled_form_message', t('This contact form has been disabled.')),
];
$form['contact_storage_preview'] = [
'#type' => 'checkbox',
'#title' => t('Allow preview'),
'#description' => t('Show the preview button?'),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage', 'show_preview', TRUE),
];
$form['contact_storage_maximum_submissions_user'] = [
'#type' => 'textfield',
'#title' => t('Maximum submissions'),
'#description' => t('The maximum number of times, per user, the form can be submitted (0 for unlimited).'),
'#default_value' => $contact_form->getThirdPartySetting('contact_storage', 'maximum_submissions_user', 0),
];
// Overrides the 'reply' field provided by Core with a formattable field. If
// html e-mails are disabled, enforce plain text format.
$form['reply']['#type'] = 'text_format';
if (!\Drupal::config('contact_storage.settings')->get('send_html')) {
$form['reply']['#allowed_formats'] = ['plain_text'];
}
else {
$form['reply']['#format'] = $contact_form->getThirdPartySetting('contact_storage', 'page_autoreply_format', 'plain_text');
// Explicitly set the allowed formats so that the fallback format is not
// removed. That allows to prevent any formatting and is the default option,
// without setting it, this option would go away after selecting something
// else.
$formats = filter_formats(\Drupal::currentUser());
$form['reply']['#allowed_formats'] = array_keys($formats);
}
$form['#entity_builders'][] = 'contact_storage_contact_form_form_builder';
$form['#validate'][] = 'contact_storage_contact_form_form_validate';
$form['actions']['submit']['#submit'][] = 'contact_storage_contact_form_form_submit';
}
/**
* Entity builder for the contact form edit form with third party options.
*
* @see contact_storage_test_form_contact_form_edit_form_alter()
*/
function contact_storage_contact_form_form_builder($entity_type, ContactFormInterface $contact_form, &$form, FormStateInterface $form_state) {
$contact_form->setThirdPartySetting('contact_storage', 'submit_text', $form_state->getValue('contact_storage_submit_text'));
$contact_form->setThirdPartySetting('contact_storage', 'show_preview', $form_state->getValue('contact_storage_preview'));
$contact_form->setThirdPartySetting('contact_storage', 'disabled_form_message', $form_state->getValue('contact_storage_disabled_form_message'));
$contact_form->setThirdPartySetting('contact_storage', 'maximum_submissions_user', $form_state->getValue('contact_storage_maximum_submissions_user'));
// Auto-reply value is handled by Core; 'reply' is expected to be a string.
$reply = $form_state->getValue('reply');
if (isset($reply['value'])) {
$form_state->setValue('reply', $reply['value']);
$contact_form->setThirdPartySetting('contact_storage', 'page_autoreply_format', $reply['format']);
}
}
/**
* Contact form's form validation handler.
*
* @param array $form
* An associative array containing the structure of the form.
*
* @param \Drupal\Core\Form\FormStateInterface $formState
* The current state of the form.
*/
function contact_storage_contact_form_form_validate(&$form, FormStateInterface &$formState) {
// Validates the url alias. It has to start with a slash.
if (!empty($formState->getValue('contact_storage_url_alias'))) {
if (strpos($formState->getValue('contact_storage_url_alias'), '/') !== 0) {
$formState->setError($form['contact_storage_url_alias'], 'The alias path has to start with a slash.');
}
}
}
/**
* Contact form's form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
*
* @param \Drupal\Core\Form\FormStateInterface $formState
* The current state of the form.
*/
function contact_storage_contact_form_form_submit(&$form, FormStateInterface &$formState) {
$alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
$entity = $formState->getFormObject()->getEntity();
if ($old_alias = $formState->get('path_alias_id')) {
$old_alias = $alias_storage->load($old_alias);
}
$new_alias = $formState->getValue('contact_storage_url_alias');
// If there isn't an alias, set a new one.
if (!$old_alias) {
if ($new_alias) {
$alias_storage->create([
'path' => '/' . $entity->toUrl('canonical')->getInternalPath(),
'alias' => $formState->getValue('contact_storage_url_alias'),
])->save();
}
}
else {
// Delete old alias if user erased it.
if ($old_alias && !$new_alias) {
$old_alias->delete();
}
// Only save a non-empty alias.
elseif ($new_alias) {
$old_alias
->setAlias($formState->getValue('contact_storage_url_alias'))
->save();
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for contact_form_form().
*/
function contact_storage_form_contact_message_form_alter(&$form, &$form_state, $form_id) {
/** @var \Drupal\Core\Entity\ContentEntityForm $form_object */
$form_object = $form_state->getFormObject();
/* @var \Drupal\contact\MessageInterface $contact_message */
$contact_message = $form_object->getEntity();
$contact_form = ContactForm::load($contact_message->bundle());
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_mode */
if ($form_object instanceof MessageForm) {
if ($submit_text = $contact_form->getThirdPartySetting('contact_storage', 'submit_text', FALSE)) {
$form['actions']['submit']['#value'] = $submit_text;
}
if (!$contact_form->getThirdPartySetting('contact_storage', 'show_preview', TRUE)) {
$form['actions']['preview']['#access'] = FALSE;
}
}
// Check if the current user has reached the form's maximum submission limit.
$maximum_submissions_user = $contact_form->getThirdPartySetting('contact_storage', 'maximum_submissions_user', 0);
if (($maximum_submissions_user !== 0) && contact_storage_maximum_submissions_user($contact_form) >= $maximum_submissions_user) {
// Sets the error message.
$form['maximum_submissions_error'] = [
'#type' => 'container',
'#markup' => t('You have reached the maximum submission limit of @limit for this form.', ['@limit' => $maximum_submissions_user]),
'#attributes' => [
'class' => ['messages', 'messages--error'],
],
'#weight' => -100,
];
// Remove the submit and preview buttons.
$form['actions']['submit']['#access'] = FALSE;
$form['actions']['preview']['#access'] = FALSE;
}
}
/**
* Implements hook_entity_base_field_info().
*/
function contact_storage_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'contact_message') {
$fields = [];
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Message ID'))
->setDescription(t('The message ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the message was created.'))
->setTranslatable(TRUE)
->setReadOnly(TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID.'))
->setSetting('target_type', 'contact_form')
->setDefaultValueCallback('contact_storage_contact_message_default_uid');
$fields['ip_address'] = BaseFieldDefinition::create('string')
->setLabel(t('IP address'))
->setDescription(t('The IP address of the submitter.'))
->setDefaultValueCallback('contact_storage_contact_message_default_ip_address');
return $fields;
}
}
/**
* Default value callback for the contact message uid field.
*
* @return int
* The user ID.
*/
function contact_storage_contact_message_default_uid() {
return \Drupal::currentUser()->id();
}
/**
* Default value callback for the contact message ip_address field.
*
* @return int
* The client IP address.
*/
function contact_storage_contact_message_default_ip_address() {
return \Drupal::request()->getClientIp();
}
/**
* Implements hook_entity_base_field_info_alter().
*/
function contact_storage_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'contact_message') {
// Start at min 3 because message default weight is 0.
$i = -3;
foreach (['name', 'mail', 'subject'] as $field_name) {
$fields[$field_name]->setDisplayConfigurable('view', TRUE);
$fields[$field_name]->setDisplayOptions('view', ['weight' => $i]);
$i++;
}
// Add a validation constraint to prevent form submission if the limit is
// reached.
$fields['message']->addConstraint('ConstactStorageMaximumSubmissions', []);
}
}
/**
* Implements hook_entity_operation_alter().
*/
function contact_storage_entity_operation_alter(array &$operations, EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'contact_message' && $entity->access('view')) {
$operations['view'] = [
'title' => t('View'),
'weight' => 0,
'url' => $entity->toUrl('canonical'),
];
}
if ($entity->getEntityTypeId() == 'contact_form' && $entity->access('update')) {
$operations['clone'] = [
'title' => t('Clone'),
'weight' => 10,
'url' => $entity->toUrl('clone-form'),
];
// Provide a link to view messages submitted form the form, if the view
// exists and if the user has access rights to it.
$view = Views::getView('contact_messages');
if ($view && $view->access('page_1')) {
$view->setDisplay('page_1');
$operations['view_messages'] = [
'title' => t('View messages'),
'url' => $view->getUrl()->setOption('query', ['form' => $entity->id()]),
];
}
}
}
/**
* Implements hook_entity_type_alter().
*/
function contact_storage_entity_type_alter(array &$entity_types) {
/* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
// Set the controller class for nodes to an alternate implementation of the
// Drupal\Core\Entity\EntityStorageInterface interface.
$entity_types['contact_message']->setStorageClass('\Drupal\Core\Entity\Sql\SqlContentEntityStorage');
$keys = $entity_types['contact_message']->getKeys();
$keys['id'] = 'id';
$keys['label'] = 'subject';
$entity_types['contact_message']->set('entity_keys', $keys);
$entity_types['contact_message']->set('base_table', 'contact_message');
// Add edit and delete forms.
$entity_types['contact_message']->setFormClass('edit', '\Drupal\contact_storage\MessageEditForm');
$entity_types['contact_message']->setFormClass('delete', '\Drupal\Core\Entity\ContentEntityDeleteForm');
$entity_types['contact_message']->setFormClass('delete-multiple-confirm', 'Drupal\Core\Entity\Form\DeleteMultipleForm');
// Add clone form for messages.
$entity_types['contact_form']->setFormClass('clone', '\Drupal\contact_storage\Form\ContactFormCloneForm');
// Allow edit/delete links in list builder.
$entity_types['contact_message']->setLinkTemplate('collection', '/admin/structure/contact/messages');
$entity_types['contact_message']->setLinkTemplate('canonical', '/admin/structure/contact/messages/{contact_message}');
$entity_types['contact_message']->setLinkTemplate('edit-form', '/admin/structure/contact/messages/{contact_message}/edit');
$entity_types['contact_message']->setLinkTemplate('delete-form', '/admin/structure/contact/messages/{contact_message}/delete');
$entity_types['contact_message']->setLinkTemplate('delete-multiple-form', '/admin/structure/contact/messages/delete');
// Add clone link for forms.
$entity_types['contact_form']->setLinkTemplate('clone-form', '/admin/structure/contact/manage/{contact_form}/clone');
// Define the entity route provider.
foreach (['contact_message', 'contact_form'] as $entity_type_id) {
$entity_types[$entity_type_id]->setHandlerClass('route_provider', [
'html' => ContactRouteProvider::class,
] + $entity_types[$entity_type_id]->getRouteProviderClasses());
}
// @todo Replace with access control handler when not enough.
$entity_types['contact_message']->set('admin_permission', 'administer contact forms');
// Integrate with Views.
$entity_types['contact_message']->setHandlerClass('views_data', '\Drupal\contact_storage\MessageViewsData');
$entity_types['contact_message']->setListBuilderClass('\Drupal\Core\Entity\EntityListBuilder');
$entity_types['contact_form']->setViewBuilderClass('\Drupal\contact_storage\ContactFormViewBuilder');
// If the body of the message should be sent as HTML.
if (\Drupal::config('contact_storage.settings')->get('send_html')) {
$entity_types['contact_message']->setViewBuilderClass('Drupal\contact_storage\ContactMessageViewBuilder');
}
$keys = $entity_types['contact_form']->getKeys();
$keys['status'] = 'status';
$entity_types['contact_form']->set('entity_keys', $keys);
// Handler classes and templates for the Enable and Disable options.
$entity_types['contact_form']
->setFormClass('disable', ContactFormDisableForm::class)
->setFormClass('enable', ContactFormEnableForm::class)
->setLinkTemplate('enable', '/admin/structure/contact/view/{contact_form}/enable')
->setLinkTemplate('disable', '/admin/structure/contact/view/{contact_form}/disable');
}
/**
* Implements hook_entity_extra_field_info().
*/
function contact_storage_entity_extra_field_info() {
$fields = [];
foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('contact_message')) as $bundle) {
$fields['contact_message'][$bundle]['form']['preview'] = [
'label' => t('Preview'),
'description' => t('Rendered preview'),
'weight' => 50,
];
}
return $fields;
}
/**
* Implements hook_mail_alter().
*/
function contact_storage_mail_alter(&$message) {
// Check that the message isn't a copy sent to the sender (page_copy).
if (($message['key'] == 'page_mail') && isset($message['params']['contact_message'])) {
$contact_message = $message['params']['contact_message'];
foreach ($contact_message->getFields() as $field) {
if ($field->getFieldDefinition()->getType() === 'contact_storage_options_email') {
// Add recipients to the message from the Option email field.
foreach ($field as $delta => $item) {
$label = $item->value;
// Obtain the email to add to the message, using the label.
$email = $item->getFieldDefinition()->getSetting('allowed_values')[$label]['emails'];
$message['to'] .= ',' . $email;
}
}
}
}
if ($message['module'] === 'contact' && isset($message['params']['contact_message'])) {
if (($message['key'] == 'page_autoreply')) {
$contact_form = $message['params']['contact_form'];
// Filters the auto-reply message using the chosen format and sets it.
if ($reply = $contact_form->getReply()) {
$filtered_reply = check_markup($contact_form->getReply(), $contact_form->getThirdPartySetting('contact_storage', 'page_autoreply_format', 'plain_text'));
$message['body'] = [$filtered_reply];
}
}
// Enforce that we are sending mails as html, if enabled, and tell
// Swiftmailer to generate a plain text version.
if (\Drupal::config('contact_storage.settings')->get('send_html')) {
$message['headers']['Content-Type'] = 'text/html';
$message['params']['convert'] = TRUE;
}
}
}
/**
* Implements hook_theme().
*/
function contact_storage_theme() {
return [
'swiftmailer__contact' => [
'variables' => [
'message' => [],
],
],
'contact_storage_disabled_form' => [
'template' => 'contact-storage-disabled-form',
'variables' => [
'contact_form' => NULL,
'disabled_form_message' => '',
],
],
];
}
/**
* Prepares variables for contact mail templates.
*
* @param array $variables
* An associative array containing:
* - message: An associative array containing the message array.
* - body: The processed body.
*/
function template_preprocess_swiftmailer__contact(&$variables) {
$variables['subject'] = $variables['message']['subject'];
$variables['body'] = $variables['message']['body'];
}
/**
* Implements hook_field_formatter_info_alter().
*/
function contact_storage_field_formatter_info_alter(&$info) {
// Let our options_email field type re-use the default list formatter.
$info['list_default']['field_types'][] = 'contact_storage_options_email';
}
/**
* Implements hook_field_widget_info_alter().
*/
function contact_storage_field_widget_info_alter(&$info) {
// Let our options_email field type re-use the default options widget.
$info['options_select']['field_types'][] = 'contact_storage_options_email';
}
/**
* Returns the number of times the current user has submitted the specified
* form.
*
* @param Drupal\contact\ContactFormInterface $contact_form
* The contact_form entity.
*
* @return int
* The number of times the current user has submitted the specified form.
*/
function contact_storage_maximum_submissions_user(ContactFormInterface $contact_form) {
$account = \Drupal::currentUser();
if ($account->isAnonymous()) {
// Anonymous user, limit per submission with the same IP address.
$ip_address = \Drupal::request()->getClientIp();
$query = \Drupal::entityQuery('contact_message')
->accessCheck()
->condition('contact_form', $contact_form->id())
->condition('ip_address', $ip_address)
->condition('uid', $account->id());
return count($query->execute());
}
else {
// Limit per submission with the same uid.
$query = \Drupal::entityQuery('contact_message')
->accessCheck()
->condition('contact_form', $contact_form->id())
->condition('uid', $account->id());
return count($query->execute());
}
}
/**
* Implements hook_ENTITY_TYPE_delete() for 'contact_form'.
*/
function contact_storage_contact_form_delete(EntityInterface $entity) {
$alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
// Delete all aliases with this contact form as a source.
$aliases = $alias_storage->loadByProperties([
'path' => '/' . $entity->toUrl('canonical')->getInternalPath(),
]);
$alias_storage->delete($aliases);
}
/**
* Implements hook_action_info_alter().
*/
function contact_storage_action_info_alter(array &$definitions) {
// For backwards compatibility, treat 'message_delete_action' as an alias of
// the 'entity:delete_action:contact_message' plugin provided by core.
$definitions['message_delete_action'] = $definitions['entity:delete_action:contact_message'];
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @file
* Post update functions for Contact Storage.
*/
use Drupal\system\Entity\Action;
/**
* Renames the "message_delete" action to avoid Message module conflicts.
*/
function contact_storage_post_update_rename_message_delete_action() {
$action = Action::load('message_delete_action');
if ($action) {
$action->set('id', 'contact_message_delete_action')
->setPlugin('entity:delete_action:contact_message');
$action->save();
}
}

View File

@@ -0,0 +1,31 @@
contact_storage.settings:
path: '/admin/structure/contact/settings'
defaults:
_form: '\Drupal\contact_storage\Form\ContactStorageSettingsForm'
_title: 'Contact settings'
requirements:
_permission: 'administer contact forms'
entity.contact_form.disable:
path: '/admin/structure/contact/manage/{contact_form}/disable'
defaults:
_entity_form: 'contact_form.disable'
_title: 'Disable contact form'
requirements:
_entity_access: 'contact_form.disable'
entity.contact_form.enable:
path: '/admin/structure/contact/manage/{contact_form}/enable'
defaults:
_entity_form: 'contact_form.enable'
_title: 'Enable contact form'
requirements:
_entity_access: 'contact_form.enable'
entity.contact.multiple_delete_confirm:
path: '/admin/structure/contact/messages/delete'
defaults:
_form: 'Drupal\Core\Entity\Form\DeleteMultipleForm'
entity_type_id: 'contact_message'
requirements:
_permission: 'administer contact forms'

View File

@@ -0,0 +1,9 @@
services:
contact_storage.settings_form_save:
class: \Drupal\contact_storage\EventSubscriber\ContactStorageSettingsFormSave
tags:
- { name: event_subscriber }
contact_storage.route_subscriber:
class: Drupal\contact_storage\Routing\RouteSubscriber
tags:
- { name: event_subscriber }

View File

@@ -0,0 +1,11 @@
# Configuration file for PHPStan static code checking, see https://phpstan.org.
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
parameters:
level: 2
paths:
- .
ignoreErrors:
# new static() is a best practice in Drupal, so we cannot fix that.
- "#^Unsafe usage of new static#"

View File

@@ -0,0 +1,159 @@
<?php
namespace Drupal\contact_storage;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityViewBuilderInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a contact form view builder.
*
* @see \Drupal\contact\Entity\ContactForm
*/
class ContactFormViewBuilder implements EntityViewBuilderInterface, EntityHandlerInterface {
/**
* The entity form builder.
*
* @var \Drupal\Core\Entity\EntityFormBuilderInterface
*/
protected $entityFormBuilder;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The contact settings config object.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The contact message storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $contactMessageStorage;
/**
* Constructs a new contact form view builder.
*
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
* The entity form builder service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\Core\Config\Config $config
* The contact settings config object.
* @param \Drupal\Core\Entity\EntityStorageInterface $contact_message_storage
* The contact message storage.
*/
public function __construct(EntityFormBuilderInterface $entity_form_builder, RendererInterface $renderer, Config $config, EntityStorageInterface $contact_message_storage) {
$this->entityFormBuilder = $entity_form_builder;
$this->renderer = $renderer;
$this->config = $config;
$this->contactMessageStorage = $contact_message_storage;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$container->get('entity.form_builder'),
$container->get('renderer'),
$container->get('config.factory')->get('contact.settings'),
$container->get('entity_type.manager')->getStorage('contact_message')
);
}
/**
* {@inheritdoc}
*/
public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
if ($entity->status()) {
$message = $this->contactMessageStorage->create([
'contact_form' => $entity->id(),
]);
$form = $this->entityFormBuilder->getForm($message);
$form['#title'] = $entity->label();
$form['#cache']['contexts'][] = 'user.permissions';
$this->renderer->addCacheableDependency($form, $this->config);
}
else {
// Form disabled, display a custom message using a template.
$form['disabled_form_error'] = [
'#theme' => 'contact_storage_disabled_form',
'#contact_form' => $entity,
'#redirect_uri' => $entity->getThirdPartySetting('contact_storage', 'redirect_uri', ''),
'#disabled_form_message' => $entity->getThirdPartySetting('contact_storage', 'disabled_form_message', t('This contact form has been disabled.')),
];
}
// Add required cacheability metadata from the contact form entity, so that
// changing it invalidates the cache.
$this->renderer->addCacheableDependency($form, $entity);
return $form;
}
/**
* {@inheritdoc}
*/
public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
$build = [];
foreach ($entities as $key => $entity) {
$build[$key] = $this->view($entity, $view_mode, $langcode);
}
return $build;
}
/**
* {@inheritdoc}
*/
public function resetCache(array $entities = NULL) {
// Intentionally empty.
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
// Intentionally empty.
}
/**
* {@inheritdoc}
*/
public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
throw new \LogicException();
}
/**
* {@inheritdoc}
*/
public function viewField(FieldItemListInterface $items, $display_options = []) {
throw new \LogicException();
}
/**
* {@inheritdoc}
*/
public function viewFieldItem(FieldItemInterface $item, $display_options = []) {
throw new \LogicException();
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Drupal\contact_storage;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
/**
* Customized contact message view that does not do HTML to plain conversion.
*
* Also relies on standard field formatters to build the message. Does not
* extend from MessageViewBuilder to avoid running that code.
*/
class ContactMessageViewBuilder extends EntityViewBuilder {
/**
* {@inheritdoc}
*/
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
$build = parent::getBuildDefaults($entity, $view_mode);
// The message fields are individually rendered into email templates, so
// the entity has no template itself.
// @todo Remove this when providing a template in
// https://www.drupal.org/node/2722501.
unset($build['#theme']);
return $build;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Drupal\contact_storage;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider;
use Symfony\Component\Routing\Route;
/**
* Provides routes for contact messages and contact forms.
*/
class ContactRouteProvider extends DefaultHtmlRouteProvider {
/**
* {@inheritdoc}
*/
public function getRoutes(EntityTypeInterface $entity_type) {
$route_collection = parent::getRoutes($entity_type);
if ($entity_type->hasLinkTemplate('collection')) {
$route = (new Route($entity_type->getLinkTemplate('collection')))
->addDefaults([
'_entity_list' => 'contact_message',
'_title' => 'Contact messages',
])
->addRequirements([
'_permission' => 'administer contact forms',
]);
$route_collection->add('entity.' . $entity_type->id() . '.collection', $route);
}
if ($entity_type->hasLinkTemplate('clone-form')) {
$route = (new Route($entity_type->getLinkTemplate('clone-form')))
->addDefaults([
'_entity_form' => 'contact_form.clone',
'_title' => 'Clone form',
])
->addRequirements([
'_entity_access' => 'contact_form.clone',
]);
$route_collection->add('entity.' . $entity_type->id() . '.clone_form', $route);
}
return $route_collection;
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Drupal\contact_storage\Controller;
use Drupal\contact\ContactFormInterface;
use Drupal\contact\Controller\ContactController;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Controller routines for contact storage routes.
*/
class ContactStorageController extends ContactController {
/**
* {@inheritdoc}
*/
public function contactSitePage(ContactFormInterface $contact_form = NULL) {
// This is an override of ContactController::contactSitePage() that uses
// the entity view builder. This is necessary to show the close message for
// disabled forms.
$config = $this->config('contact.settings');
// Use the default form if no form has been passed.
$manager = $this->entityTypeManager();
if (empty($contact_form)) {
$contact_form = $manager
->getStorage('contact_form')
->load($config->get('default_form'));
// If there are no forms, do not display the form.
if (empty($contact_form)) {
if ($this->currentUser()->hasPermission('administer contact forms')) {
$this->messenger()->addError($this->t('The contact form has not been configured. <a href=":add">Add one or more forms</a> .', [
':add' => Url::fromRoute('contact.form_add')->toString(),
]));
return [];
}
else {
throw new NotFoundHttpException();
}
}
}
$view_builder = $manager->getViewBuilder('contact_form');
return $view_builder->view($contact_form, 'full', $contact_form->language());
}
/**
* Route title callback.
*
* @param \Drupal\contact\ContactFormInterface $contact_form
* The contact form.
*
* @return string
* The title of the contact form.
*/
public function contactFormTitle(ContactFormInterface $contact_form) {
return $contact_form->label();
}
/**
* Edit route title callback.
*
* @param \Drupal\contact\ContactFormInterface $contact_form
* The contact form.
*
* @return string
* The title of the contact form.
*/
public function contactEditFormTitle(ContactFormInterface $contact_form) {
return $this->t('Edit @label', ['@label' => $contact_form->label()]);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Drupal\contact_storage\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Invalidates the entity type definition cache when the settings are changed.
*/
class ContactStorageSettingsFormSave implements EventSubscriberInterface {
/**
* Invalidates the entity type definition cache whenever settings are changed.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The Event to process.
*/
public function onSave(ConfigCrudEvent $event) {
if ($event->getConfig()->getName() === 'contact_storage.settings') {
\Drupal::entityTypeManager()->clearCachedDefinitions();
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events[ConfigEvents::SAVE][] = ['onSave'];
return $events;
}
}

View File

@@ -0,0 +1,153 @@
<?php
namespace Drupal\contact_storage\Form;
use Drupal\contact\ContactFormEditForm;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Drupal\field\Entity\FieldConfig;
use Egulias\EmailValidator\EmailValidator;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a class for cloning a contact form.
*/
class ContactFormCloneForm extends ContactFormEditForm {
/**
* Entity Field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $fieldManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('email.validator'),
$container->get('path.validator'),
$container->get('entity_field.manager')
);
}
/**
* Constructs a new ContactFormCloneForm object.
*
* @param \Egulias\EmailValidator\EmailValidator $email_validator
* Email validator.
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator
* The path validator.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager
* Entity field manager.
*/
public function __construct(EmailValidator $email_validator, PathValidatorInterface $path_validator, EntityFieldManagerInterface $field_manager) {
parent::__construct($email_validator, $path_validator);
$this->fieldManager = $field_manager;
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
// Add #process and #after_build callbacks.
$form['#process'][] = '::processForm';
$form['#after_build'][] = '::afterBuild';
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => '',
'#description' => $this->t("Example: 'website feedback' or 'product information'."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => '',
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#machine_name' => [
'exists' => '\Drupal\contact\Entity\ContactForm::load',
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\contact\ContactFormInterface $contact_form */
$contact_form = $this->entity;
// Get the original ID.
$original_id = $contact_form->getOriginalId();
$new_id = $contact_form->id();
// Create the new form.
$contact_form = $contact_form->createDuplicate();
$contact_form->set('id', $new_id);
$contact_form->save();
// Clone configurable fields.
foreach ($this->fieldManager->getFieldDefinitions('contact_message', $original_id) as $field) {
if ($field instanceof BaseFieldDefinition) {
continue;
}
if ($this->moduleHandler->moduleExists('field')) {
if ($config = $field->getConfig($original_id)) {
$new_config = FieldConfig::create([
'bundle' => $contact_form->id(),
'uuid' => NULL,
] + $config->toArray());
$new_config->save();
}
}
}
// Clone the entity form display.
$display = EntityFormDisplay::load('contact_message.' . $original_id . '.default');
if ($display) {
EntityFormDisplay::create([
'bundle' => $contact_form->id(),
'uuid' => NULL,
] + $display->toArray())->save();
}
// Clone the entity view display.
$display = EntityViewDisplay::load('contact_message.' . $original_id . '.default');
if ($display) {
EntityViewDisplay::create([
'bundle' => $contact_form->id(),
'uuid' => NULL,
] + $display->toArray())->save();
}
// Redirect and show messge.
$form_state->setRedirect('entity.contact_form.edit_form', ['contact_form' => $contact_form->id()]);
$edit_link = $this->entity->toLink($this->t('Edit'))->toString();
$this->messenger()->addStatus($this->t('Contact form %label has been added.', ['%label' => $contact_form->label()]));
$this->logger('contact')->notice('Contact form %label has been added.', ['%label' => $contact_form->label(), 'link' => $edit_link]);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Clone');
return $actions;
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Drupal\contact_storage\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Provides the contact form disable form.
*/
class ContactFormDisableForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to disable the contact form %form?', ['%form' => $this->entity->label()]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.contact_form.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Disable');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Disabled contact forms are not displayed. This action can be undone from the contact forms administration page.');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['contact_storage_disabled_form_message'] = [
'#type' => 'textfield',
'#title' => t('Default disabled contact form message'),
'#description' => t('Default message to display if the contact form is disabled. It will be saved when clicking "Disable".'),
'#default_value' => $this->getEntity()->getThirdPartySetting('contact_storage', 'disabled_form_message', $this->t('This contact form has been disabled.')),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save the default disabled form message.
$this->entity->setThirdPartySetting('contact_storage', 'disabled_form_message', $form_state->getValue('contact_storage_disabled_form_message'));
$this->entity->disable()->save();
$this->messenger()->addStatus($this->t('Disabled contact form %form.', ['%form' => $this->entity->label()]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Drupal\contact_storage\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Provides the contact form enable form.
*/
class ContactFormEnableForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to enable the contact form %form?', ['%form' => $this->entity->label()]);
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('entity.contact_form.collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Enable');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('Enabling the contact form will make it visible. This action can be undone from the contact forms administration page.');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->enable()->save();
$this->messenger()->addStatus($this->t('Enabled contact form %form.', ['%form' => $this->entity->label()]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Drupal\contact_storage\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Defines a class for contact_storage's settings form.
*/
class ContactStorageSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'contact_storage_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'contact_storage.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('contact_storage.settings');
// Global setting.
$form['send_html'] = [
'#type' => 'checkbox',
'#title' => t('Send HTML'),
'#description' => t('Whether the mails should be sent as HTML. A module like <a href="https://www.drupal.org/project/swiftmailer">Swiftmailer</a> is needed for this feature. This has only been tested with the Swiftmailer module, other modules might not work out of the box and will not use the provided default template.'),
'#default_value' => $config->get('send_html'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('contact_storage.settings')
->set('send_html', $form_state->getValue('send_html'))
->save();
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Drupal\contact_storage;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for contact message edit forms.
*/
class MessageEditForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\contact\MessageInterface $message */
$message = $this->entity;
$form = parent::form($form, $form_state);
$form['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Author name'),
'#maxlength' => 255,
'#default_value' => $message->getSenderName(),
];
$form['mail'] = [
'#type' => 'email',
'#title' => $this->t('Sender email address'),
'#default_value' => $message->getSenderMail(),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity->save();
$this->logger('contact')->notice('The contact message %subject has been updated.', [
'%subject' => $this->entity->getSubject(),
'link' => $this->getEntity()->toLink($this->t('Edit'), 'edit-form')->toString(),
]);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Drupal\contact_storage;
use Drupal\views\EntityViewsData;
/**
* Provides data to integrate messages with Views.
*/
class MessageViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$data['contact_message']['contact_form_label'] = [
'title' => $this->t('Form'),
'help' => $this->t('The label of the associated form.'),
'real field' => 'contact_form',
'field' => [
'id' => 'contact_form',
],
];
$data['contact_message']['message_bulk_form'] = [
'title' => $this->t('Message operations bulk form'),
'help' => $this->t('Add a form element that lets you run operations on multiple messages.'),
'field' => [
'id' => 'message_bulk_form',
],
];
return $data;
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace Drupal\contact_storage\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Session\AccountInterface;
/**
* Plugin to add the Option email item custom field type.
*
* @FieldType(
* id = "contact_storage_options_email",
* label = @Translation("Options email"),
* description = @Translation("Stores e-mail recipients for the provided options."),
* default_widget = "options_select",
* default_formatter = "list_default"
* )
*/
class OptionsEmailItem extends OptionsListItemBase {
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Text value'))
->addConstraint('Length', ['max' => 255])
->setRequired(TRUE);
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 255,
],
],
'indexes' => [
'value' => ['value'],
],
];
}
/**
* {@inheritdoc}
*/
protected function allowedValuesDescription() {
$description = '<p>' . $this->t('The possible values this field can contain. Enter one value per line, in the format key|label|emails.');
$description .= '<br/>' . $this->t('"key" is the message that is added to the body of the message.');
$description .= '<br/>' . $this->t('"label" is the value displayed in the dropdown menu on the contact form.');
$description .= '<br/>' . $this->t('"emails" are the email addresses to add to the recipients list (each separated by a comma).');
$description .= '</p>';
$description .= '<p>' . $this->t('Allowed HTML tags in labels: @tags', ['@tags' => FieldFilteredMarkup::displayAllowedTags()]) . '</p>';
return $description;
}
/**
* {@inheritdoc}
*/
protected static function extractAllowedValues($string, $has_data) {
$values = [];
// Explode the content of the text area per line.
$list = explode("\n", $string);
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
foreach ($list as $text) {
// Explode each line around the pipe symbol.
$elements = explode('|', $text);
// Expects 3 elements (value, label and emails).
if (count($elements) == 3) {
// Sanitize the email address.
if (\Drupal::service('email.validator')->isValid($elements[2])) {
$values[$elements[0]] = [
'value' => $elements[1],
'emails' => $elements[2],
];
continue;
}
}
// Failed at some point. Returns NULL to display an error.
return;
}
if (empty($values)) {
return;
}
return $values;
}
/**
* {@inheritdoc}
*/
protected static function simplifyAllowedValues(array $structured_values) {
$values = [];
foreach ($structured_values as $value) {
$values[$value['key']] = [
'value' => $value['value'],
'emails' => $value['emails'],
];
}
return $values;
}
/**
* {@inheritdoc}
*/
protected static function structureAllowedValues(array $values) {
$structured_values = [];
foreach ($values as $key => $value) {
$structured_values[] = [
'key' => $key,
'value' => $value['value'],
'emails' => $value['emails'],
];
}
return $structured_values;
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
$allowed_options_keys = [];
$allowed_options = $this->getOptionsAllowedValues();
// Each option is currently an array containing the value and emails, keyed
// with the key defined by the user. Remove the array to keep only the key.
foreach ($allowed_options as $key => $option) {
$allowed_options_keys[$key] = $key;
}
return $allowed_options_keys;
}
/**
* Returns the array of allowed values for the Options email field.
*
* @return array
* An array of allowed values entered by the user, for the Options email
* field.
*/
protected function getOptionsAllowedValues() {
return options_allowed_values($this->getFieldDefinition()->getFieldStorageDefinition(), $this->getEntity());
}
/**
* {@inheritdoc}
*/
protected function allowedValuesString($values) {
$lines = [];
foreach ($values as $key => $value) {
$lines[] = $key . '|' . $value['value'] . '|' . $value['emails'];
}
return implode("\n", $lines);
}
}

View File

@@ -0,0 +1,340 @@
<?php
namespace Drupal\contact_storage\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\OptGroup;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\OptionsProviderInterface;
/**
* Plugin base class inherited by the options field types.
*
* This is copy from ListItemBase with textarea for allowed values.
* https://www.drupal.org/i/2521800
*/
abstract class OptionsListItemBase extends FieldItemBase implements OptionsProviderInterface {
/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
return [
'allowed_values' => [],
'allowed_values_function' => '',
] + parent::defaultStorageSettings();
}
/**
* {@inheritdoc}
*/
public function getPossibleValues(AccountInterface $account = NULL) {
// Flatten options firstly, because Possible Options may contain group
// arrays.
$flatten_options = OptGroup::flattenOptions($this->getPossibleOptions($account));
return array_keys($flatten_options);
}
/**
* {@inheritdoc}
*/
public function getPossibleOptions(AccountInterface $account = NULL) {
return $this->getSettableOptions($account);
}
/**
* {@inheritdoc}
*/
public function getSettableValues(AccountInterface $account = NULL) {
// Flatten options firstly, because Settable Options may contain group
// arrays.
$flatten_options = OptGroup::flattenOptions($this->getSettableOptions($account));
return array_keys($flatten_options);
}
/**
* {@inheritdoc}
*/
public function getSettableOptions(AccountInterface $account = NULL) {
$allowed_options = options_allowed_values($this->getFieldDefinition()->getFieldStorageDefinition(), $this->getEntity());
return $allowed_options;
}
/**
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$allowed_options = options_allowed_values($field_definition->getFieldStorageDefinition());
if (empty($allowed_options)) {
$values['value'] = NULL;
return $values;
}
$values['value'] = array_rand($allowed_options);
return $values;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
return empty($this->value) && (string) $this->value !== '0';
}
/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$allowed_values = $this->getSetting('allowed_values');
$allowed_values_function = $this->getSetting('allowed_values_function');
$element['allowed_values'] = [
'#type' => 'textarea',
'#title' => $this->t('Allowed values list'),
'#default_value' => $this->allowedValuesString($allowed_values),
'#rows' => 10,
'#access' => empty($allowed_values_function),
'#element_validate' => [[static::class, 'validateAllowedValues']],
'#field_has_data' => $has_data,
'#field_name' => $this->getFieldDefinition()->getName(),
'#entity_type' => $this->getEntity()->getEntityTypeId(),
'#allowed_values' => $allowed_values,
'#required' => TRUE,
];
$element['allowed_values']['#description'] = $this->allowedValuesDescription();
$element['allowed_values_function'] = [
'#type' => 'item',
'#title' => $this->t('Allowed values list'),
'#markup' => $this->t('The value of this field is being determined by the %function function and may not be changed.', ['%function' => $allowed_values_function]),
'#access' => !empty($allowed_values_function),
'#value' => $allowed_values_function,
];
return $element;
}
/**
* Provides the field type specific allowed values form element #description.
*
* @return string
* The field type allowed values form specific description.
*/
abstract protected function allowedValuesDescription();
/**
* #element_validate callback for options field allowed values.
*
* @param $element
* An associative array containing the properties and children of the
* generic form element.
* @param $form_state
* The current state of the form for the form this element belongs to.
*
* @see \Drupal\Core\Render\Element\FormElement::processPattern()
*/
public static function validateAllowedValues($element, FormStateInterface $form_state) {
$values = static::extractAllowedValues($element['#value'], $element['#field_has_data']);
if (!is_array($values)) {
$form_state->setError($element, new TranslatableMarkup('Allowed values list: invalid input.'));
}
else {
// Check that keys are valid for the field type.
foreach ($values as $key => $value) {
if ($error = static::validateAllowedValue($key)) {
$form_state->setError($element, $error);
break;
}
}
// Prevent removing values currently in use.
if ($element['#field_has_data']) {
$lost_keys = array_keys(array_diff_key($element['#allowed_values'], $values));
if (_options_values_in_use($element['#entity_type'], $element['#field_name'], $lost_keys)) {
$form_state->setError($element, new TranslatableMarkup('Allowed values list: some values are being removed while currently in use.'));
}
}
$form_state->setValueForElement($element, $values);
}
}
/**
* Extracts the allowed values array from the allowed_values element.
*
* @param string $string
* The raw string to extract values from.
* @param bool $has_data
* The current field already has data inserted or not.
*
* @return array|null
* The array of extracted key/value pairs, or NULL if the string is invalid.
*
* @see \Drupal\options\Plugin\Field\FieldType\ListItemBase::allowedValuesString()
*/
protected static function extractAllowedValues($string, $has_data) {
$values = [];
$list = explode("\n", $string);
$list = array_map('trim', $list);
$list = array_filter($list, 'strlen');
$generated_keys = $explicit_keys = FALSE;
foreach ($list as $position => $text) {
// Check for an explicit key.
$matches = [];
if (preg_match('/(.*)\|(.*)/', $text, $matches)) {
// Trim key and value to avoid unwanted spaces issues.
$key = trim($matches[1]);
$value = trim($matches[2]);
$explicit_keys = TRUE;
}
// Otherwise see if we can use the value as the key.
elseif (!static::validateAllowedValue($text)) {
$key = $value = $text;
$explicit_keys = TRUE;
}
// Otherwise see if we can generate a key from the position.
elseif (!$has_data) {
$key = (string) $position;
$value = $text;
$generated_keys = TRUE;
}
else {
return;
}
$values[$key] = $value;
}
// We generate keys only if the list contains no explicit key at all.
if ($explicit_keys && $generated_keys) {
return;
}
return $values;
}
/**
* Checks whether a candidate allowed value is valid.
*
* @param string $option
* The option value entered by the user.
*
* @return string
* The error message if the specified value is invalid, NULL otherwise.
*/
protected static function validateAllowedValue($option) {}
/**
* Generates a string representation of an array of 'allowed values'.
*
* This string format is suitable for edition in a textarea.
*
* @param array $values
* An array of values, where array keys are values and array values are
* labels.
*
* @return string
* The string representation of the $values array:
* - Values are separated by a carriage return.
* - Each value is in the format "value|label" or "value".
*/
protected function allowedValuesString($values) {
$lines = [];
foreach ($values as $key => $value) {
$lines[] = "$key|$value";
}
return implode("\n", $lines);
}
/**
* {@inheritdoc}
*/
public static function storageSettingsToConfigData(array $settings) {
if (isset($settings['allowed_values'])) {
$settings['allowed_values'] = static::structureAllowedValues($settings['allowed_values']);
}
return $settings;
}
/**
* {@inheritdoc}
*/
public static function storageSettingsFromConfigData(array $settings) {
if (isset($settings['allowed_values'])) {
$settings['allowed_values'] = static::simplifyAllowedValues($settings['allowed_values']);
}
return $settings;
}
/**
* Simplifies allowed values to a key-value array from the structured array.
*
* @param array $structured_values
* Array of items with a 'value' and 'label' key each for the allowed
* values.
*
* @return array
* Allowed values were the array key is the 'value' value, the value is
* the 'label' value.
*
* @see \Drupal\options\Plugin\Field\FieldType\ListItemBase::structureAllowedValues()
*/
protected static function simplifyAllowedValues(array $structured_values) {
$values = [];
foreach ($structured_values as $item) {
if (is_array($item['label'])) {
// Nested elements are embedded in the label.
$item['label'] = static::simplifyAllowedValues($item['label']);
}
$values[$item['value']] = $item['label'];
}
return $values;
}
/**
* Creates a structured array of allowed values from a key-value array.
*
* @param array $values
* Allowed values were the array key is the 'value' value, the value is
* the 'label' value.
*
* @return array
* Array of items with a 'value' and 'label' key each for the allowed
* values.
*
* @see \Drupal\options\Plugin\Field\FieldType\ListItemBase::simplifyAllowedValues()
*/
protected static function structureAllowedValues(array $values) {
$structured_values = [];
foreach ($values as $value => $label) {
if (is_array($label)) {
$label = static::structureAllowedValues($label);
}
$structured_values[] = [
'value' => static::castAllowedValue($value),
'label' => $label,
];
}
return $structured_values;
}
/**
* Converts a value to the correct type.
*
* @param mixed $value
* The value to cast.
*
* @return mixed
* The casted value.
*/
protected static function castAllowedValue($value) {
return $value;
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Drupal\contact_storage\Plugin\Mail;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Mail\MailInterface;
/**
* A mail sending implementation that captures sent messages to a variable.
*
* This class is for running tests or for development and does not convert HTML
* to plaintext.
*
* @Mail(
* id = "test_contact_storage_html_mail",
* label = @Translation("HTML test mailer"),
* )
*/
class HTMLTestingMailSystem implements MailInterface {
/**
* Implements MailSystemInterface::format().
*/
public function format(array $message) {
// Join the body array into one string.
$message['body'] = implode("\n\n", $message['body']);
// Wrap the mail body for sending.
$message['body'] = MailFormatHelper::wrapMail($message['body']);
return $message;
}
/**
* Implements MailSystemInterface::mail().
*/
public function mail(array $message) {
$captured_emails = \Drupal::state()->get('system.test_mail_collector') ?: [];
$captured_emails[] = $message;
\Drupal::state()->set('system.test_mail_collector', $captured_emails);
return TRUE;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Drupal\contact_storage\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Verify that the form has not been submitted more times that the limit.
*
* @Constraint(
* id = "ConstactStorageMaximumSubmissions",
* label = @Translation("Maximum submission limit", context = "Validation"),
* )
*/
class ConstactStorageMaximumSubmissionsConstraint extends Constraint {
/**
* Message shown when the maximum submission limit has been reached.
*
* @var string
*/
public $limitReached = 'You have reached the maximum submission limit of @limit for this form.';
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Drupal\contact_storage\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the maximum submission limit constraint.
*/
class ConstactStorageMaximumSubmissionsConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($entity, Constraint $constraint) {
// Check if the current user has reached the form's maximum submission limit.
$contact_form = $entity->getParent()->get('contact_form')->referencedEntities()[0];
$maximum_submissions_user = $contact_form->getThirdPartySetting('contact_storage', 'maximum_submissions_user', 0);
if (($maximum_submissions_user !== 0) && contact_storage_maximum_submissions_user($contact_form) >= $maximum_submissions_user) {
// Limit reached; can't submit the form.
$this->context->addViolation($constraint->limitReached, ['@limit' => $maximum_submissions_user]);
}
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace Drupal\contact_storage\Plugin\views\field;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Field handler to provide the label of a contact form.
*
* @ingroup views_field_handlers
*
* @ViewsField("contact_form")
*/
class ContactForm extends FieldPluginBase {
/**
* The storage controller for contact forms.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $formStorage;
/**
* Constructs a ContactForm Views field object.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigEntityStorageInterface $form_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formStorage = $form_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager')->getStorage('contact_form'));
}
/**
* Render form label.
*/
protected function renderName($form_id, $values) {
if ($form_id !== NULL && $form_id !== '') {
$type = $this->formStorage->load($form_id);
return $type ? $this->sanitizeValue($type->label()) : '';
}
return $this->sanitizeValue($form_id);
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
return $this->renderName($value, $values);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Drupal\contact_storage\Plugin\views\field;
use Drupal\views\Plugin\views\field\BulkForm;
/**
* Defines a contact message operations bulk form element.
*
* @ViewsField("message_bulk_form")
*/
class MessageBulkForm extends BulkForm {
/**
* {@inheritdoc}
*/
protected function emptySelectedMessage() {
return $this->t('No message selected.');
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Drupal\contact_storage\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
use \Drupal\contact_storage\Controller\ContactStorageController;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
// Change the contact_form controller.
if ($route = $collection->get('entity.contact_form.canonical')) {
$route->setDefault('_controller', ContactStorageController::class . '::contactSitePage');
$route->setDefault('_title_callback', ContactStorageController::class . '::contactFormTitle');
}
if ($route = $collection->get('entity.contact_form.edit_form')) {
$route->setDefault('_title_callback', ContactStorageController::class . '::contactEditFormTitle');
}
}
}

View File

@@ -0,0 +1,28 @@
{#
/**
* @file
* The template file for a disabled contact form.
*
* Template used to display a message when a contact form is disabled. The
* provided example displays a standard error message showing the message
* defined when creating or disabling the form, for the Bartik theme.
*
* Available variables:
* - contact_message: The contact message entity. Some useful methods available
* are :
* - contact_form.getRecipients will return the list of recipients.
* - contact_form.label will return the label of the contact form.
* See \Drupal\contact\Entity\ContactForm for a full list of public
* properties and methods for the contact form object. Other available
* variables :
* - redirect_uri : The redirect URI of the contact form, an empty string if
* not defined.
* - disabled_form_message : The disabled form message, configured when
* creating or disabling the form. Bu default "This contact form has been
* disabled.".
*/
#}
<div class="messages messages--error">
{{ disabled_form_message }}
</div>

View File

@@ -0,0 +1,54 @@
{#
/**
* @file
* The template file for contact_storage e-mails.
*
* The table / HTML structure is taken from swiftmailer's default template file
* for e-mails, only the CSS has been added.
*/
#}
<style type="text/css">
table tr td {
font-family: Arial;
font-size: 12px;
}
.form-item, .field--label-inline, .field--label-above {
margin-top: 10px;
}
.label, label {
font-weight: bold;
}
label {
display: block;
}
.field--label-inline .label {
float:left; /*LTR*/
margin-right: 0.5em; /*LTR*/
}
.field--label-inline .label::after {
content: ':';
}
.clearfix {
clear: both;
}
</style>
<div>
<table width="800px" cellpadding="0" cellspacing="0">
<tr>
<td>
<div style="padding: 0px 0px 0px 0px;">
{{ body }}
</div>
</td>
</tr>
</table>
</div>

View File

@@ -0,0 +1,12 @@
name: 'Contact test views'
type: module
description: 'Provides default views for views contact tests.'
package: Testing
dependencies:
- contact_storage:contact_storage
- drupal:language
# Information added by Drupal.org packaging script on 2024-06-22
version: '8.x-1.3+5-dev'
project: 'contact_storage'
datestamp: 1719055400

Some files were not shown because too many files have changed in this diff Show More