You are here

function NodeTerms::findFieldToCascadeTerms in Taxonomy Facets 8

Check node fields for Taxo Faceted fields.

Check if this node have such a field, i.e taxonomy reference field to a given vocabulary. If yes cascade terms.

Parameters

$vid: Vocabulary id of a vocabulary against which we want to check for a field existence.

1 call to NodeTerms::findFieldToCascadeTerms()
NodeTerms::__construct in src/NodeTerms.php

File

src/NodeTerms.php, line 39

Class

NodeTerms

Namespace

Drupal\taxonomy_facets

Code

function findFieldToCascadeTerms($vid) {

  // Go trough all fields until you find a field referencing to this taxonomy.

  //kint($this->node->getFieldDefinitions());

  //exit();
  foreach ($this->node
    ->getFieldDefinitions() as $field_definition) {
    if (method_exists($field_definition, 'get')) {
      if ($field_definition
        ->get('field_type') === 'entity_reference') {
        $settings = $field_definition
          ->get('settings');
        if (isset($settings['handler_settings']['target_bundles']) && $vid === current($settings['handler_settings']['target_bundles'])) {

          // Field found, now cascade terms.
          $cardinality = $field_definition
            ->getFieldStorageDefinition()
            ->get('cardinality');
          if ($cardinality === -1) {
            $this
              ->cascadeTerms($field_definition
              ->get('field_name'));
          }
          else {

            //drupal_set_message('warning', t("Taxonomy reference field:) " . ));
            drupal_set_message(t('The entity reference filed: @fieldName, that is a
                  reference to Taxonomy: @account, has not been set as mutivalue
                  field. Please change definition of the field, set "Allowed number of values" to "unlimited".
                  Alternatively change the setting of the Taxonomy Faceted search, deselect "CASCADE TERMS"
                  checkbox for this vocabulary in Administration >> Configuration >> Taxonomy Facets configuration', [
              '@fieldName' => $field_definition
                ->get('field_name'),
              '@account' => $vid,
            ]), 'warning');
          }
        }
      }
    }
  }
}