re-implemented tracklist to term_reference_tree
This commit is contained in:
@@ -78,7 +78,43 @@ class CheckboxTree extends FormElement {
|
||||
$tree = new \stdClass();
|
||||
$tree->children = $terms;
|
||||
unset($element['#needs_validation']);
|
||||
$element[] = _term_reference_tree_build_level($element, $tree, $form_state, $value, $element['#max_choices'], [], 1);
|
||||
$element['checkbox_tree'] = _term_reference_tree_build_level($element, $tree, $form_state, $value, $element['#max_choices'], [], 1);
|
||||
|
||||
// track list
|
||||
if ($element['#track_list']) {
|
||||
if($form_state->getTriggeringElement()){
|
||||
// we are on ajax
|
||||
// $value is not updated on ajax callback
|
||||
// we need an other way to get wich options are checked
|
||||
// and give an accurate track list
|
||||
|
||||
$parent = $element['#parents'][0]; // get the element's parent field
|
||||
$input = $form_state->getUserInput(); // get all inputs from form state
|
||||
$checkbox_tree_input = $input[$parent]['checkbox_tree']; // get the chekbox_tree input
|
||||
$selected = _term_reference_tree_get_flatten_selected_values($checkbox_tree_input); // get selected flattenized
|
||||
|
||||
$track_list_input = $input[$parent]['track_list']; // get the current track_list input with correct order
|
||||
$old_list = array_keys($track_list_input); // flattenize
|
||||
|
||||
// remove all chebox_tree input unselected from the list
|
||||
$list = [];
|
||||
foreach ($old_list as $id) {
|
||||
if(in_array($id, $selected)){
|
||||
$list[] = $id;
|
||||
}
|
||||
}
|
||||
// append newly selected
|
||||
$diff = array_diff($selected, $list);
|
||||
foreach ($diff as $id) {
|
||||
$list[] = $id;
|
||||
}
|
||||
}else{
|
||||
// if not ajax just get the default value
|
||||
$list = $value;
|
||||
}
|
||||
// build the track_list form element
|
||||
$element['track_list'] = _term_reference_tree_build_track_list_order($list, $element['#options']);
|
||||
}
|
||||
return $element;
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\term_reference_tree\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Component\Utility\NestedArray;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
@@ -37,6 +38,7 @@ class TermReferenceTree extends WidgetBase {
|
||||
'leaves_only' => FALSE,
|
||||
'select_parents' => FALSE,
|
||||
'cascading_selection' => self::CASCADING_SELECTION_NONE,
|
||||
'track_list' => FALSE,
|
||||
'max_depth' => 0,
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
@@ -94,6 +96,15 @@ class TermReferenceTree extends WidgetBase {
|
||||
$form['cascading_selection']['#description'] .= ' <em>' . $this->t("This option is only valid if an unlimited number of values can be selected.") . '</em>';
|
||||
}
|
||||
|
||||
$form['track_list'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Track list'),
|
||||
'#description' => $this->t('Track what the user has chosen in a list below the tree.
|
||||
Useful when the tree is large, with many levels.'),
|
||||
'#default_value' => $this->getSetting('track_list'),
|
||||
'#return_value' => 1,
|
||||
];
|
||||
|
||||
$form['max_depth'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Maximum Depth'),
|
||||
@@ -134,6 +145,10 @@ class TermReferenceTree extends WidgetBase {
|
||||
$summary[] = sprintf('%s (%s)', $this->t('Cascading selection'), $this->t('Only deselect'));
|
||||
}
|
||||
|
||||
if ($this->getSetting('track_list')) {
|
||||
$summary[] = $this->t('Track list');
|
||||
}
|
||||
|
||||
if ($this->getSetting('max_depth')) {
|
||||
$summary[] = $this->formatPlural($this->getSetting('max_depth'), 'Maximum Depth: @count level', 'Maximum Depth: @count levels');
|
||||
}
|
||||
@@ -156,6 +171,7 @@ class TermReferenceTree extends WidgetBase {
|
||||
$element['#leaves_only'] = $this->getSetting('leaves_only');
|
||||
$element['#select_parents'] = $this->getSetting('select_parents');
|
||||
$element['#cascading_selection'] = $this->getSetting('cascading_selection');
|
||||
$element['#track_list'] = $this->getSetting('track_list');
|
||||
$element['#value_key'] = 'target_id';
|
||||
$element['#max_depth'] = $this->getSetting('max_depth');
|
||||
$element['#start_minimized'] = $this->getSetting('start_minimized');
|
||||
@@ -189,6 +205,19 @@ class TermReferenceTree extends WidgetBase {
|
||||
array_push($value, [$element['#value_key'] => $child['#value']]);
|
||||
}
|
||||
}
|
||||
// if track_list enabled, reorder the value array regarding the tablegrag input
|
||||
if (!empty($value) && $element['#track_list']) {
|
||||
$input = &$form_state->getUserInput();
|
||||
$nested_input = NestedArray::getValue($form_state->getValues(), $element['#parents']);
|
||||
if (is_array($nested_input['track_list'])) {
|
||||
$track_list = array_keys($nested_input['track_list']);
|
||||
$sorted_value = [];
|
||||
foreach ($track_list as $id) {
|
||||
$sorted_value[] = $value[array_search($id, array_column($value, 'target_id'))];
|
||||
}
|
||||
$value = $sorted_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If it's a tree of radio buttons, they all have the same value,
|
||||
|
Reference in New Issue
Block a user