EditFolderForm.php in Taxonomy File Tree 3.x
File
src/Form/EditFolderForm.php
View source
<?php
namespace Drupal\tft\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
class EditFolderForm extends FormBase {
public function getFormId() {
return 'tft_edit_term_form';
}
public function buildForm(array $form, FormStateInterface $form_state, TermInterface $taxonomy_term = NULL) {
$name = $taxonomy_term
->getName();
$form['name'] = [
'#type' => 'textfield',
'#title' => t("Name"),
'#required' => TRUE,
'#default_value' => $name,
'#weight' => -10,
];
$form['tid'] = [
'#type' => 'hidden',
'#value' => $taxonomy_term
->id(),
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
$cancel_uri = str_replace('%23', '#', $_GET['destination']);
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t("cancel"),
'#url' => Url::fromUri('internal:' . $cancel_uri),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (strpos($form_state
->getValue('name'), ',') !== FALSE || strpos($form_state
->getValue('name'), '+') !== FALSE) {
$form_state
->setErrorByName('name', $this
->t("The following characters are not allowed: ',' (comma) and +"));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$term = Term::load($form_state
->getValue('tid'));
$term
->setName($form_state
->getValue('name'));
$term
->save();
$this
->messenger()
->addMessage(t("The folder '@name' was updated.", [
'@name' => $form_state
->getValue('name'),
]));
}
}