View source
<?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;
class ContactFormCloneForm extends ContactFormEditForm {
protected $fieldManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('email.validator'), $container
->get('path.validator'), $container
->get('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;
}
public function form(array $form, FormStateInterface $form_state) {
$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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function save(array $form, FormStateInterface $form_state) {
$contact_form = $this->entity;
$original_id = $contact_form
->getOriginalId();
$new_id = $contact_form
->id();
$contact_form = $contact_form
->createDuplicate();
$contact_form
->set('id', $new_id);
$contact_form
->save();
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();
}
}
}
$display = EntityFormDisplay::load('contact_message.' . $original_id . '.default');
EntityFormDisplay::create([
'bundle' => $contact_form
->id(),
'uuid' => NULL,
] + $display
->toArray())
->save();
$display = EntityViewDisplay::load('contact_message.' . $original_id . '.default');
EntityViewDisplay::create([
'bundle' => $contact_form
->id(),
'uuid' => NULL,
] + $display
->toArray())
->save();
$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,
]);
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Clone');
return $actions;
}
}