0

I have a content type 'company' with a related tax-term: 'city'. With a view I can search for companies based on 'city' with autocomplete.

Is there a way I can relate adjacent cities into the same tax-term 'city'? If not, I would have to create a new tax-term into the content type 'company' and select for each company the adjacent cities. That is a lof of work.

Geo lookup is not an option, because we want te select which adjacent cities belongs to a specific city.

For example: Tax-term: city

City A
-- City B // adjacent city of 'city a'
-- City C // adjacent city of 'city a'
-- City D // adjacent city of 'city a'
City B
City C
City D

As you can see, City B, C and D exists in the tax-term but also are children of 'city A'. It should be an alias, because otherwise the children will have all a different ID, so that won't work.

Hope it is clear ...

6
  • 1
    Vocabularies aren't structured like that, you can't have terms that are aliases or shortcuts to other terms. What you're describing would usually be achieved using an entity reference field
    – Clive
    Commented Apr 18, 2018 at 9:22
  • First, I created an entity reference field. But I could not use this in the view ... then I asked my question here. Commented Apr 18, 2018 at 9:29
  • 1
    Regardless, you can't achieve what you've descried with taxonomy alone. All terms at any level of a vocabulary need to be real entities, not virtual
    – Clive
    Commented Apr 18, 2018 at 9:30
  • I understand, that were also my thoughts ... Commented Apr 18, 2018 at 11:37
  • If I use an entity reference field in the city-tax-term, and add those cities here. Could I collect those ID's with code and use these values as filter value in the view? Those ID's must be collected after hitting the search button... Commented Apr 18, 2018 at 11:45

1 Answer 1

0

I would like to share my own working solution. If you see any improvements, please correct me. That would be helpful. If not I hope this script can be helpful for someone else...

I've created my own module and used this function to alter the query:

<?php

use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\taxonomy\Entity\Term;

function search_cities_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
    // machine_name of view
    if ($view->id() == 'zoek_een_bedrijf') {
        foreach ($query->where as &$condition_group) {
            foreach ($condition_group['conditions'] as &$condition) {
                // check which field must be changed
                if ($condition['field'] == 'node__field_stad.field_stad_target_id = :node__field_stad_field_stad_target_id') {
                    $value = $condition['value'];
                    // get ID from taxonomy term
                    $city_id = $value[':node__field_stad_field_stad_target_id'];
                    // load taxonomy term info
                    $term = Term::load($city_id);
                    // load reference field with taxonomy term ID's
                    $term_field = $term->get('field_aangrenzende_gemeenten')->getValue();
                    $cities = array();
                    // add ID from parent taxonmy term
                    array_push($cities, $city_id);
                    // add taxonomy terms from reference field (children)
                    //print_r($term_field);
                    foreach ($term_field as $key => $value) {
                        array_push($cities, $value['target_id']);
                    }
                    // update the condition before sending query to database
                    $condition = array(
                        'field' => 'node__field_stad.field_stad_target_id',
                        'value' => $cities,
                        'operator' => 'IN',
                    );
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.