public function TaxonomyMenuForm::form in Taxonomy menu 8.3
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ TaxonomyMenuForm.php, line 59
Class
- TaxonomyMenuForm
- Class TaxonomyMenuForm.
Namespace
Drupal\taxonomy_menu\FormCode
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/* @var $taxonomy_menu \Drupal\taxonomy_menu\Entity\TaxonomyMenu */
$taxonomy_menu = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $taxonomy_menu
->label(),
'#description' => $this
->t("Label for the Taxonomy Menu."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $taxonomy_menu
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\taxonomy_menu\\Entity\\TaxonomyMenu::load',
],
'#disabled' => !$taxonomy_menu
->isNew(),
];
// Vocabulary selection.
$options = [];
$vocabulary_storage = $this->entityTypeManager
->getStorage('taxonomy_vocabulary');
foreach ($vocabulary_storage
->loadMultiple() as $vocabulary) {
$options[$vocabulary
->id()] = $vocabulary
->label();
}
$form['vocabulary'] = [
'#type' => 'select',
'#title' => $this
->t('Vocabulary'),
'#options' => $options,
'#default_value' => $taxonomy_menu
->getVocabulary(),
'#ajax' => [
'callback' => '::ajaxReplaceDescriptionFieldForm',
'wrapper' => 'description-field-container',
'method' => 'replace',
],
];
// Description field selection.
$form['description_container'] = [
'#type' => 'container',
'#prefix' => '<div id="description-field-container">',
'#suffix' => '</div>',
];
$selected_vocabulary = $taxonomy_menu
->getVocabulary();
if ($selected_vocabulary) {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions('taxonomy_term', $selected_vocabulary);
// Build a field options array.
$field_options = [
'' => $this
->t('none'),
];
if (count($field_definitions)) {
foreach ($field_definitions as $field_name => $field_definition) {
$field_options[$field_name] = $field_definition
->getName();
}
}
if (count($field_options)) {
$form['description_container']['description_field_name'] = [
'#type' => 'select',
'#title' => $this
->t('Description field'),
'#description' => $this
->t('Select the field to be used for the menu item description.'),
'#options' => $field_options,
'#default_value' => $taxonomy_menu
->getDescriptionFieldName(),
];
}
}
// Menu selection.
$options = [];
$menu_storage = $this->entityTypeManager
->getStorage('menu');
foreach ($menu_storage
->loadMultiple() as $menu) {
$options[$menu
->id()] = $menu
->label();
}
$form['menu'] = [
'#type' => 'select',
'#title' => $this
->t('Menu'),
'#options' => $options,
'#default_value' => $taxonomy_menu
->getMenu(),
];
$form['expanded'] = [
'#type' => 'checkbox',
'#title' => $this
->t('All menus entries expanded'),
'#default_value' => $taxonomy_menu->expanded,
];
$form['depth'] = [
'#type' => 'select',
'#title' => $this
->t('Depth'),
'#default_value' => $taxonomy_menu
->getDepth(),
'#options' => range(1, 9),
];
// Menu selection.
$custom_menus = Menu::loadMultiple();
foreach ($custom_menus as $menu_name => $menu) {
$custom_menus[$menu_name] = $menu
->label();
}
asort($custom_menus);
$available_menus = $custom_menus;
$menu_options = $this->menuParentSelector
->getParentSelectOptions(NULL, $available_menus);
$form['menu_parent'] = [
'#type' => 'select',
'#title' => $this
->t('Parent menu link'),
'#options' => $menu_options,
'#default_value' => $taxonomy_menu
->getMenuParent(),
];
// If this checkbox is active, use the term weight for the menu item order.
// Otherwise the menu items will be sorted alphabetically.
// The default is order by weight.
$form['use_term_weight_order'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use term weight order'),
'#default_value' => isset($taxonomy_menu->use_term_weight_order) ? $taxonomy_menu->use_term_weight_order : TRUE,
];
return $form;
}