You are here

protected function AddHierarchy::findHierarchicalProperties in Search API 8

Finds all hierarchical properties nested on an entity-typed property.

Parameters

\Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface $property: The property to be searched for hierarchical nested properties.

string $property_label: The property's label.

Return value

string[] An options list of hierarchical properties, keyed by the parent property's entity type ID and the nested properties identifier, concatenated with a dash (-).

1 call to AddHierarchy::findHierarchicalProperties()
AddHierarchy::getHierarchyFields in src/Plugin/search_api/processor/AddHierarchy.php
Finds all (potentially) hierarchical fields for this processor's index.

File

src/Plugin/search_api/processor/AddHierarchy.php, line 163

Class

AddHierarchy
Adds all ancestors' IDs to a hierarchical field.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

protected function findHierarchicalProperties(EntityDataDefinitionInterface $property, $property_label) {
  $entity_type_id = $property
    ->getEntityTypeId();
  $property_label = Utility::escapeHtml($property_label);
  $options = [];

  // Check properties for potential hierarchy. Check two levels down, since
  // Core's entity references all have an additional "entity" sub-property for
  // accessing the actual entity reference, which we'd otherwise miss.
  foreach ($this
    ->getFieldsHelper()
    ->getNestedProperties($property) as $name_2 => $property_2) {
    $property_2_label = $property_2
      ->getLabel();
    $property_2 = $this
      ->getFieldsHelper()
      ->getInnerProperty($property_2);
    $is_reference = FALSE;
    if ($property_2 instanceof EntityDataDefinitionInterface) {
      if ($property_2
        ->getEntityTypeId() == $entity_type_id) {
        $is_reference = TRUE;
      }
    }
    elseif ($property_2 instanceof ComplexDataDefinitionInterface) {
      foreach ($property_2
        ->getPropertyDefinitions() as $property_3) {
        $property_3 = $this
          ->getFieldsHelper()
          ->getInnerProperty($property_3);
        if ($property_3 instanceof EntityDataDefinitionInterface) {
          if ($property_3
            ->getEntityTypeId() == $entity_type_id) {
            $is_reference = TRUE;
            break;
          }
        }
      }
    }
    if ($is_reference) {
      $property_2_label = Utility::escapeHtml($property_2_label);
      $options["{$entity_type_id}-{$name_2}"] = $property_label . ' » ' . $property_2_label;
    }
  }
  return $options;
}