CategoryForm.php 4.38 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\contact\CategoryForm.
6 7 8 9
 */

namespace Drupal\contact;

10
use Drupal\Core\Entity\EntityForm;
11
use Drupal\Core\Entity\EntityTypeInterface;
12 13

/**
14
 * Base form for category edit forms.
15
 */
16
class CategoryForm extends EntityForm {
17 18

  /**
19
   * Overrides Drupal\Core\Entity\EntityForm::form().
20
   */
21 22
  public function form(array $form, array &$form_state) {
    $form = parent::form($form, $form_state);
23

24
    $category = $this->entity;
25
    $default_category = \Drupal::config('contact.settings')->get('default_category');
26 27 28 29 30 31 32 33 34 35 36 37

    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#maxlength' => 255,
      '#default_value' => $category->label(),
      '#description' => t("Example: 'website feedback' or 'product information'."),
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $category->id(),
38
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      '#machine_name' => array(
        'exists' => 'contact_category_load',
      ),
      '#disabled' => !$category->isNew(),
    );
    $form['recipients'] = array(
      '#type' => 'textarea',
      '#title' => t('Recipients'),
      '#default_value' => implode(', ', $category->recipients),
      '#description' => t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com' . To specify multiple recipients, separate each e-mail address with a comma."),
      '#required' => TRUE,
    );
    $form['reply'] = array(
      '#type' => 'textarea',
      '#title' => t('Auto-reply'),
      '#default_value' => $category->reply,
      '#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
    );
    $form['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#default_value' => $category->weight,
      '#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'),
    );
    $form['selected'] = array(
      '#type' => 'checkbox',
      '#title' => t('Make this the default category.'),
      '#default_value' => $default_category === $category->id(),
    );
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );

    return $form;
  }

  /**
78
   * Overrides Drupal\Core\Entity\EntityForm::validate().
79 80 81 82 83 84 85 86 87 88
   */
  public function validate(array $form, array &$form_state) {
    parent::validate($form, $form_state);

    // Validate and each e-mail recipient.
    $recipients = explode(',', $form_state['values']['recipients']);

    foreach ($recipients as &$recipient) {
      $recipient = trim($recipient);
      if (!valid_email_address($recipient)) {
89
        $this->setFormError('recipients', $form_state, $this->t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
90 91 92 93 94 95
      }
    }
    $form_state['values']['recipients'] = $recipients;
  }

  /**
96
   * Overrides Drupal\Core\Entity\EntityForm::save().
97 98
   */
  public function save(array $form, array &$form_state) {
99
    $category = $this->entity;
100
    $status = $category->save();
101

102
    $edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
103

104 105
    if ($status == SAVED_UPDATED) {
      drupal_set_message(t('Category %label has been updated.', array('%label' => $category->label())));
106
      watchdog('contact', 'Category %label has been updated.', array('%label' => $category->label()), WATCHDOG_NOTICE, $edit_link);
107 108
    }
    else {
109
      drupal_set_message(t('Category %label has been added.', array('%label' => $category->label())));
110
      watchdog('contact', 'Category %label has been added.', array('%label' => $category->label()), WATCHDOG_NOTICE, $edit_link);
111 112 113
    }

    // Update the default category.
114
    $contact_config = \Drupal::config('contact.settings');
115 116
    if ($form_state['values']['selected']) {
      $contact_config
117
        ->set('default_category', $category->id())
118 119 120
        ->save();
    }
    // If it was the default category, empty out the setting.
121
    elseif ($contact_config->get('default_category') == $category->id()) {
122
      $contact_config
123
        ->set('default_category', NULL)
124 125 126
        ->save();
    }

127
    $form_state['redirect_route']['route_name'] = 'contact.category_list';
128
  }
129

130
}