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

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

View File

@@ -0,0 +1,187 @@
langcode: en
status: true
dependencies:
module:
- contact
- contact_storage
id: test_contact_message_bulk_form
label: ''
module: views
description: ''
tag: ''
base_table: contact_message
base_field: id
display:
default:
display_plugin: default
id: default
display_title: Master
position: null
display_options:
style:
type: table
row:
type: fields
fields:
message_bulk_form:
id: message_bulk_form
table: contact_message
field: message_bulk_form
relationship: none
group_type: group
admin_label: ''
label: 'Message operations bulk form'
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
action_title: 'With selection'
include_exclude: exclude
selected_actions: { }
entity_type: contact_message
plugin_id: message_bulk_form
subject:
table: contact_message
field: subject
id: subject
entity_type: null
entity_field: subject
plugin_id: field
relationship: none
group_type: group
admin_label: ''
label: Subject
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings: { }
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
filters: { }
sorts:
id:
id: id
table: contact_message
field: id
relationship: none
group_type: group
admin_label: ''
order: ASC
exposed: false
expose:
label: ''
entity_type: contact_message
entity_field: id
plugin_id: standard
title: ''
header: { }
footer: { }
empty: { }
relationships: { }
arguments: { }
display_extenders: { }
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
tags: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: test-contact-message-bulk-form
cache_metadata:
max-age: 0
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
tags: { }

View File

@@ -0,0 +1,83 @@
<?php
namespace Drupal\Tests\contact_storage\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Tests\ViewTestData;
/**
* Tests a contact message bulk form.
*
* @group contact_storage
* @see \Drupal\contact_storage\Plugin\views\field\MessageBulkForm
*/
class BulkFormTest extends ContactStorageTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Modules to be enabled.
*
* @var array
*/
protected static $modules = [
'contact_storage',
'contact_test_views',
'language',
];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_contact_message_bulk_form'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::Setup();
// Create and login administrative user.
$admin_user = $this->drupalCreateUser([
'administer contact forms',
]);
$this->drupalLogin($admin_user);
// Create first valid contact form.
$mail = 'simpletest@example.com';
$this->addContactForm('test_id', 'test_label', $mail, TRUE);
$this->assertSession()->pageTextContains('Contact form test_label has been added.');
$this->drupalLogout();
// Ensure that anonymous can submit site-wide contact form.
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ['access site-wide contact form']);
$this->drupalGet('contact');
$this->assertSession()->pageTextContains('Your email address');
// Submit contact form few times.
for ($i = 1; $i <= 5; $i++) {
$this->submitContact($this->randomMachineName(), $mail, $this->randomMachineName(), 'test_id', $this->randomMachineName());
$this->assertSession()->pageTextContains('Your message has been sent.');
}
}
/**
* Test multiple deletion.
*/
public function testBulkDeletion() {
$this->drupalGet('contact');
ViewTestData::createTestViews(get_class($this), ['contact_test_views']);
// Check the operations are accessible to the administer permission user.
$this->drupalLogin($this->drupalCreateUser(['administer contact forms']));
$this->drupalGet('test-contact-message-bulk-form');
// There are 2 options: "- Select -", "Delete contact message".
$elements = $this->xpath('//select[@id="edit-action"]//option[@value="contact_message_delete_action"]');
$this->assertSame(count($elements), 1, 'All contact message operations are found.');
$this->drupalGet('test-contact-message-bulk-form');
$this->submitForm([], 'Apply to selected items');
$this->assertSession()->pageTextContains('No message selected.');
}
}

View File

