Loading js/custom/tree.js +8 −5 Original line number Diff line number Diff line Loading @@ -9,7 +9,9 @@ var treeSettings = settings.taxonomy_manager.tree || []; if (treeSettings instanceof Array) { for (var i=0; i<treeSettings.length; i++) { $("#"+ treeSettings[i].id).once('taxonomy-manager-tree').each(function() { new Drupal.TaxonomyManagerFancyTree(treeSettings[i].id, treeSettings[i].name, treeSettings[i].source); }); } } } Loading Loading @@ -54,10 +56,11 @@ }; }, source: source, }); $("#"+ id).closest('form').submit(function() { // Render hidden <input> elements for active and selected nodes $("#"+ id).fancytree("getTree").generateFormElements(name + '[]'); select: function(event, data) { // We update the the form inputs on every checkbox state change as // ajax events might require the latest state. data.tree.generateFormElements(name + '[]'); } }); } Loading src/Form/AddTermsToVocabularyForm.php +16 −58 Original line number Diff line number Diff line Loading @@ -22,38 +22,19 @@ class AddTermsToVocabularyForm extends FormBase { * @param \Drupal\taxonomy\VocabularyInterface $vocabulary * @return array */ public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $vocabulary = NULL) { $form['voc'] = array('#type' => 'value', "#value" => $vocabulary); $form['#attached']['library'][] = 'taxonomy_manager/taxonomy_manager.css'; $attributes = array(); if ($hide_form) { $form['#attached']['js'][] = array( 'data' => array('hideForm' => array(array( 'show_button' => 'edit-add-show', 'hide_button' => 'edit-add-cancel', 'div' => 'edit-add'))), 'type' => 'setting'); $attributes = array('style' => 'display:none;', 'id' => 'edit-add'); $form['toolbar']['add_show'] = array( //'#type' => 'button', '#attributes' => array('class' => 'taxonomy-manager-buttons add'), '#value' => $this->t('Add'), '#theme' => 'no_submit_button', ); public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $parents = array()) { $form['voc'] = array('#type' => 'value', '#value' => $taxonomy_vocabulary); $form['parents']['#tree'] = TRUE; foreach ($parents as $p) { $form['parents'][$p] = array('#type' => 'value', '#value' => $p); } $description = $this->t("If you have selected one or more terms in the tree view, the new terms are automatically children of those."); $form['add'] = array( '#type' => 'fieldset', '#tree' => TRUE, '#attributes' => $attributes, '#title' => $this->t('Add new terms'), '#description' => $description, $form['help'] = array( '#markup' => $description, ); $form['add']['mass_add'] = array( $form['mass_add'] = array( '#type' => 'textarea', '#title' => $this->t('Terms'), '#description' => $this->t("One term per line. Child terms can be prefixed with a Loading @@ -67,30 +48,15 @@ class AddTermsToVocabularyForm extends FormBase { -feline<br /> --cat"), '#rows' => 10, '#required' => TRUE, ); $form['add']['add'] = array( $form['add'] = array( '#type' => 'submit', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'add')), '#value' => $this->t('Add'), ); $form['add']['cancel'] = array( '#type' => 'button', '#value' => $this->t('Cancel'), '#theme' => 'no_submit_button', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'cancel')), ); return $form; } public function validateForm(array &$form, FormStateInterface $form_state) { // Check if form is empty. $values = $form_state->getValues(); if (empty($values['add']['mass_add'])) { $form_state->setErrorByName('mass_add', $this->t('You must enter at least 1 term name.')); } } /** * Submit handler for adding terms. * @param array $form Loading @@ -100,29 +66,21 @@ class AddTermsToVocabularyForm extends FormBase { $term_names_too_long = array(); $term_names = array(); $selected_terms = $form_state->getValue(['taxonomy', 'manager', 'tree', 'selected_terms']); $parents = isset($selected_terms) ? $selected_terms : array(); $language = $form_state->getValue(['taxonomy', 'manager', 'top', 'language']); $lang = isset($language) ? $language : ""; $taxonomy_vocabulary = $form_state->getValue('voc'); $parents = $form_state->getValue('parents'); $mass_terms = $form_state->getValue('mass_add'); $mass_terms = $form_state->getValue(['add', 'mass_add']); $vocabulary = $form_state->getValue(['voc']); $new_terms = TaxonomyManagerHelper::mass_add_terms($mass_terms, $vocabulary->id(), $parents, $lang, $term_names_too_long); $new_terms = TaxonomyManagerHelper::mass_add_terms($mass_terms, $taxonomy_vocabulary->id(), $parents, $term_names_too_long); foreach ($new_terms as $term) { $term_names[] = $term->label(); } if (\Drupal::moduleHandler()->moduleExists('i18n_taxonomy') && !empty($lang) && i18n_taxonomy_vocabulary_mode($vocabulary->id(), I18N_MODE_TRANSLATE)) { drupal_set_message($this->t("Saving terms to language @lang", array('@lang' => locale_language_name($language)))); } if (count($term_names_too_long)) { drupal_set_message($this->t("Following term names were too long and truncated to 255 characters: %names.", array('%names' => implode(', ', $term_names_too_long))), 'warning'); } drupal_set_message($this->t("Terms added: %terms", array('%terms' => implode(', ', $term_names)))); $form_state->setRedirect('taxonomy_manager.admin_vocabulary', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); } public function getFormId() { Loading src/Form/TaxonomyManagerForm.php +56 −16 Original line number Diff line number Diff line Loading @@ -7,10 +7,13 @@ namespace Drupal\taxonomy_manager\Form; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\OpenModalDialogCommand; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\taxonomy\VocabularyInterface; use Drupal\taxonomy_manager\TaxonomyManagerHelper; Loading Loading @@ -72,7 +75,10 @@ class TaxonomyManagerForm extends FormBase { */ public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) { $form['voc'] = array('#type' => 'value', "#value" => $taxonomy_vocabulary); $form['#attached']['library'][] = 'taxonomy_manager/taxonomy_manager.css'; $form['#attached']['library'] = array( 'core/drupal.dialog.ajax', 'taxonomy_manager/taxonomy_manager.css', ); if (TaxonomyManagerHelper::_taxonomy_manager_voc_is_empty($taxonomy_vocabulary->id())) { $form['text'] = array( Loading @@ -83,11 +89,7 @@ class TaxonomyManagerForm extends FormBase { } /* Toolbar. */ /*$form['toolbar'] = array( '#type' => 'fieldset', '#title' => $this->t('Toolbar'), ); /* $form['toolbar']['weight_up'] = array( '#type' => 'button', '#attributes' => array('class' => array('taxonomy-manager-buttons')), Loading @@ -103,13 +105,6 @@ class TaxonomyManagerForm extends FormBase { '#theme' => 'no_submit_button', ); $form['toolbar']['add_show'] = array( '#type' => 'button', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'add')), '#value' => $this->t('Add'), '#theme' => 'no_submit_button', ); $form['toolbar']['delete_confirm'] = array( '#type' => 'button', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'delete')), Loading Loading @@ -152,6 +147,26 @@ class TaxonomyManagerForm extends FormBase { '#prefix' => '</div>', );*/ //$url = $this->url('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); //$url = Url::fromRoute('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); //dsm($url); //$form['#action'] = $url; $form['toolbar'] = array( '#type' => 'fieldset', '#title' => $this->t('Toolbar'), ); $form['toolbar']['add'] = array( '#type' => 'submit', '#name' => 'add', '#value' => $this->t('Add'), '#ajax' => array( 'callback' => array($this, 'addFromCallback'), ), ); /* Taxonomy manager. */ $form['taxonomy']['#tree'] = TRUE; Loading Loading @@ -190,13 +205,12 @@ class TaxonomyManagerForm extends FormBase { $form['taxonomy']['manager']['pager'] = array('#type' => 'pager'); /*$form['submit'] = array( $form['submit'] = array( '#type' => 'submit', '#value' => 'Submit', );*/ ); return $form; } public function validateForm(array &$form, FormStateInterface $form_state) { Loading @@ -204,6 +218,32 @@ class TaxonomyManagerForm extends FormBase { } public function submitForm(array &$form, FormStateInterface $form_state) { $selected_terms = $form_state->getValue(['taxonomy', 'manager', 'tree']); //dsm($selected_terms); } /** * AJAX callback handler for add form. */ public function addFromCallback($form, FormStateInterface $form_state) { $taxonomy_vocabulary = $form_state->getValue('voc'); $selected_terms = $form_state->getValue(['taxonomy', 'manager', 'tree']); // @todo Passing the selected terms does not work yet, as // AddTermsToVocabularyForm::buildForm is called twice and the second time // it missing the selected terms parameter. $add_form = \Drupal::formBuilder()->getForm('Drupal\taxonomy_manager\Form\AddTermsToVocabularyForm', $taxonomy_vocabulary, $selected_terms); $add_form['#attached']['library'][] = 'core/drupal.dialog.ajax'; // Change the form action url form the current site to the add form. // @todo $this->url ended in an exception... $add_form['#action'] = '/admin/structure/taxonomy_manager/voc/' . $taxonomy_vocabulary->id() . '/add'; //$add_form['#action'] = $this->url('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); //Url::fromRoute('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); $response = new AjaxResponse(); $response->addCommand(new OpenModalDialogCommand($this->t('Add terms'), $add_form, array('width' => '700'))); return $response; } } src/TaxonomyManagerHelper.php +2 −4 Original line number Diff line number Diff line Loading @@ -28,15 +28,13 @@ class TaxonomyManagerHelper { * The vocabulary id. * @param int $parents * An array of parent term ids for the new inserted terms. Can be 0. * @param $lang * The i18n language, if i18n exists. * @param $term_names_too_long * Return value that is used to indicate that some term names were too long * and truncated to 255 characters. * * @return An array of the newly inserted term objects */ public static function mass_add_terms($input, $vid, $parents, $lang = "", &$term_names_too_long = array()) { public static function mass_add_terms($input, $vid, $parents, &$term_names_too_long = array()) { $new_terms = array(); $terms = explode("\n", str_replace("\r", '', $input)); $parents = count($parents) ? $parents : 0; Loading taxonomy_manager.routing.yml +8 −0 Original line number Diff line number Diff line Loading @@ -29,3 +29,11 @@ taxonomy_manager.subTree: _title: 'Sub Tree' requirements: _permission: 'administer taxonomy' taxonomy_manager.admin_vocabulary.add: path: '/admin/structure/taxonomy_manager/voc/{taxonomy_vocabulary}/add' defaults: _form: '\Drupal\taxonomy_manager\Form\AddTermsToVocabularyForm' _title: 'Add terms' requirements: _permission: 'administer taxonomy' Loading
js/custom/tree.js +8 −5 Original line number Diff line number Diff line Loading @@ -9,7 +9,9 @@ var treeSettings = settings.taxonomy_manager.tree || []; if (treeSettings instanceof Array) { for (var i=0; i<treeSettings.length; i++) { $("#"+ treeSettings[i].id).once('taxonomy-manager-tree').each(function() { new Drupal.TaxonomyManagerFancyTree(treeSettings[i].id, treeSettings[i].name, treeSettings[i].source); }); } } } Loading Loading @@ -54,10 +56,11 @@ }; }, source: source, }); $("#"+ id).closest('form').submit(function() { // Render hidden <input> elements for active and selected nodes $("#"+ id).fancytree("getTree").generateFormElements(name + '[]'); select: function(event, data) { // We update the the form inputs on every checkbox state change as // ajax events might require the latest state. data.tree.generateFormElements(name + '[]'); } }); } Loading
src/Form/AddTermsToVocabularyForm.php +16 −58 Original line number Diff line number Diff line Loading @@ -22,38 +22,19 @@ class AddTermsToVocabularyForm extends FormBase { * @param \Drupal\taxonomy\VocabularyInterface $vocabulary * @return array */ public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $vocabulary = NULL) { $form['voc'] = array('#type' => 'value', "#value" => $vocabulary); $form['#attached']['library'][] = 'taxonomy_manager/taxonomy_manager.css'; $attributes = array(); if ($hide_form) { $form['#attached']['js'][] = array( 'data' => array('hideForm' => array(array( 'show_button' => 'edit-add-show', 'hide_button' => 'edit-add-cancel', 'div' => 'edit-add'))), 'type' => 'setting'); $attributes = array('style' => 'display:none;', 'id' => 'edit-add'); $form['toolbar']['add_show'] = array( //'#type' => 'button', '#attributes' => array('class' => 'taxonomy-manager-buttons add'), '#value' => $this->t('Add'), '#theme' => 'no_submit_button', ); public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $parents = array()) { $form['voc'] = array('#type' => 'value', '#value' => $taxonomy_vocabulary); $form['parents']['#tree'] = TRUE; foreach ($parents as $p) { $form['parents'][$p] = array('#type' => 'value', '#value' => $p); } $description = $this->t("If you have selected one or more terms in the tree view, the new terms are automatically children of those."); $form['add'] = array( '#type' => 'fieldset', '#tree' => TRUE, '#attributes' => $attributes, '#title' => $this->t('Add new terms'), '#description' => $description, $form['help'] = array( '#markup' => $description, ); $form['add']['mass_add'] = array( $form['mass_add'] = array( '#type' => 'textarea', '#title' => $this->t('Terms'), '#description' => $this->t("One term per line. Child terms can be prefixed with a Loading @@ -67,30 +48,15 @@ class AddTermsToVocabularyForm extends FormBase { -feline<br /> --cat"), '#rows' => 10, '#required' => TRUE, ); $form['add']['add'] = array( $form['add'] = array( '#type' => 'submit', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'add')), '#value' => $this->t('Add'), ); $form['add']['cancel'] = array( '#type' => 'button', '#value' => $this->t('Cancel'), '#theme' => 'no_submit_button', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'cancel')), ); return $form; } public function validateForm(array &$form, FormStateInterface $form_state) { // Check if form is empty. $values = $form_state->getValues(); if (empty($values['add']['mass_add'])) { $form_state->setErrorByName('mass_add', $this->t('You must enter at least 1 term name.')); } } /** * Submit handler for adding terms. * @param array $form Loading @@ -100,29 +66,21 @@ class AddTermsToVocabularyForm extends FormBase { $term_names_too_long = array(); $term_names = array(); $selected_terms = $form_state->getValue(['taxonomy', 'manager', 'tree', 'selected_terms']); $parents = isset($selected_terms) ? $selected_terms : array(); $language = $form_state->getValue(['taxonomy', 'manager', 'top', 'language']); $lang = isset($language) ? $language : ""; $taxonomy_vocabulary = $form_state->getValue('voc'); $parents = $form_state->getValue('parents'); $mass_terms = $form_state->getValue('mass_add'); $mass_terms = $form_state->getValue(['add', 'mass_add']); $vocabulary = $form_state->getValue(['voc']); $new_terms = TaxonomyManagerHelper::mass_add_terms($mass_terms, $vocabulary->id(), $parents, $lang, $term_names_too_long); $new_terms = TaxonomyManagerHelper::mass_add_terms($mass_terms, $taxonomy_vocabulary->id(), $parents, $term_names_too_long); foreach ($new_terms as $term) { $term_names[] = $term->label(); } if (\Drupal::moduleHandler()->moduleExists('i18n_taxonomy') && !empty($lang) && i18n_taxonomy_vocabulary_mode($vocabulary->id(), I18N_MODE_TRANSLATE)) { drupal_set_message($this->t("Saving terms to language @lang", array('@lang' => locale_language_name($language)))); } if (count($term_names_too_long)) { drupal_set_message($this->t("Following term names were too long and truncated to 255 characters: %names.", array('%names' => implode(', ', $term_names_too_long))), 'warning'); } drupal_set_message($this->t("Terms added: %terms", array('%terms' => implode(', ', $term_names)))); $form_state->setRedirect('taxonomy_manager.admin_vocabulary', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); } public function getFormId() { Loading
src/Form/TaxonomyManagerForm.php +56 −16 Original line number Diff line number Diff line Loading @@ -7,10 +7,13 @@ namespace Drupal\taxonomy_manager\Form; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\OpenModalDialogCommand; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\taxonomy\VocabularyInterface; use Drupal\taxonomy_manager\TaxonomyManagerHelper; Loading Loading @@ -72,7 +75,10 @@ class TaxonomyManagerForm extends FormBase { */ public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) { $form['voc'] = array('#type' => 'value', "#value" => $taxonomy_vocabulary); $form['#attached']['library'][] = 'taxonomy_manager/taxonomy_manager.css'; $form['#attached']['library'] = array( 'core/drupal.dialog.ajax', 'taxonomy_manager/taxonomy_manager.css', ); if (TaxonomyManagerHelper::_taxonomy_manager_voc_is_empty($taxonomy_vocabulary->id())) { $form['text'] = array( Loading @@ -83,11 +89,7 @@ class TaxonomyManagerForm extends FormBase { } /* Toolbar. */ /*$form['toolbar'] = array( '#type' => 'fieldset', '#title' => $this->t('Toolbar'), ); /* $form['toolbar']['weight_up'] = array( '#type' => 'button', '#attributes' => array('class' => array('taxonomy-manager-buttons')), Loading @@ -103,13 +105,6 @@ class TaxonomyManagerForm extends FormBase { '#theme' => 'no_submit_button', ); $form['toolbar']['add_show'] = array( '#type' => 'button', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'add')), '#value' => $this->t('Add'), '#theme' => 'no_submit_button', ); $form['toolbar']['delete_confirm'] = array( '#type' => 'button', '#attributes' => array('class' => array('taxonomy-manager-buttons', 'delete')), Loading Loading @@ -152,6 +147,26 @@ class TaxonomyManagerForm extends FormBase { '#prefix' => '</div>', );*/ //$url = $this->url('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); //$url = Url::fromRoute('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); //dsm($url); //$form['#action'] = $url; $form['toolbar'] = array( '#type' => 'fieldset', '#title' => $this->t('Toolbar'), ); $form['toolbar']['add'] = array( '#type' => 'submit', '#name' => 'add', '#value' => $this->t('Add'), '#ajax' => array( 'callback' => array($this, 'addFromCallback'), ), ); /* Taxonomy manager. */ $form['taxonomy']['#tree'] = TRUE; Loading Loading @@ -190,13 +205,12 @@ class TaxonomyManagerForm extends FormBase { $form['taxonomy']['manager']['pager'] = array('#type' => 'pager'); /*$form['submit'] = array( $form['submit'] = array( '#type' => 'submit', '#value' => 'Submit', );*/ ); return $form; } public function validateForm(array &$form, FormStateInterface $form_state) { Loading @@ -204,6 +218,32 @@ class TaxonomyManagerForm extends FormBase { } public function submitForm(array &$form, FormStateInterface $form_state) { $selected_terms = $form_state->getValue(['taxonomy', 'manager', 'tree']); //dsm($selected_terms); } /** * AJAX callback handler for add form. */ public function addFromCallback($form, FormStateInterface $form_state) { $taxonomy_vocabulary = $form_state->getValue('voc'); $selected_terms = $form_state->getValue(['taxonomy', 'manager', 'tree']); // @todo Passing the selected terms does not work yet, as // AddTermsToVocabularyForm::buildForm is called twice and the second time // it missing the selected terms parameter. $add_form = \Drupal::formBuilder()->getForm('Drupal\taxonomy_manager\Form\AddTermsToVocabularyForm', $taxonomy_vocabulary, $selected_terms); $add_form['#attached']['library'][] = 'core/drupal.dialog.ajax'; // Change the form action url form the current site to the add form. // @todo $this->url ended in an exception... $add_form['#action'] = '/admin/structure/taxonomy_manager/voc/' . $taxonomy_vocabulary->id() . '/add'; //$add_form['#action'] = $this->url('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); //Url::fromRoute('taxonomy_manager.admin_vocabulary.add', array('taxonomy_vocabulary' => $taxonomy_vocabulary->id())); $response = new AjaxResponse(); $response->addCommand(new OpenModalDialogCommand($this->t('Add terms'), $add_form, array('width' => '700'))); return $response; } }
src/TaxonomyManagerHelper.php +2 −4 Original line number Diff line number Diff line Loading @@ -28,15 +28,13 @@ class TaxonomyManagerHelper { * The vocabulary id. * @param int $parents * An array of parent term ids for the new inserted terms. Can be 0. * @param $lang * The i18n language, if i18n exists. * @param $term_names_too_long * Return value that is used to indicate that some term names were too long * and truncated to 255 characters. * * @return An array of the newly inserted term objects */ public static function mass_add_terms($input, $vid, $parents, $lang = "", &$term_names_too_long = array()) { public static function mass_add_terms($input, $vid, $parents, &$term_names_too_long = array()) { $new_terms = array(); $terms = explode("\n", str_replace("\r", '', $input)); $parents = count($parents) ? $parents : 0; Loading
taxonomy_manager.routing.yml +8 −0 Original line number Diff line number Diff line Loading @@ -29,3 +29,11 @@ taxonomy_manager.subTree: _title: 'Sub Tree' requirements: _permission: 'administer taxonomy' taxonomy_manager.admin_vocabulary.add: path: '/admin/structure/taxonomy_manager/voc/{taxonomy_vocabulary}/add' defaults: _form: '\Drupal\taxonomy_manager\Form\AddTermsToVocabularyForm' _title: 'Add terms' requirements: _permission: 'administer taxonomy'