Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Definition of Drupal\entity_test\EntityTestFormController.
*/
namespace Drupal\entity_test;
use Drupal\Core\Entity\EntityInterface;
Dries Buytaert
committed
use Drupal\Core\Entity\EntityFormControllerNG;
Angie Byron
committed
/**
* Form controller for the test entity edit forms.
*/
Dries Buytaert
committed
class EntityTestFormController extends EntityFormControllerNG {
Angie Byron
committed
/**
* Overrides Drupal\Core\Entity\EntityFormController::form().
*/
public function form(array $form, array &$form_state, EntityInterface $entity) {
$form = parent::form($form, $form_state, $entity);
$langcode = $this->getFormLangcode($form_state);
Dries Buytaert
committed
$translation = $entity->getTranslation($langcode);
Angie Byron
committed
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
Dries Buytaert
committed
'#default_value' => $translation->name->value,
Angie Byron
committed
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#weight' => -10,
);
Dries Buytaert
committed
$form['user_id'] = array(
Angie Byron
committed
'#type' => 'textfield',
'#title' => 'UID',
Dries Buytaert
committed
'#default_value' => $translation->user_id->value,
Angie Byron
committed
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#weight' => -10,
);
Angie Byron
committed
$form['langcode'] = array(
'#title' => t('Language'),
'#type' => 'language_select',
'#default_value' => $entity->language()->langcode,
'#languages' => LANGUAGE_ALL,
);
Angie Byron
committed
Angie Byron
committed
return $form;
Angie Byron
committed
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::save().
*/
public function save(array $form, array &$form_state) {
$entity = $this->getEntity($form_state);
$is_new = $entity->isNew();
$entity->save();
$message = $is_new ? t('entity_test @id has been created.', array('@id' => $entity->id())) : t('entity_test @id has been updated.', array('@id' => $entity->id()));
drupal_set_message($message);
if ($entity->id()) {
$form_state['redirect'] = 'entity-test/manage/' . $entity->id() . '/edit';
}
else {
// Error on save.
drupal_set_message(t('The entity could not be saved.'), 'error');
$form_state['rebuild'] = TRUE;
}
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::delete().
*/
public function delete(array $form, array &$form_state) {
$entity = $this->getEntity($form_state);
$entity->delete();
drupal_set_message(t('entity_test @id has been deleted.', array('@id' => $entity->id())));
$form_state['redirect'] = '<front>';
}
}