Skip to content
Snippets Groups Projects
Commit 2ff8ddb2 authored by Alex Goh's avatar Alex Goh Committed by Andreas Koepke
Browse files

Issue #2356131 by alexgkt, akoepke: Support for entityreference module

parent abd685be
Branches 7.x-1.x
Tags 7.x-1.1
No related merge requests found
......@@ -56,6 +56,14 @@ function taxonomy_set_lineage_config_form(array $form, array &$form_state) {
}
$taxonomy_fields = field_read_fields(array('type' => 'taxonomy_term_reference'));
$entity_reference_fields = field_read_fields(array('type' => 'entityreference'));
foreach ($entity_reference_fields as $field_name => $info) {
if ($info['settings']['target_type'] === 'taxonomy_term') {
$taxonomy_fields[$field_name] = $info;
}
}
foreach ($taxonomy_fields as $field_name => $info) {
$base_info = field_info_field($field_name);
......
......@@ -109,6 +109,16 @@ function _taxonomy_set_lineage_vocab_to_instance_field_name($entity, $entity_typ
return $field['field_name'];
}
}
if ($field['type'] === 'entityreference') {
if ($field['settings']['target_type'] === 'taxonomy_term') {
$bundles = array_keys($field['settings']['handler_settings']['target_bundles']);
$found_vocab_machine_name = $bundles[0];
// If the vocab we are looking for is found in field, return field name.
if ($found_vocab_machine_name === $taxonomy_machine_name) {
return $field['field_name'];
}
}
}
}
}
......@@ -136,12 +146,13 @@ function _taxonomy_set_lineage_field_values_updated($entity, $tax_field_name) {
// Is there something to do?
if (isset($entity->{$tax_field_name}) && is_array($entity->{$tax_field_name})) {
foreach ($entity->{$tax_field_name} as $lang_code => $values) {
$key = isset($values[0]['target_id']) ? 'target_id' : 'tid';
// Two loops to check for changes. First the new state.
$tids = array_column($values, 'tid');
$tids = array_column($values, $key);
// Now the old version of the entity.
$original_tids = array();
if (isset($entity->original->{$tax_field_name}[$lang_code])) {
$original_tids = array_column($entity->original->{$tax_field_name}[$lang_code], 'tid');
$original_tids = array_column($entity->original->{$tax_field_name}[$lang_code], $key);
}
// Are there changes?
asort($tids);
......@@ -178,19 +189,20 @@ function _taxonomy_set_lineage_field_values_updated($entity, $tax_field_name) {
function _taxonomy_set_lineage_process_field($entity, $tax_field_name) {
if (isset($entity->{$tax_field_name}) && is_array($entity->{$tax_field_name})) {
foreach ($entity->{$tax_field_name} as $lang_code => $values) {
$tids = array_column($values, 'tid');
$key = isset($values[0]['target_id']) ? 'target_id' : 'tid';
$tids = array_column($values, $key);
$new_tids = $tids;
foreach ($new_tids as $tid) {
// Get full ancestry of term which includes child term.
$parent_term_objects = taxonomy_get_parents_all($tid);
if (count($parent_term_objects) > 1) {
// Find delta of child TID.
$delta = array_search($tid, array_column($values, 'tid'));
$delta = array_search($tid, array_column($values, $key));
$new_values = [];
// Reverse parents so we can add them in the correct order.
foreach (array_reverse($parent_term_objects, TRUE) as $parent) {
if (!in_array($parent->tid, $tids)) {
$new_values[] = ['tid' => $parent->tid];
$new_values[] = [$key => $parent->tid];
}
}
array_splice($values, $delta, 0, $new_values);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment