View source
<?php
namespace Drupal\crm_core_activity\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ActivityTypeForm extends EntityForm {
protected $messenger;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->messenger = $container
->get('messenger');
return $instance;
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$type = $this->entity;
$form['name'] = [
'#title' => $this
->t('Name'),
'#type' => 'textfield',
'#default_value' => $type->name,
'#description' => $this
->t('The human-readable name of this activity type. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
'#required' => TRUE,
'#size' => 32,
];
$form['type'] = [
'#type' => 'machine_name',
'#default_value' => $type
->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#machine_name' => [
'exists' => 'Drupal\\crm_core_activity\\Entity\\ActivityType::load',
'source' => [
'name',
],
],
'#description' => $this
->t('A unique machine-readable name for this activity type. It must only contain lowercase letters, numbers, and underscores.'),
];
$form['description'] = [
'#title' => $this
->t('Description'),
'#type' => 'textarea',
'#default_value' => $type->description,
'#description' => $this
->t('Describe this activity type.'),
];
$form['activity_string_container'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Display settings'),
];
$form['activity_string_container']['activity_string'] = [
'#type' => 'textfield',
'#title' => $this
->t('Activity string'),
'#description' => $this
->t('Enter text describing the relationship between the contact and this activity. For example: Someone Somewhere registered for this activity.'),
'#default_value' => empty($type->activity_string) ? '' : $type->activity_string,
];
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save activity type');
$actions['delete']['#title'] = $this
->t('Delete activity type');
return $actions;
}
public function save(array $form, FormStateInterface $form_state) {
$type = $this->entity;
$status = $type
->save();
$t_args = [
'%name' => $type
->label(),
];
if ($status == SAVED_UPDATED) {
$this->messenger
->addMessage($this
->t('The activity type %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
$this->messenger
->addMessage($this
->t('The activity type %name has been added.', $t_args));
$this
->logger('crm_core_activity')
->notice('Added activity type %name.', $t_args);
}
$form_state
->setRedirect('entity.crm_core_activity_type.collection');
}
}