Commit 1793df6d authored by alexpott's avatar alexpott

Issue #2289063 by larowlan, andypost | Berdir: Change contact message entity...

Issue #2289063 by larowlan, andypost | Berdir: Change contact message entity to behave more like a normal entity.
parent 8bb62da2
......@@ -386,7 +386,7 @@ protected function buildBaseFieldDefinitions($entity_type_id) {
foreach ($module_definitions as $field_name => $definition) {
// @todo Remove this check once FieldDefinitionInterface exposes a
// proper provider setter. See https://drupal.org/node/2225961.
if ($definition instanceof FieldDefinition) {
if ($definition instanceof FieldDefinition && $definition->getProvider() == NULL) {
$definition->setProvider($module);
}
$base_field_definitions[$field_name] = $definition;
......
......@@ -183,7 +183,7 @@ public function setSetting($setting_name, $value) {
* {@inheritdoc}
*/
public function getProvider() {
return $this->definition['provider'];
return isset($this->definition['provider']) ? $this->definition['provider'] : NULL;
}
/**
......
......@@ -26,7 +26,8 @@
* }
* },
* entity_keys = {
* "bundle" = "category"
* "bundle" = "category",
* "uuid" = "uuid"
* },
* bundle_entity_type = "contact_category",
* fieldable = TRUE,
......@@ -37,20 +38,6 @@
*/
class Message extends ContentEntityBase implements MessageInterface {
/**
* {@inheritdoc}
*/
public function id() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function uuid() {
return NULL;
}
/**
* {@inheritdoc}
*/
......@@ -154,6 +141,15 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setSetting('target_type', 'contact_category')
->setRequired(TRUE);
$fields['uuid'] = FieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The message UUID.'))
->setReadOnly(TRUE);
$fields['langcode'] = FieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The comment language code.'));
$fields['name'] = FieldDefinition::create('string')
->setLabel(t("The sender's name"))
->setDescription(t('The name of the person that is sending the contact message.'));
......
......@@ -11,7 +11,9 @@
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Flood\FloodInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -33,6 +35,13 @@ class MessageForm extends ContentEntityForm {
*/
protected $flood;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Constructs a MessageForm object.
*
......@@ -40,11 +49,13 @@ class MessageForm extends ContentEntityForm {
* The entity manager.
* @param \Drupal\Core\Flood\FloodInterface $flood
* The flood control mechanism.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
*/
public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood) {
public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood, LanguageManagerInterface $language_manager) {
parent::__construct($entity_manager);
$this->flood = $flood;
$this->languageManager = $language_manager;
}
/**
......@@ -53,7 +64,8 @@ public function __construct(EntityManagerInterface $entity_manager, FloodInterfa
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('flood')
$container->get('flood'),
$container->get('language_manager')
);
}
......@@ -61,7 +73,7 @@ public static function create(ContainerInterface $container) {
* Overrides Drupal\Core\Entity\EntityForm::form().
*/
public function form(array $form, array &$form_state) {
$user = \Drupal::currentUser();
$user = $this->currentUser();
$message = $this->entity;
$form = parent::form($form, $form_state, $message);
$form['#attributes']['class'][] = 'contact-form';
......@@ -71,9 +83,18 @@ public function form(array $form, array &$form_state) {
'#theme_wrappers' => array('container__preview'),
'#attributes' => array('class' => array('preview')),
);
$form['preview']['message'] = entity_view($message, 'full');
$form['preview']['message'] = $this->entityManager->getViewBuilder('contact_message')->view($message, 'full');
}
$language_configuration = $this->moduleHandler->invoke('language', 'get_default_configuration', array('contact_message', $message->getCategory()->id()));
$form['langcode'] = array(
'#title' => t('Language'),
'#type' => 'language_select',
'#default_value' => $message->getUntranslated()->language()->id,
'#languages' => Language::STATE_ALL,
'#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
......@@ -158,18 +179,19 @@ public function preview(array $form, array &$form_state) {
* Overrides Drupal\Core\Entity\EntityForm::save().
*/
public function save(array $form, array &$form_state) {
$user = \Drupal::currentUser();
$user = $this->currentUser();
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
$language_interface = $this->languageManager->getCurrentLanguage();
$message = $this->entity;
$sender = clone user_load($user->id());
$sender = clone $this->entityManager->getStorage('user')->load($user->id());
if ($user->isAnonymous()) {
// At this point, $sender contains an anonymous user, so we need to take
// over the submitted form values.
$sender->name = $message->getSenderName();
$sender->mail = $message->getSenderMail();
// Save the anonymous user information to a cookie for reuse.
// @todo remove when https://www.drupal.org/node/749748 is in.
user_cookie_save(array('name' => $message->getSenderName(), 'mail' => $message->getSenderMail()));
// For the email message, clarify that the sender name is not verified; it
// could potentially clash with a username on this site.
......@@ -186,7 +208,7 @@ public function save(array $form, array &$form_state) {
$params['contact_category'] = $category;
$to = implode(', ', $category->recipients);
$recipient_langcode = language_default()->id;
$recipient_langcode = $this->languageManager->getDefaultLanguage()->getId();
}
elseif ($recipient = $message->getPersonalRecipient()) {
// Send to the user in the user's preferred language.
......@@ -241,5 +263,26 @@ public function save(array $form, array &$form_state) {
else {
$form_state['redirect_route']['route_name'] = '<front>';
}
// Save the message. In core this is a no-op but should contrib wish to
// implement message storage, this will make the task of swapping in a real
// storage controller straight-forward.
$message->save();
}
/**
* {@inheritdoc}
*/
protected function init(array &$form_state) {
$message = $this->entity;
// Make the message inherit the current content language unless specifically
// set.
if ($message->isNew() && !$message->langcode->value) {
$language_content = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$message->langcode->value = $language_content->id;
}
parent::init($form_state);
}
}
<?php
/**
* @file
* Contains \Drupal\contact\Tests\ContactStorageTest.
*/
namespace Drupal\contact\Tests;
use Drupal\contact\Entity\Message;
/**
* Tests storing contact messages.
*/
class ContactStorageTest extends ContactSitewideTest {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array(
'text',
'contact',
'field_ui',
'contact_storage_test',
);
public static function getInfo() {
return array(
'name' => 'Contact Storage',
'description' => 'Tests that contact messages can be stored.',
'group' => 'Contact',
);
}
/**
* Tests configuration options and the site-wide contact form.
*/
public function testContactStorage() {
// Create and login administrative user.
$admin_user = $this->drupalCreateUser(array(
'access site-wide contact form',
'administer contact forms',
'administer users',
'administer account settings',
'administer contact_message fields',
));
$this->drupalLogin($admin_user);
// Create first valid category.
$mail = 'simpletest@example.com';
$this->addCategory($id = drupal_strtolower($this->randomName(16)), $label = $this->randomName(16), implode(',', array($mail)), '', TRUE);
$this->assertRaw(t('Category %label has been added.', array('%label' => $label)));
// Ensure that anonymous can submit site-wide contact form.
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
$this->drupalLogout();
$this->drupalGet('contact');
$this->assertText(t('Your email address'));
$this->assertNoText(t('Category'));
$this->submitContact($name = $this->randomName(16), $mail, $subject = $this->randomName(16), $id, $message = $this->randomName(64));
$this->assertText(t('Your message has been sent.'));
$messages = Message::loadMultiple();
/** @var \Drupal\contact\Entity\Message $message */
$message = reset($messages);
$this->assertEqual($message->getCategory()->id(), $id);
$this->assertEqual($message->getSenderName(), $name);
$this->assertEqual($message->getSubject(), $subject);
$this->assertEqual($message->getSenderMail(), $mail);
}
}
name: 'Contact test storage'
type: module
description: 'Tests that contact messages can be stored.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- contact
- user
<?php
/**
* @file
* Contains install and update hooks.
*/
/**
* Implements hook_install().
*/
function contact_storage_test_install() {
// ModuleHandler won't create the schema automatically because Message entity
// belongs to contact.module.
// @todo Remove this when https://www.drupal.org/node/1498720 is in.
$entity_manager = \Drupal::entityManager();
$schema = \Drupal::database()->schema();
$entity_type = $entity_manager->getDefinition('contact_message');
$storage = $entity_manager->getStorage($entity_type->id());
foreach ($storage->getSchema() as $table_name => $table_schema) {
if (!$schema->tableExists($table_name)) {
$schema->createTable($table_name, $table_schema);
}
}
}
<?php
/**
* @file
* Contains custom contact message functionality for ContactStorageTest.
*/
use Drupal\Core\Field\FieldDefinition;
/**
* Implements hook_entity_base_field_info().
*/
function contact_storage_test_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'contact_message') {
$fields = array();
$fields['id'] = FieldDefinition::create('integer')
->setLabel(t('Message ID'))
->setDescription(t('The message ID.'))
->setReadOnly(TRUE)
// Explicitly set this to 'contact' so that
// ContentEntityDatabaseStorage::usesDedicatedTable() doesn't attempt to
// put the ID in a dedicated table.
// @todo Remove when https://www.drupal.org/node/1498720 is in.
->setProvider('contact')
->setSetting('unsigned', TRUE);
return $fields;
}
}
/**
* Implements hook_entity_type_alter().
*/
function contact_storage_test_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\ContentEntityDatabaseStorage');
$keys = $entity_types['contact_message']->getKeys();
$keys['id'] = 'id';
$entity_types['contact_message']->set('entity_keys', $keys);
$entity_types['contact_message']->set('base_table', 'contact_message');
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment