View source
<?php
namespace Drupal\profile;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\String;
use Drupal\profile\Entity\ProfileType;
class ProfileTypeFormController extends EntityForm {
function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$type = $this->entity;
if ($this->operation == 'add') {
$form['#title'] = String::checkPlain($this
->t('Add profile type'));
}
else {
$form['#title'] = $this
->t('Edit %label profile type', array(
'%label' => $type
->label(),
));
}
$form['label'] = array(
'#title' => t('Label'),
'#type' => 'textfield',
'#default_value' => $type
->label(),
'#description' => t('The human-readable name of this profile type.'),
'#required' => TRUE,
'#size' => 30,
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $type
->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#machine_name' => array(
'exists' => array(
$this,
'exists',
),
),
);
$form['registration'] = array(
'#type' => 'checkbox',
'#title' => t('Include in user registration form'),
'#default_value' => $type->registration,
);
$form['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Allow multiple profiles'),
'#default_value' => $type->multiple,
);
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
if (\Drupal::moduleHandler()
->moduleExists('field_ui') && $this
->getEntity()
->isNew()) {
$actions['save_continue'] = $actions['submit'];
$actions['save_continue']['#value'] = t('Save and manage fields');
$actions['save_continue']['#submit'][] = array(
$this,
'redirectToFieldUI',
);
}
return $actions;
}
public function save(array $form, FormStateInterface $form_state) {
$type = $this->entity;
$status = $type
->save();
if ($status == SAVED_UPDATED) {
drupal_set_message(t('%label profile type has been updated.', array(
'%label' => $type
->label(),
)));
}
else {
drupal_set_message(t('%label profile type has been created.', array(
'%label' => $type
->label(),
)));
}
$form_state
->setRedirect('profile.overview_types');
}
public function redirectToFieldUI(array $form, FormStateInterface $form_state) {
$form_state
->setRedirect('field_ui.overview_profile', array(
'profile_type' => $this->entity
->id(),
));
}
public function delete(array $form, FormStateInterface $form_state) {
$form_state
->setRedirect('entity.profile_type.delete_form', array(
'profile_type' => $this->entity
->id(),
));
}
public function exists($id) {
$profile_type = ProfileType::load($id);
return !empty($profile_type);
}
}