class CategoryFormController

Base form controller for category edit forms.

Hierarchy

Expanded class hierarchy of CategoryFormController

File

drupal/core/modules/contact/lib/Drupal/contact/CategoryFormController.php, line 16
Definition of Drupal\contact\CategoryFormController.

Namespace

Drupal\contact
View source
class CategoryFormController extends EntityFormController {

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   */
  public function form(array $form, array &$form_state, EntityInterface $category) {
    $form = parent::form($form, $form_state, $category);
    $default_category = config('contact.settings')
      ->get('default_category');
    $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(),
      '#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;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::validate().
   */
  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)) {
        form_set_error('recipients', t('%recipient is an invalid e-mail address.', array(
          '%recipient' => $recipient,
        )));
      }
    }
    $form_state['values']['recipients'] = $recipients;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::save().
   */
  public function save(array $form, array &$form_state) {
    $category = $this
      ->getEntity($form_state);
    $status = $category
      ->save();
    if ($status == SAVED_UPDATED) {
      drupal_set_message(t('Category %label has been updated.', array(
        '%label' => $category
          ->label(),
      )));
      watchdog('contact', 'Category %label has been updated.', array(
        '%label' => $category
          ->label(),
      ), WATCHDOG_NOTICE, l(t('Edit'), $category
        ->uri() . '/edit'));
    }
    else {
      drupal_set_message(t('Category %label has been added.', array(
        '%label' => $category
          ->label(),
      )));
      watchdog('contact', 'Category %label has been added.', array(
        '%label' => $category
          ->label(),
      ), WATCHDOG_NOTICE, l(t('Edit'), $category
        ->uri() . '/edit'));
    }

    // Update the default category.
    $contact_config = config('contact.settings');
    if ($form_state['values']['selected']) {
      $contact_config
        ->set('default_category', $category
        ->id())
        ->save();
    }
    elseif ($contact_config
      ->get('default_category') == $category
      ->id()) {
      $contact_config
        ->set('default_category', NULL)
        ->save();
    }
    $form_state['redirect'] = 'admin/structure/contact';
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::delete().
   */
  public function delete(array $form, array &$form_state) {
    $category = $this
      ->getEntity($form_state);
    $form_state['redirect'] = 'admin/structure/contact/manage/' . $category
      ->id() . '/delete';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CategoryFormController::delete public function Overrides Drupal\Core\Entity\EntityFormController::delete(). Overrides EntityFormController::delete
CategoryFormController::form public function Overrides Drupal\Core\Entity\EntityFormController::form(). Overrides EntityFormController::form
CategoryFormController::save public function Overrides Drupal\Core\Entity\EntityFormController::save(). Overrides EntityFormController::save
CategoryFormController::validate public function Overrides Drupal\Core\Entity\EntityFormController::validate(). Overrides EntityFormController::validate
EntityFormController::$operation protected property The name of the current operation.
EntityFormController::actions protected function Returns an array of supported actions for the current entity form. 10
EntityFormController::actionsElement protected function Returns the action form element for the current entity form. 1
EntityFormController::build public function Implements Drupal\Core\Entity\EntityFormControllerInterface::build(). Overrides EntityFormControllerInterface::build
EntityFormController::buildEntity public function Implements Drupal\Core\Entity\EntityFormControllerInterface::buildEntity(). Overrides EntityFormControllerInterface::buildEntity 1
EntityFormController::getEntity public function Implements Drupal\Core\Entity\EntityFormControllerInterface::getEntity(). Overrides EntityFormControllerInterface::getEntity 1
EntityFormController::getFormLangcode public function Implements Drupal\Core\Entity\EntityFormControllerInterface::getFormLangcode(). Overrides EntityFormControllerInterface::getFormLangcode
EntityFormController::getOperation public function Implements Drupal\Core\Entity\EntityFormControllerInterface::getOperation(). Overrides EntityFormControllerInterface::getOperation
EntityFormController::init protected function Initialize the form state and the entity before the first form build.
EntityFormController::isDefaultFormLangcode public function Implements EntityFormControllerInterface::isDefaultFormLangcode(). Overrides EntityFormControllerInterface::isDefaultFormLangcode
EntityFormController::prepareEntity protected function Prepares the entity object before the form is built first. 2
EntityFormController::setEntity public function Implements Drupal\Core\Entity\EntityFormControllerInterface::setEntity(). Overrides EntityFormControllerInterface::setEntity 1
EntityFormController::submit public function Implements Drupal\Core\Entity\EntityFormControllerInterface::submit(). Overrides EntityFormControllerInterface::submit 9
EntityFormController::submitEntityLanguage protected function Handle possible entity language changes.
EntityFormController::__construct public function Constructs an EntityFormController object.