@@ -0,0 +1,133 @@
<?php
namespace Drupal\Tests\contact_storage\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Tests\BrowserTestBase;
/**
* Tests personal contact form functionality.
*
* @group contact
*/
class ContactStoragePersonalTest extends BrowserTestBase {
use AssertMailTrait {
getMails as drupalGetMails;
}
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['contact', 'contact_storage', 'dblog'];
/**
* A user with some administrative permissions.
*
* @var \Drupal\user\UserInterface
*/
private $adminUser;
/**
* A user with permission to view profiles and access user contact forms.
*
* @var \Drupal\user\UserInterface
*/
private $webUser;
/**
* A user without any permissions.
*
* @var \Drupal\user\UserInterface
*/
private $contactUser;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
protected function setUp(): void {
parent::setUp();
// Create an admin user.
$this->adminUser = $this->drupalCreateUser(['administer contact forms', 'administer users', 'administer account settings', 'access site reports']);
// Create some normal users with their contact forms enabled by default.
$this->config('contact.settings')->set('user_default_enabled', TRUE)->save();
$this->webUser = $this->drupalCreateUser(['access user profiles', 'access user contact forms']);
$this->contactUser = $this->drupalCreateUser();
}
/**
* Tests that mails for contact messages are correctly sent.
*/
public function testSendPersonalContactMessage() {
// Ensure that the web user's email needs escaping.
$mail = $this->webUser->getAccountName() . '&escaped@example.com';
$this->webUser->setEmail($mail)->save();
$this->drupalLogin($this->webUser);
$this->drupalGet('user/' . $this->contactUser->id() . '/contact');
$this->assertSession()->assertEscaped($mail);
$message = $this->submitPersonalContact($this->contactUser);
$mails = $this->drupalGetMails();
$this->assertEquals(1, count($mails));
$mail = $mails[0];
$this->assertEquals($mail['to'], $this->contactUser->getEmail());
$this->assertEquals($mail['from'], $this->config('system.site')->get('mail'));
$this->assertEquals($mail['reply-to'], $this->webUser->getEmail());
$this->assertEquals($mail['key'], 'user_mail');
$variables = [
'@site-name' => $this->config('system.site')->get('name'),
'@subject' => $message['subject[0][value]'],
'@recipient-name' => $this->contactUser->getDisplayName(),
];
$subject = PlainTextOutput::renderFromHtml(t('[@site-name] @subject', $variables));
$this->assertEquals($mail['subject'], $subject, 'Subject is in sent message.');
$this->assertTrue(strpos($mail['body'], 'Hello ' . $variables['@recipient-name']) !== FALSE, 'Recipient name is in sent message.');
$this->assertTrue(strpos($mail['body'], $this->webUser->getDisplayName()) !== FALSE, 'Sender name is in sent message.');
$this->assertTrue(strpos($mail['body'], $message['message[0][value]']) !== FALSE, 'Message body is in sent message.');
// Check there was no problems raised during sending.
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
// Verify that the correct watchdog message has been logged.
$this->drupalGet('/admin/reports/dblog');
$placeholders = [
'@sender_name' => $this->webUser->getAccountName(),
'@sender_email' => $this->webUser->getEmail(),
'@recipient_name' => $this->contactUser->getAccountName(),
];
$this->assertSession()->responseContains(new FormattableMarkup('@sender_name (@sender_email) sent @recipient_name an email.', $placeholders));
// Ensure an unescaped version of the email does not exist anywhere.
$this->assertSession()->responseNotContains($this->webUser->getEmail());
}
/**
* Fills out a user's personal contact form and submits it.
*
* @param \Drupal\Core\Session\AccountInterface $account
* A user object of the user being contacted.
* @param array $message
* (optional) An array with the form fields being used. Defaults to an empty
* array.
*
* @return array
* An array with the form fields being used.
*/
protected function submitPersonalContact(AccountInterface $account, array $message = []) {
$message += [
'subject[0][value]' => $this->randomMachineName(16),
'message[0][value]' => $this->randomMachineName(64),
];
$this->drupalGet('user/' . $account->id() . '/contact');
$this->submitForm($message, 'Send message');
return $message;
}
}

View File

