diff --git a/core/modules/contact/config/contact.category.personal.yml b/core/modules/contact/config/contact.category.personal.yml new file mode 100644 index 0000000..6b40a8d --- /dev/null +++ b/core/modules/contact/config/contact.category.personal.yml @@ -0,0 +1,5 @@ +id: personal +label: 'Personal contact form' +recipients: [] +reply: '' +weight: '0' diff --git a/core/modules/contact/contact.pages.inc b/core/modules/contact/contact.pages.inc index 5ee9fb7..423cd72 100644 --- a/core/modules/contact/contact.pages.inc +++ b/core/modules/contact/contact.pages.inc @@ -76,6 +76,7 @@ function contact_personal_page($recipient) { $message = entity_create('contact_message', array( 'recipient' => $recipient, + 'category' => 'personal', )); return entity_get_form($message); } diff --git a/core/modules/contact/contact.routing.yml b/core/modules/contact/contact.routing.yml index 1f34937..8d6132a 100644 --- a/core/modules/contact/contact.routing.yml +++ b/core/modules/contact/contact.routing.yml @@ -3,4 +3,4 @@ contact_category_delete: defaults: _form: '\Drupal\contact\Form\DeleteForm' requirements: - _permission: 'administer contact forms' + _entity_access: 'contact_category.delete' diff --git a/core/modules/contact/lib/Drupal/contact/CategoryAccessController.php b/core/modules/contact/lib/Drupal/contact/CategoryAccessController.php new file mode 100644 index 0000000..91114a7 --- /dev/null +++ b/core/modules/contact/lib/Drupal/contact/CategoryAccessController.php @@ -0,0 +1,34 @@ +id() !== 'personal'; + } + else { + return user_access('administer contact forms', $account); + } + } + +} diff --git a/core/modules/contact/lib/Drupal/contact/CategoryFormController.php b/core/modules/contact/lib/Drupal/contact/CategoryFormController.php index 8febed8..5f2e78c 100644 --- a/core/modules/contact/lib/Drupal/contact/CategoryFormController.php +++ b/core/modules/contact/lib/Drupal/contact/CategoryFormController.php @@ -128,8 +128,7 @@ public function save(array $form, array &$form_state) { * Overrides Drupal\Core\Entity\EntityFormController::delete(). */ public function delete(array $form, array &$form_state) { - $category = $this->entity; - $form_state['redirect'] = 'admin/structure/contact/manage/' . $category->id() . '/delete'; + $form_state['redirect'] = 'admin/structure/contact/manage/' . $this->entity->id() . '/delete'; } } diff --git a/core/modules/contact/lib/Drupal/contact/CategoryListController.php b/core/modules/contact/lib/Drupal/contact/CategoryListController.php index a3ec886..3bb0505 100644 --- a/core/modules/contact/lib/Drupal/contact/CategoryListController.php +++ b/core/modules/contact/lib/Drupal/contact/CategoryListController.php @@ -34,6 +34,13 @@ public function getOperations(EntityInterface $entity) { 'weight' => 12, ); } + + if (!$entity->access('delete')) { + unset($operations['delete']); + } + if (!$entity->access('update')) { + unset($operations['edit']); + } return $operations; } diff --git a/core/modules/contact/lib/Drupal/contact/MessageInterface.php b/core/modules/contact/lib/Drupal/contact/MessageInterface.php index 9e8879b..f01ad9f 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageInterface.php +++ b/core/modules/contact/lib/Drupal/contact/MessageInterface.php @@ -14,4 +14,12 @@ */ interface MessageInterface extends EntityInterface { + /** + * Return TRUE if this is the personal contact form. + * + * @return bool + * TRUE if the message bundle is personal. + */ + public function isPersonal(); + } diff --git a/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Category.php b/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Category.php index 7e068da..a760f94 100644 --- a/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Category.php +++ b/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Category.php @@ -21,6 +21,7 @@ * module = "contact", * controllers = { * "storage" = "Drupal\contact\CategoryStorageController", + * "access" = "Drupal\contact\CategoryAccessController", * "list" = "Drupal\contact\CategoryListController", * "form" = { * "default" = "Drupal\contact\CategoryFormController" diff --git a/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php b/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php index 667c06a..3bec9e9 100644 --- a/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php +++ b/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php @@ -69,9 +69,8 @@ class Message extends Entity implements MessageInterface { * @see Drupal\contact\MessageFormController::form() * @see Drupal\contact\MessageFormController::save() * - * @todo Convert user contact form into a locked contact category, and replace - * Category::$recipients with the user account's e-mail address upon - * Entity::create(). + * @todo Replace Category::$recipients with the user account's e-mail address + * upon Entity::create(). * * @var Drupal\user\Plugin\Core\Entity\User */ @@ -113,17 +112,10 @@ public function bundle() { } /** - * Overrides Drupal\Core\Entity\Entity::entityInfo(). + * {@inheritdoc} */ - public function entityInfo() { - // The user contact form is not a category/bundle currently, so it is not - // fieldable. Prevent EntityFormController from calling into Field Attach - // functions, since those will throw errors without a bundle name. - $info = entity_get_info($this->entityType); - if (isset($this->recipient)) { - $info['fieldable'] = FALSE; - } - return $info; + public function isPersonal() { + return $this->bundle() == 'personal'; } } diff --git a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php index 1ea830b..b0e46bf 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php @@ -49,8 +49,16 @@ function testSiteWideContact() { $this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration')); $this->assertText(t('The configuration options have been saved.')); + $this->drupalGet('admin/structure/contact'); + // Default category exists. + $this->assertLinkByHref('admin/structure/contact/manage/feedback/delete'); + // User category could not be deleted. + $this->assertNoLinkByHref('admin/structure/contact/manage/personal/delete'); // Delete old categories to ensure that new categories are used. $this->deleteCategories(); + $this->drupalGet('admin/structure/contact'); + $this->assertLinkByHref('admin/structure/contact/manage/personal'); + $this->assertNoLinkByHref('admin/structure/contact/manage/feedback'); // Ensure that the contact form won't be shown without categories. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form')); @@ -179,10 +187,6 @@ function testSiteWideContact() { $this->drupalGet('contact'); $this->assertResponse(403); $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => config('contact.settings')->get('flood.limit'), '@interval' => format_interval(600)))); - - // Delete created categories. - $this->drupalLogin($admin_user); - $this->deleteCategories(); } /** @@ -313,9 +317,16 @@ function submitContact($name, $mail, $subject, $id, $message) { function deleteCategories() { $categories = entity_load_multiple('contact_category'); foreach ($categories as $id => $category) { - $this->drupalPost("admin/structure/contact/manage/$id/delete", array(), t('Delete')); - $this->assertRaw(t('Category %label has been deleted.', array('%label' => $category->label()))); - $this->assertFalse(entity_load('contact_category', $id), format_string('Category %category not found', array('%category' => $category->label()))); + if ($id == 'personal') { + // Personal category could not be deleted. + $this->drupalGet("admin/structure/contact/manage/$id/delete"); + $this->assertResponse(403); + } + else { + $this->drupalPost("admin/structure/contact/manage/$id/delete", array(), t('Delete')); + $this->assertRaw(t('Category %label has been deleted.', array('%label' => $category->label()))); + $this->assertFalse(entity_load('contact_category', $id), format_string('Category %category not found', array('%category' => $category->label()))); + } } }