View source
<?php
namespace Drupal\token_custom\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\token_custom\Entity\TokenCustomType;
class TokenCustomForm extends ContentEntityForm {
protected $tokenCustomStorage;
protected $tokenCustomTypeStorage;
protected $entity;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->tokenCustomStorage = $entity_type_manager
->getStorage('token_custom');
$this->tokenCustomTypeStorage = $entity_type_manager
->getStorage('token_custom_type');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('entity_type.manager'));
}
public function form(array $form, FormStateInterface $form_state) {
$token = $this->entity;
$form = parent::form($form, $form_state);
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('Edit custom token %label', [
'%label' => $token
->label(),
]);
}
$types = TokenCustomType::loadMultiple();
$options = [];
foreach ($types as $type) {
$options[$type
->id()] = $type
->label();
}
$form['type'] = [
'#type' => 'select',
'#title' => 'Token type',
'#description' => $this
->t('The token type determines the availability of the token according to the data in the $data array (ex. a token of type <em>node</em> will need $data[node].'),
'#options' => $options,
'#maxlength' => 128,
'#default_value' => $token
->bundle(),
'#weight' => -1,
];
$form['machine_name']['widget'][0]['value']['#type'] = 'machine_name';
$form['machine_name']['widget'][0]['value']['#machine_name'] = [
'exists' => '\\Drupal\\token_custom\\Entity\\TokenCustom::load',
];
$account = $this
->currentUser();
$form['machine_name']['#access'] = $account
->hasPermission('administer custom tokens');
$form['machine_name']['#disabled'] = !$token
->isNew();
$form['machine_name']['widget'][0]['value']['#required'] = TRUE;
$form['machine_name']['#required'] = TRUE;
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$token = $this->entity;
$insert = $token
->isNew();
$token
->save();
token_clear_cache();
$context = [
'@type' => $token
->bundle(),
'%info' => $token
->label(),
];
$logger = $this
->logger('token_custom');
$token_type = $this->tokenCustomTypeStorage
->load($token
->bundle());
$t_args = [
'@type' => $token_type
->label(),
'%info' => $token
->label(),
];
if ($insert) {
$logger
->notice('@type: added %info.', $context);
$this
->messenger()
->addStatus($this
->t('@type %info has been created.', $t_args));
}
else {
$logger
->notice('@type: updated %info.', $context);
$this
->messenger()
->addStatus($this
->t('@type %info has been updated.', $t_args));
}
if ($token
->id()) {
$form_state
->setValue('id', $token
->id());
$form_state
->set('id', $token
->id());
$form_state
->setRedirectUrl($token
->toUrl('collection'));
}
else {
$this
->messenger()
->addError($this
->t('The token could not be saved.'));
$form_state
->setRebuild();
}
}
}