@@ -0,0 +1,519 @@
<?php
namespace Drupal\Tests\contact_storage\Functional;
use Drupal\contact\Entity\ContactForm;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
/**
* Tests storing contact messages and viewing them through UI.
*
* @group contact_storage
*/
class ContactStorageTest extends ContactStorageTestBase {
use FieldUiTestTrait;
use AssertMailTrait {
getMails as drupalGetMails;
}
use PathAliasTestTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* An administrative user with permission to administer contact forms.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'text',
'block',
'contact',
'language',
'field_ui',
'contact_test',
'contact_storage',
'filter',
];
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('system_breadcrumb_block');
$this->drupalPlaceBlock('local_actions_block');
$this->drupalPlaceBlock('page_title_block');
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
]);
$full_html_format->save();
// Create and login administrative user.
$this->adminUser = $this->drupalCreateUser([
'access site-wide contact form',
'administer contact forms',
'administer users',
'administer account settings',
'administer contact_message fields',
'administer contact_message form display',
'administer contact_message display',
'use text format full_html',
]);
$this->drupalLogin($this->adminUser);
}
/**
* Tests contact messages submitted through contact form.
*/
public function testContactStorage() {
// Create first valid contact form.
$mail = 'simpletest@example.com';
$this->addContactForm('test_id', 'test_label', $mail, TRUE);
$this->assertSession()->pageTextContains('Contact form test_label has been added.');
// Ensure that new contact form can be cloned.
$this->cloneContactForm('test_id', 'test_id_cloned');
$original_form = ContactForm::load('test_id');
$cloned_form = ContactForm::load('test_id_cloned');
$this->assertNotEquals($original_form->uuid(), $cloned_form->uuid());
// Ensure that anonymous can submit site-wide contact form.
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ['access site-wide contact form']);
$this->drupalLogout();
$this->drupalGet('contact');
$this->assertSession()->pageTextContains('Your email address');
$this->assertSession()->pageTextNotContains('Form');
$this->submitContact('Test_name', $mail, 'Test_subject', 'test_id', 'Test_message');
$this->assertSession()->pageTextContains('Your message has been sent.');
// Verify that only 1 message has been sent (by default, the "Send a copy
// to yourself" option is disabled.
$captured_emails = $this->drupalGetMails();
$this->assertTrue(count($captured_emails) === 1);
// Login as admin.
$this->drupalLogin($this->adminUser);
// Verify that the global setting stating whether e-mails should be sent in
// HTML format is false by default.
$this->assertFalse(\Drupal::config('contact_storage.settings')->get('send_html'));
// Verify that this first e-mail was sent in plain text format.
$captured_emails = $this->drupalGetMails();
$this->assertTrue(strpos($captured_emails[0]['headers']['Content-Type'], 'text/plain') !== FALSE);
// Go to the settings form and enable sending messages in HTML format.
$this->drupalGet('/admin/structure/contact/settings');
$enable_html = [
'send_html' => TRUE,
];
$this->submitForm($enable_html, 'Save configuration');
// Check that the form POST was successful.
$this->assertSession()->pageTextContains('The configuration options have been saved.');
// Check that the global setting is properly updated.
$this->assertTrue(\Drupal::config('contact_storage.settings')->get('send_html'));
$display_fields = [
"The sender's name",
"The sender's email",
"Subject",
];
// Check that the page title is correct.
$this->drupalGet('contact/test_id');
$this->assertTrue(!empty($this->cssSelect('h1:contains(test_label)')));
$this->assertSession()->titleEquals('test_label | Drupal');
// Check that the configuration edit page title is correct.
$this->drupalGet('admin/structure/contact/manage/test_id');
$this->assertTrue(!empty($this->cssSelect('h1:contains(test_label)')));
$this->assertSession()->titleEquals('Edit test_label | Drupal');
// Check that name, subject and mail are configurable on display.
$this->drupalGet('admin/structure/contact/manage/test_id/display');
foreach ($display_fields as $label) {
$this->assertSession()->pageTextContains($label);
}
// Check that preview is configurable on form display.
$this->drupalGet('admin/structure/contact/manage/test_id/form-display');
$this->assertSession()->pageTextContains('Preview');
// Check the message list overview.
$this->drupalGet('admin/structure/contact/messages');
$rows = $this->xpath('//tbody/tr');
// Make sure only 1 message is available.
$this->assertEquals(count($rows), 1);
// Some fields should be present.
$this->assertSession()->pageTextContains('Test_subject');
$this->assertSession()->pageTextContains('Test_name');
$this->assertSession()->pageTextContains('test_label');
// Click the view link and make sure name, subject and email are displayed
// by default.
$this->clickLink('View');
foreach ($display_fields as $label) {
$this->assertSession()->pageTextContains($label);
}
// Make sure the stored message is correct.
$this->drupalGet('admin/structure/contact/messages');
$this->clickLink('Edit');
$this->assertSession()->fieldValueEquals('edit-name', 'Test_name');
$this->assertSession()->fieldValueEquals('edit-mail', $mail);
$this->assertSession()->fieldValueEquals('edit-subject-0-value', 'Test_subject');
$this->assertSession()->fieldValueEquals('edit-message-0-value', 'Test_message');
// Submit should redirect back to listing.
$this->submitForm([], 'Save');
$this->assertSession()->addressEquals('admin/structure/contact/messages');
// Delete the message.
$this->clickLink('Delete');
$this->submitForm([], 'Delete');
$this->assertSession()->responseContains((string) t('The @entity-type %label has been deleted.', [
// See \Drupal\Core\Entity\EntityDeleteFormTrait::getDeletionMessage().
'@entity-type' => 'contact message',
'%label' => 'Test_subject',
]));
// Make sure no messages are available.
$this->assertSession()->pageTextContains('There is no Contact message yet.');
// Fill the "Submit button text" field and assert the form can still be
// submitted.
$edit = [
'contact_storage_submit_text' => 'Submit the form',
'contact_storage_preview' => FALSE,
];
$this->drupalGet('admin/structure/contact/manage/test_id');
$this->submitForm($edit, 'Save');
$edit = [
'subject[0][value]' => 'Test subject',
'message[0][value]' => 'Test message',
];
$this->drupalGet('contact');
$element = $this->cssSelect('#edit-preview');
// Preview button is hidden.
$this->assertTrue(empty($element));
$this->submitForm($edit, 'Submit the form');
$this->assertSession()->pageTextContains('Your message has been sent.');
// Add an Options email item field to the form.
// There is a problem with allowed value. I was not able to create field
// manually on D10.
$settings = ['settings[allowed_values]' => "test_key1|test_label1|simpletest1@example.com\ntest_key2|test_label2|simpletest2@example.com"];
$this->fieldUIAddNewField('admin/structure/contact/manage/test_id', 'category', 'Category', 'contact_storage_options_email', $settings);
// Verify that the new field shows up correctly on the form.
$this->drupalGet('contact');
$this->assertSession()->pageTextContains('Category');
$this->assertSession()->optionExists('edit-field-category', '_none');
$this->assertSession()->optionExists('edit-field-category', 'test_key1');
$this->assertSession()->optionExists('edit-field-category', 'test_key2');
// Send a message using the Options email item field and enable the "Send a
// copy to yourself" option.
$captured_emails = $this->drupalGetMails();
$emails_count_before = count($captured_emails);
$edit = [
'subject[0][value]' => 'Test subject',
'message[0][value]' => 'Test message',
'field_category' => 'test_key2',
'copy' => 'checked',
];
$this->submitForm($edit, 'Submit the form');
$this->assertSession()->pageTextContains('Your message has been sent.');
// Check that 2 messages were sent and that the body of the last
// message contains the added message.
$captured_emails = $this->drupalGetMails();
$emails_count_after = count($captured_emails);
$this->assertTrue($emails_count_after === ($emails_count_before + 2));
$this->assertMailString('body', 'test_key2', 2);
// The last message is the one sent as a copy, the one before it is the
// original. Check that the original contains the added recipients and that
// the copied one is only sent to the sender.
$logged_in_user_email = $this->loggedInUser->getEmail();
$this->assertTrue($captured_emails[$emails_count_after - 2]['to'] == "$mail,simpletest2@example.com");
$this->assertTrue($captured_emails[$emails_count_after - 1]['to'] == $logged_in_user_email);
// Test clone functionality - add field to existing form.
$this->fieldUIAddNewField('admin/structure/contact/manage/test_id', 'text_field', 'Text field', 'text');
// Then clone it.
$this->cloneContactForm('test_id', 'test_id_2');
$edit = [
'subject[0][value]' => 'Test subject',
'message[0][value]' => 'Test message',
];
// The added field should be on the cloned form too.
$edit['field_text_field[0][value]'] = 'Some text';
$this->drupalGet('contact/test_id_2');
$this->submitForm($edit, 'Submit the form');
$form = ContactForm::load('test_id_2');
$this->assertNotEmpty($form->uuid());
// Try changing the options email label, field default value and setting it
// to required.
$this->drupalGet('/admin/structure/contact/manage/test_id/fields');
$this->clickLink('Edit');
$this->submitForm([
'label' => 'Category-2',
'required' => TRUE,
'default_value_input[field_category]' => 'test_key1',
], 'Save settings');
// Verify that the changes are visible into the contact form.
$this->drupalGet('contact');
$this->assertSession()->pageTextContains('Category-2');
$this->assertSession()->optionExists('edit-field-category', 'test_key1');
$this->assertSession()->optionExists('edit-field-category', 'test_key2');
$this->assertNotEmpty($this->xpath('//select[@id="edit-field-category" and @required="required"]//option[@value="test_key1"]'));
// Verify that the 'View messages' link exists for the 2 forms and that it
// links to the correct view.
$this->drupalGet('/admin/structure/contact');
$this->assertSession()->linkByHrefExists('/admin/structure/contact/messages?form=test_id');
$this->assertSession()->linkByHrefExists('/admin/structure/contact/messages?form=test_id_2');
// Create a new contact form and assert that the disable link exists for
// each forms.
$this->addContactForm('test_disable_id', 'test_disable_label', 'simpletest@example.com', FALSE);
$this->drupalGet('/admin/structure/contact');
$contact_form_count = count(ContactForm::loadMultiple());
// In D10 stark theme there are no li.disable, li.enable elements.
$this->assertEquals($contact_form_count, count($this->cssSelect('li a[href*="/disable"]:contains(Disable)')));
$this->drupalGet('/admin/structure/contact/manage/test_disable_id/disable');
// Disable the form and assert that there is 1 less "Disable" button and 1
// "Enable" button.
$this->submitForm([], 'Disable');
$this->assertSession()->pageTextContains('Disabled contact form test_disable_label.');
$this->drupalGet('/admin/structure/contact');
$this->assertEquals(($contact_form_count - 1), count($this->cssSelect('li a[href*="/disable"]:contains(Disable)')));
$this->assertEquals(1, count($this->cssSelect('li a[href*="/enable"]:contains(Enable)')));
// Assert that the disabled form has no input or text area and the message.
$this->drupalGet('contact/test_disable_id');
$this->assertEquals(0, count($this->cssSelect('input')));
$this->assertEquals(0, count($this->cssSelect('textarea')));
$this->assertSession()->pageTextContains('This contact form has been disabled.');
$this->drupalGet('/admin/structure/contact/manage/test_disable_id/enable');
// Try to re-enable the form and assert that it can be accessed.
$this->submitForm([], 'Enable');
$this->assertSession()->pageTextContains('Enabled contact form test_disable_label.');
$this->drupalGet('contact/test_disable_id');
$this->assertSession()->pageTextNotContains('This contact form has been disabled.');
// Create a new contact form with a custom disabled message, disable it and
// assert that the message displayed is correct.
$this->addContactForm('test_disable_id_2', 'test_disable_label_2', 'simpletest@example.com', FALSE, ['contact_storage_disabled_form_message' => 'custom disabled message']);
$this->drupalGet('/admin/structure/contact/manage/test_disable_id_2/disable');
$this->submitForm([], 'Disable');
$this->assertSession()->pageTextContains('Disabled contact form test_disable_label_2.');
$this->drupalGet('contact/test_disable_id_2');
$this->assertSession()->pageTextContains('custom disabled message');
}
/**
* Tests the url alias creation feature.
*/
public function testUrlAlias() {
// Add a second language to make sure aliases work with any language.
$language = ConfigurableLanguage::createFromLangcode('de');
$language->save();
// Set the second language as default.
$this->config('system.site')->set('default_langcode', $language->getId())->save();
$this->rebuildContainer();
$mail = 'simpletest@example.com';
// Test for alias without slash.
$this->addContactForm('form_alias_1', 'contactForm', $mail, FALSE, ['contact_storage_url_alias' => 'form51']);
$this->assertSession()->pageTextContains('The alias path has to start with a slash.');
$this->drupalGet('form51');
$this->assertSession()->statusCodeEquals(404);
// Test for correct alias. Verify that we land on the correct contact form.
$this->addContactForm('form_alias_2', 'contactForm', $mail, FALSE, ['contact_storage_url_alias' => '/form51']);
$this->assertSession()->pageTextContains('Contact form contactForm has been added.');
$this->drupalGet('form51');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('contactForm');
$this->assertSession()->buttonExists('Send message');
$this->drupalGet('admin/structure/contact/manage/form_alias_2');
// Edit the contact form without changing anything. Verify that the existing
// alias continues to work.
$this->submitForm([], 'Save');
$this->assertSession()->pageTextContains('Contact form contactForm has been updated.');
$this->drupalGet('form51');
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet('admin/structure/contact/manage/form_alias_2');
// Edit the contact form by changing the alias. Verify that the new alias
// is generated and the old one removed.
$this->submitForm(['contact_storage_url_alias' => '/form52'], 'Save');
$this->assertSession()->pageTextContains('Contact form contactForm has been updated.');
$this->drupalGet('form51');
$this->assertSession()->statusCodeEquals(404);
$this->drupalGet('form52');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('contactForm');
$this->drupalGet('admin/structure/contact/manage/form_alias_2');
// Edit the contact form by removing the alias. Verify that is is deleted.
$this->submitForm(['contact_storage_url_alias' => ''], 'Save');
$this->assertSession()->pageTextContains('Contact form contactForm has been updated.');
$this->drupalGet('form52');
$this->assertSession()->statusCodeEquals(404);
$this->drupalGet('admin/structure/contact/manage/form_alias_2');
// Add an alias back and delete the contact form. Verify that the alias is
// deleted along with the contact form.
$this->submitForm(['contact_storage_url_alias' => '/form52'], 'Save');
$this->assertSession()->pageTextContains('Contact form contactForm has been updated.');
$this->drupalGet('form52');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('contactForm');
$this->drupalGet('admin/structure/contact/manage/form_alias_2/delete');
$this->submitForm([], 'Delete');
$alias = $this->loadPathAliasByConditions([
'path' => '/contact/form_alias_2',
]);
$this->assertNull($alias);
}
public function testMaximumSubmissionLimit() {
// Create a new contact form with a maximum submission limit of 2.
$this->addContactForm('test_id_3', 'test_label', 'simpletest@example.com', FALSE, ['contact_storage_maximum_submissions_user' => 2]);
$this->assertSession()->pageTextContains('Contact form test_label has been added.');
// Sends 2 messages with "Send yourself a copy" option activated, shouldn't
// reach the limit even if 2 messages are sent twice.
$this->drupalGet('contact/test_id_3');
$edit = [
'subject[0][value]' => 'Test subject',
'message[0][value]' => 'Test message',
'copy' => 'checked',
];
$this->submitForm($edit, 'Send message');
$this->assertSession()->pageTextContains('Your message has been sent.');
$this->drupalGet('contact/test_id_3');
$this->submitForm($edit, 'Send message');
$this->assertSession()->pageTextContains('Your message has been sent.');
// Try accessing the form after the limit has been reached.
$this->drupalGet('contact/test_id_3');
$this->assertSession()->pageTextContains('You have reached the maximum submission limit of 2 for this form.');
}
/**
* Tests the Auto-reply field.
*/
public function testAutoReplyField() {
// Create a new contact form with an auto-reply.
$this->addContactForm('test_auto_reply_id_1', 'test_auto_reply_label_1', 'simpletest@example.com', TRUE, ['reply[value]' => "auto_reply_1\nsecond_line"]);
$this->assertSession()->pageTextContains('Contact form test_auto_reply_label_1 has been added.');
// Verify that the auto-reply shows up in the field and only offers
// one format (plain text), since html e-mails are disabled.
$this->drupalGet('admin/structure/contact/manage/test_auto_reply_id_1');
$this->assertNotEmpty($this->xpath('//textarea[@id="edit-reply-value" and text()=:text]', [':text' => "auto_reply_1\nsecond_line"]));
$this->assertEmpty($this->xpath('//select[@name="reply[format]"]'));
$this->drupalGet('contact');
$edit = [
'subject[0][value]' => 'Test subject',
'message[0][value]' => 'Test message',
];
$this->drupalGet('contact');
$this->submitForm($edit, 'Send message');
$this->assertSession()->pageTextContains('Your message has been sent.');
$captured_emails = $this->drupalGetMails();
// Checks that the last captured email is the auto-reply, has a correct
// body and is in html format.
$this->assertEquals(end($captured_emails)['key'], 'page_autoreply');
$this->assertStringContainsString("auto_reply_1\nsecond_line", end($captured_emails)['body']);
$this->assertStringContainsString('text/plain', end($captured_emails)['headers']['Content-Type']);
$this->drupalGet('/admin/structure/contact/settings');
// Enable sending messages in html format and verify that the available
// formats correctly show up on the contact form edit page.
$this->submitForm(['send_html' => TRUE], 'Save configuration');
$this->drupalGet('admin/structure/contact/manage/test_auto_reply_id_1');
$this->assertNotEmpty($this->xpath('//select[@name="reply[format]"]//option[@value="plain_text" and @selected="selected"]'));
$this->assertNotEmpty($this->xpath('//select[@name="reply[format]"]//option[@value="full_html"]'));
// Use custom testing mail system to support HTML mails.
$mail_config = $this->config('system.mail');
$mail_config->set('interface.default', 'test_contact_storage_html_mail');
$mail_config->save();
// Test sending a HTML mail.
$this->drupalGet('contact');
$edit = [
'subject[0][value]' => 'Test subject',
'message[0][value]' => 'Test message',
];
$this->drupalGet('contact');
$this->submitForm($edit, 'Send message');
$this->assertSession()->pageTextContains('Your message has been sent.');
$captured_emails = $this->drupalGetMails();
$this->assertEquals('page_autoreply', end($captured_emails)['key']);
// 10.1 - "auto_reply_1<br />\nsecond_line"
// 10.2 - "auto_reply_1<br>\nsecond_line"
$body = end($captured_emails)['body'];
$this->assertTrue(strpos($body, "auto_reply_1<br") !== FALSE);
$this->assertTrue(strpos($body, ">\nsecond_line") !== FALSE);
$this->assertEquals(end($captured_emails)['headers']['Content-Type'], 'text/html');
$this->drupalGet('admin/structure/contact/manage/test_auto_reply_id_1');
// Select full html format (not selected by default) and verify that it is
// properly set.
$this->submitForm(['reply[format]' => 'full_html'], 'Save');
$this->drupalGet('admin/structure/contact/manage/test_auto_reply_id_1');
$this->assertNotEmpty($this->xpath('//select[@name="reply[format]"]//option[@value="full_html" and @selected="selected"]'));
}
/**
* Clone form.
*
* @param string $original_form_id
* The original form machine name.
* @param string $clone_form_id
* The new form machine name.
*/
protected function cloneContactForm($original_form_id, $clone_form_id) {
$this->drupalGet("admin/structure/contact/manage/$original_form_id/clone");
$this->submitForm([
'id' => $clone_form_id,
'label' => 'Cloned',
], 'Clone');
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Drupal\Tests\contact_storage\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Defines a base-class for contact-storage tests.
*/
abstract class ContactStorageTestBase extends BrowserTestBase {
/**
* Adds a form.
*
* @param string $id
* The form machine name.
* @param string $label
* The form label.
* @param string $recipients
* The list of recipient email addresses.
* @param bool $selected
* A Boolean indicating whether the form should be selected by default.
* @param array $third_party_settings
* Array of third party settings to be added to the posted form data.
* @param string $message
* The message that will be displayed to a user upon completing the contact
* form.
*/
public function addContactForm($id, $label, $recipients, $selected, $third_party_settings = [], $message = 'Your message has been sent.') {
$this->drupalGet('admin/structure/contact/add');
$edit = [];
$edit['label'] = $label;
$edit['id'] = $id;
// 8.2.x added the message field, which is by default empty. Conditionally
// submit it if the field can be found.
$xpath = '//textarea[@name=:value]|//input[@name=:value]|//select[@name=:value]';
if ($this->xpath($this->assertSession()->buildXPathQuery($xpath, [':value' => 'message']))) {
$edit['message'] = $message;
}
$edit['recipients'] = $recipients;
$edit['selected'] = ($selected ? TRUE : FALSE);
$edit += $third_party_settings;
$this->submitForm($edit, 'Save');
}
/**
* Submits the contact form.
*
* @param string $name
* The name of the sender.
* @param string $mail
* The email address of the sender.
* @param string $subject
* The subject of the message.
* @param string $id
* The form ID of the message.
* @param string $message
* The message body.
*/
public function submitContact($name, $mail, $subject, $id, $message) {
$edit = [];
$edit['name'] = $name;
$edit['mail'] = $mail;
$edit['subject[0][value]'] = $subject;
$edit['message[0][value]'] = $message;
if ($id == $this->config('contact.settings')->get('default_form')) {
$this->drupalGet('contact');
$this->submitForm($edit, 'Send message');
}
else {
$this->drupalGet('contact/' . $id);
$this->submitForm($edit, 'Send message');
}
}
}

View File

@@ -0,0 +1,142 @@
<?php
namespace Drupal\Tests\contact_storage\Functional;
/**
* Tests adding contact form as entity reference and viewing them through UI.
*
* @group contact_storage
*/
class ContactViewBuilderTest extends ContactStorageTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'user',
'node',
'contact',
'field_ui',
'contact_test',
'contact_storage',
];
/**
* An administrative user with permission to administer contact forms.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create Article node type.
$this->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
'display_submitted' => FALSE,
]);
}
/**
* Tests contact view builder functionality.
*/
public function testContactViewBuilder() {
// Create test admin user.
$this->adminUser = $this->drupalCreateUser([
'administer content types',
'access site-wide contact form',
'administer contact forms',
'administer users',
'administer account settings',
'administer contact_message fields',
'create article content',
]);
// Login as admin user.
$this->drupalLogin($this->adminUser);
// Create first valid contact form.
$mail = 'simpletest@example.com';
$this->addContactForm('test_id', 'test_label', $mail, TRUE);
$this->assertSession()->pageTextContains('Contact form test_label has been added.');
$field_name = 'contact';
$entity_type = 'node';
$bundle_name = 'article';
// Add a Entity Reference Contact Field to Article content type.
$field_storage = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'entity_reference',
'settings' => ['target_type' => 'contact_form'],
]);
$field_storage->save();
$field = \Drupal::entityTypeManager()
->getStorage('field_config')
->create([
'field_storage' => $field_storage,
'bundle' => $bundle_name,
'settings' => [
'handler' => 'default',
],
]);
$field->save();
// Configure the contact reference field form Entity form display.
$this->container->get('entity_display.repository')->getFormDisplay($entity_type, $bundle_name)
->setComponent($field_name, [
'type' => 'options_select',
'settings' => [
'weight' => 20,
],
])
->save();
// Configure the contact reference field form Entity view display.
$this->container->get('entity_display.repository')->getViewDisplay('node', 'article')
->setComponent($field_name, [
'label' => 'above',
'type' => 'entity_reference_entity_view',
'weight' => 20,
])
->save();
// Display Article creation form.
$this->drupalGet('node/add/article');
$title_key = 'title[0][value]';
$body_key = 'body[0][value]';
$contact_key = 'contact';
// Create article node.
$edit = [];
$edit[$title_key] = $this->randomMachineName(8);
$edit[$body_key] = $this->randomMachineName(16);
$edit[$contact_key] = 'test_id';
$this->drupalGet('node/add/article');
$this->submitForm($edit, 'Save');
// Check that the node exists in the database.
$node = $this->drupalGetNodeByTitle($edit[$title_key]);
$this->drupalGet('node/' . $node->id());
// Some fields should be present.
$this->assertSession()->pageTextContains('Your email address');
$this->assertSession()->pageTextContains('Subject');
$this->assertSession()->pageTextContains('Message');
$this->assertSession()->fieldExists('subject[0][value]');
$this->assertSession()->fieldExists('message[0][value]');
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Drupal\Tests\contact_storage\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests contact_storage ID field.
*
* @group contact_storage
*/
class ContactStorageFieldTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['contact', 'user', 'system'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('contact_message');
}
/**
* Covers contact_storage_mode_preinstall().
*/
public function testContactIdFieldIsCreated() {
$this->container->get('module_installer')->install(['contact_storage']);
// There should be no updates as contact_storage_module_pre_install() should
// have applied the new field.
$this->assertTrue(empty($this->container->get('entity.definition_update_manager')->needsUpdates()['contact_message']));
$this->assertTrue(!empty($this->container->get('entity_field.manager')->getFieldStorageDefinitions('contact_message')['id']));
}
}