View source
<?php
namespace Drupal\mollie_payment\Entity\MollieProfile;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface;
use Drupal\plugin\PluginType\PluginTypeManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MollieProfileForm extends EntityForm {
protected $mollieProfileStorage;
protected $pluginSelectorManager;
protected $pluginTypeManager;
public function __construct(TranslationInterface $string_translation, EntityStorageInterface $mollie_profile_storage, PluginSelectorManagerInterface $plugin_selector_manager, PluginTypeManager $plugin_type_manager) {
$this->mollieProfileStorage = $mollie_profile_storage;
$this->pluginSelectorManager = $plugin_selector_manager;
$this->pluginTypeManager = $plugin_type_manager;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container) {
$entity_manager = $container
->get('entity.manager');
return new static($container
->get('string_translation'), $entity_manager
->getStorage('mollie_profile'), $container
->get('plugin.manager.plugin.plugin_selector'), $container
->get('plugin.plugin_type_manager'));
}
public function form(array $form, FormStateInterface $form_state) {
$mollie_profile = $this
->getEntity();
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#default_value' => $mollie_profile
->label(),
'#maxlength' => 255,
'#required' => TRUE,
];
$form['id'] = [
'#default_value' => $mollie_profile
->id(),
'#disabled' => !$mollie_profile
->isNew(),
'#machine_name' => [
'source' => [
'label',
],
'exists' => [
$this,
'MollieProfileIdExists',
],
],
'#maxlength' => 255,
'#type' => 'machine_name',
'#required' => TRUE,
];
$form['apiKey'] = [
'#type' => 'textfield',
'#title' => $this
->t('API key'),
'#default_value' => $mollie_profile
->getApiKey(),
'#maxlength' => 255,
'#required' => TRUE,
];
return parent::form($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
}
protected function copyFormValuesToEntity(EntityInterface $mollie_profile, array $form, FormStateInterface $form_state) {
parent::copyFormValuesToEntity($mollie_profile, $form, $form_state);
$values = $form_state
->getValues();
$mollie_profile
->setId($values['id']);
$mollie_profile
->setLabel($values['label']);
$mollie_profile
->setApiKey($values['apiKey']);
}
public function save(array $form, FormStateInterface $form_state) {
$mollie_profile = $this
->getEntity();
$mollie_profile
->save();
drupal_set_message($this
->t('@label has been saved.', [
'@label' => $mollie_profile
->label(),
]));
$form_state
->setRedirect('entity.mollie_profile.collection');
}
public function mollieProfileIdExists($id) {
return (bool) $this->mollieProfileStorage
->load($id);
}
}