Newer
Older
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\taxonomy\Form\OverviewTerms;
/**
* Taxonomy overview form class.
*/
class HmOverviewTerms extends OverviewTerms {
/**
* Form constructor.
*
* Override the form submit method to avoid the parent one from running,
* If the hierarchy manager taxonomy plugin is enabled.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary
* The vocabulary to display the overview form for.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
// Override the form if the taxonomy hierarchy manager has been set up.
if (!empty($taxonomy_vocabulary) && $config = \Drupal::config('hierarchy_manager.hmconfig')) {
if ($allowed_setup_plugins = $config->get('allowed_setup_plugins')) {
// If the taxonomy setup plugin is enabled.
// Override the taxonomy overview form.
if (!empty($allowed_setup_plugins['hm_setup_taxonomy'])) {
// Hierarchy Manager setup plugin configuration.
$plugin_settings = $config->get('setup_plugin_settings');
if (!empty($plugin_settings['hm_setup_taxonomy'])) {
// Enabled bundles.
$enabled_bundles = array_keys(array_filter($plugin_settings['hm_setup_taxonomy']['bundle']));
// Display profile ID.
$display_profile_id = $plugin_settings['hm_setup_taxonomy']['display_profile'];
// Display profile.
$display_profile = $this->entityTypeManager->getStorage('hm_display_profile')->load($display_profile_id);
if (!empty($display_profile) && in_array($taxonomy_vocabulary->id(), $enabled_bundles)) {
// Display plugin instance.
$instance = \Drupal::service('plugin.manager.hm.display_plugin')->createInstance($display_profile->get("plugin"));
if (method_exists($instance, 'getForm')) {
// Vocabulary ID.
$vid = $taxonomy_vocabulary->id();
// CSRF token.
$token = \Drupal::csrfToken()->get($vid);
// Destination for edit link.
$destination = $this->getDestinationArray();
if (isset($destination['destination'])) {
$destination = $destination['destination'];
// Urls.
$source_url = Url::fromRoute('hierarchy_manager.taxonomy.tree.json', ['vid' => $vid], ['query' => ['token' => $token, 'destination' => $destination]])->toString();
$update_url = Url::fromRoute('hierarchy_manager.taxonomy.tree.update', ['vid' => $vid], ['query' => ['token' => $token]])->toString();
$config = $display_profile->get("config");
$confirm = $display_profile->get('confirm');
return $instance->getForm($source_url, $update_url, $form, $form_state, $config, $confirm);
}
}
}
}
}
}
// The taxonomy setup plugin is not enabled.
return parent::buildForm($form, $form_state, $taxonomy_vocabulary);
}
/**
* Form submission handler.
*
* Override the form submit method to avoid the parent one from running,
* If the hierarchy manager taxonomy plugin is enabled.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {

Mingsong Hu
committed
// Override the form if the taxonomy hierarchy manager has been set up.
if ($config = \Drupal::config('hierarchy_manager.hmconfig')) {
if ($allowed_setup_plugins = $config->get('allowed_setup_plugins')) {
// If the taxonomy setup plugin is enabled,
// override the submitForm function.
if (!empty($allowed_setup_plugins['hm_setup_taxonomy'])) {

artis.bajars
committed
$plugin_settings = $config->get('setup_plugin_settings');
$enabled_bundles = array_keys(array_filter($plugin_settings['hm_setup_taxonomy']['bundle']));
$vocabulary = $form_state->get(['taxonomy', 'vocabulary']);
if (in_array($vocabulary->id(), $enabled_bundles)) {
// We don't need to do anything here,
// as the taxonomy plugin take it over.
return;
}

Mingsong Hu
committed
}
}
}

Mingsong Hu
committed
// The taxonomy setup plugin is not enabled.
// Let the submitForm function from core handle this request.
return parent::submitForm($form, $form_state);