function TermForm::form
Same name in other branches
- 9 core/modules/taxonomy/src/TermForm.php \Drupal\taxonomy\TermForm::form()
- 8.9.x core/modules/taxonomy/src/TermForm.php \Drupal\taxonomy\TermForm::form()
- 10 core/modules/taxonomy/src/TermForm.php \Drupal\taxonomy\TermForm::form()
Overrides ContentEntityForm::form
1 call to TermForm::form()
- ForumForm::form in core/
modules/ forum/ src/ Form/ ForumForm.php - Gets the actual form array to be built.
1 method overrides TermForm::form()
- ForumForm::form in core/
modules/ forum/ src/ Form/ ForumForm.php - Gets the actual form array to be built.
File
-
core/
modules/ taxonomy/ src/ TermForm.php, line 19
Class
- TermForm
- Base for handler for taxonomy term edit forms.
Namespace
Drupal\taxonomyCode
public function form(array $form, FormStateInterface $form_state) {
$term = $this->entity;
$vocab_storage = $this->entityTypeManager
->getStorage('taxonomy_vocabulary');
/** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_storage */
$taxonomy_storage = $this->entityTypeManager
->getStorage('taxonomy_term');
$vocabulary = $vocab_storage->load($term->bundle());
$parent = $this->getParentIds($term);
$form_state->set([
'taxonomy',
'parent',
], $parent);
$form_state->set([
'taxonomy',
'vocabulary',
], $vocabulary);
$form['relations'] = [
'#type' => 'details',
'#title' => $this->t('Relations'),
'#open' => $taxonomy_storage->getVocabularyHierarchyType($vocabulary->id()) == VocabularyInterface::HIERARCHY_MULTIPLE,
'#weight' => 10,
];
// \Drupal\taxonomy\TermStorageInterface::loadTree() and
// \Drupal\taxonomy\TermStorageInterface::loadParents() may contain large
// numbers of items so we check for taxonomy.settings:override_selector
// before loading the full vocabulary. Contrib modules can then intercept
// before hook_form_alter to provide scalable alternatives.
if (!$this->config('taxonomy.settings')
->get('override_selector')) {
$exclude = [];
if (!$term->isNew()) {
$children = $taxonomy_storage->loadTree($vocabulary->id(), $term->id());
// A term can't be the child of itself, nor of its children.
foreach ($children as $child) {
$exclude[] = $child->tid;
}
$exclude[] = $term->id();
}
$tree = $taxonomy_storage->loadTree($vocabulary->id());
$options = [
'<' . $this->t('root') . '>',
];
if (empty($parent)) {
$parent = [
0,
];
}
foreach ($tree as $item) {
if (!in_array($item->tid, $exclude)) {
$options[$item->tid] = str_repeat('-', $item->depth) . $item->name;
}
}
}
else {
$options = [
'<' . $this->t('root') . '>',
];
$parent = [
0,
];
}
if ($this->getRequest()->query
->has('parent')) {
$parent = array_values(array_intersect(array_keys($options), (array) $this->getRequest()->query
->all()['parent']));
}
$form['relations']['parent'] = [
'#type' => 'select',
'#title' => $this->t('Parent terms'),
'#options' => $options,
'#default_value' => $parent,
'#multiple' => TRUE,
];
$form['relations']['weight'] = [
'#type' => 'textfield',
'#title' => $this->t('Weight'),
'#size' => 6,
'#default_value' => $term->getWeight(),
'#description' => $this->t('Terms are displayed in ascending order by weight.'),
'#required' => TRUE,
];
$form['vid'] = [
'#type' => 'value',
'#value' => $vocabulary->id(),
];
$form['tid'] = [
'#type' => 'value',
'#value' => $term->id(),
];
return parent::form($form, $form_state);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.