diff --git a/core/modules/contact/contact.pages.inc b/core/modules/contact/contact.pages.inc index 5ee9fb7..2eed000 100644 --- a/core/modules/contact/contact.pages.inc +++ b/core/modules/contact/contact.pages.inc @@ -28,14 +28,15 @@ function contact_site_page(Category $category = NULL) { contact_flood_control(); } - if (!isset($category)) { + if ($category) { + $message = entity_create('contact_message', array( + 'category' => $category->id(), + )); + } + else { $categories = entity_load_multiple('contact_category'); - $default_category = config('contact.settings')->get('default_category'); - if (isset($categories[$default_category])) { - $category = $categories[$default_category]; - } // If there are no categories, do not display the form. - else { + if (empty($categories)) { if (user_access('administer contact forms')) { drupal_set_message(t('The contact form has not been configured. Add one or more categories to the form.', array('@add' => url('admin/structure/contact/add'))), 'error'); return array(); @@ -44,10 +45,9 @@ function contact_site_page(Category $category = NULL) { throw new NotFoundHttpException(); } } + $message = entity_create('contact_message', array()); } - $message = entity_create('contact_message', array( - 'category' => $category->id(), - )); + return entity_get_form($message); } diff --git a/core/modules/contact/lib/Drupal/contact/MessageFormController.php b/core/modules/contact/lib/Drupal/contact/MessageFormController.php index 6f73d96..e312641 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageFormController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageFormController.php @@ -75,10 +75,28 @@ public function form(array $form, array &$form_state) { ); } else { - $form['category'] = array( - '#type' => 'value', - '#value' => $message->category, - ); + if ($message->category) { + $form['category'] = array( + '#type' => 'value', + '#value' => $message->category, + ); + } + else { + $categories = entity_load_multiple('contact_category'); + $options = array(); + foreach ($categories as $category) { + $options[$category->id()] = $category->label(); + } + + $form['category'] = array( + '#type' => 'select', + '#title' => t('Category'), + '#default_value' => config('contact.settings')->get('default_category') ?: NULL, + '#options' => $options, + '#required' => TRUE, + '#access' => count($options) > 1, + ); + } } $form['subject'] = array( diff --git a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php index 1ea830b..6555ab5 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php @@ -117,6 +117,30 @@ function testSiteWideContact() { $this->addCategory($name = drupal_strtolower($this->randomName(16)), $label = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE); $this->assertRaw(t('Category %label has been added.', array('%label' => $label))); + // Ensure that the category selector appears on the site wide form. + $this->drupalGet('contact'); + $this->assertText(t('Category')); + $this->assertRaw($name); + $this->assertText($label); + $this->assertNoText('- Please choose -'); + // Test that the current default id is selected. + $this->assertTrue($this->xpath('//select[@id="edit-category"]//option[@value="' . $id . '" and @selected="selected"]'), 'Change default contact category is selected.'); + + // Change the default category and test that it is selected. + config('contact.settings')->set('default_category', $name)->save(); + $this->drupalGet('contact'); + $this->assertTrue($this->xpath('//select[@id="edit-category"]//option[@value="' . $name . '" and @selected="selected"]'), 'Change default contact category is selected.'); + + // Have no default category. + config('contact.settings')->set('default_category', '')->save(); + $this->drupalGet('contact'); + $this->assertTrue($this->xpath('//select[@id="edit-category"]//option[@value="" and @selected="selected"]'), ''); + + // Get a contact form for a specific category. + $this->drupalGet('contact/' . $name); + $this->assertNoText(t('Category')); + $this->assertRaw($name); + // Try adding a category that already exists. $this->addCategory($name, $label, '', '', FALSE); $this->assertNoRaw(t('Category %label has been saved.', array('%label' => $label))); @@ -128,6 +152,13 @@ function testSiteWideContact() { $this->assertIdentical($num_records_after, '0', 'Flood table emptied.'); $this->drupalLogout(); + // Ensure that the category selector appears on the site wide form for + // anonymous user. + $this->drupalGet('contact'); + $this->assertText(t('Category')); + $this->assertRaw($name); + $this->assertText($label); + // Check to see that anonymous user cannot see contact page without permission. user_role_revoke_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form')); $this->drupalGet('contact'); @@ -157,13 +188,6 @@ function testSiteWideContact() { $this->submitContact($this->randomName(16), $recipients[0], $this->randomName(16), $id, ''); $this->assertText(t('Message field is required.')); - // Test contact form with no default category selected. - config('contact.settings') - ->set('default_category', '') - ->save(); - $this->drupalGet('contact'); - $this->assertResponse(404); - // Try to access contact form with non-existing category IDs. $this->drupalGet('contact/0'); $this->assertResponse(404);