diff --git a/src/Item/Field.php b/src/Item/Field.php index 5d8efaf..06f3abb 100644 --- a/src/Item/Field.php +++ b/src/Item/Field.php @@ -3,7 +3,6 @@ namespace Drupal\search_api\Item; use Drupal\Component\Render\FormattableMarkup; -use Drupal\Core\TypedData\ComplexDataDefinitionInterface; use Drupal\search_api\Entity\Index; use Drupal\search_api\IndexInterface; use Drupal\search_api\SearchApiException; @@ -347,7 +346,7 @@ public function setHidden($hidden = TRUE) { public function getDataDefinition() { if (!isset($this->dataDefinition)) { $definitions = $this->index->getPropertyDefinitions($this->getDatasourceId()); - $definition = $this->getNestedDefinition($definitions, explode(':', $this->getPropertyPath())); + $definition = Utility::retrieveNestedProperty($definitions, $this->getPropertyPath()); if (!$definition) { $args['%field'] = $this->getLabel(); $args['%index'] = $this->getIndex()->label(); @@ -359,34 +358,6 @@ public function getDataDefinition() { } /** - * Retrieves a nested property definition from an array of definitions. - * - * @param \Drupal\Core\TypedData\DataDefinitionInterface[] $properties - * The given array of base definitions. - * @param string[] $keys - * An array of keys to apply to the definitions to arrive at the one that - * should be returned. - * - * @return \Drupal\Core\TypedData\DataDefinitionInterface|null - * The requested property definition, or NULL if it could not be found. - */ - protected function getNestedDefinition(array $properties, array $keys) { - $key = array_shift($keys); - if (!isset($properties[$key])) { - return NULL; - } - $property = Utility::getInnerProperty($properties[$key]); - if (!$keys) { - return $property; - } - if (!$property instanceof ComplexDataDefinitionInterface) { - return NULL; - } - $nested_properties = Utility::getNestedProperties($property); - return $this->getNestedDefinition($nested_properties, $keys); - } - - /** * {@inheritdoc} */ public function getType() { diff --git a/src/Plugin/search_api/datasource/ContentEntity.php b/src/Plugin/search_api/datasource/ContentEntity.php index d3b8286..fc34a44 100644 --- a/src/Plugin/search_api/datasource/ContentEntity.php +++ b/src/Plugin/search_api/datasource/ContentEntity.php @@ -938,7 +938,7 @@ protected function getPropertyPathDependencies($property_path, array $properties } if (isset($nested_path) && $property instanceof ComplexDataDefinitionInterface) { - $nested_dependencies = $this->getPropertyPathDependencies($nested_path, $property->getPropertyDefinitions()); + $nested_dependencies = $this->getPropertyPathDependencies($nested_path, Utility::getNestedProperties($property)); foreach ($nested_dependencies as $type => $names) { $dependencies += array($type => array()); $dependencies[$type] += $names; diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index b028c07..9241521 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -835,15 +835,12 @@ protected function checkReferenceFieldsNonBaseFields() { $this->drupalPostForm(NULL, $edit, $this->t('Save and continue')); $this->drupalPostForm(NULL, ['cardinality' => -1], $this->t('Save field settings')); - $url_options['query']['datasource'] = 'entity:node'; + $url_options['query'] = array( + 'datasource' => 'entity:node', + 'property_path' => 'field__reference_field_:entity', + ); $this->drupalGet($this->getIndexPath('fields/add'), $url_options); - $link = $this->xpath('//*[@id="edit-expand-link--7"]')[0]; - $this->drupalGet($this->getAbsoluteUrl($link[0]['href'])); - - $link = $this->xpath('//*[@id="edit-expand-link--8"]')[0]; - $this->drupalGet($this->getAbsoluteUrl($link[0]['href'])); - // Check if the field is displayed to be indexed. $this->assertRaw('name="entity:node/field__reference_field_:entity:field__reference_field_"'); } diff --git a/src/Utility.php b/src/Utility.php index cbc831e..a9da0d1 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -302,6 +302,33 @@ public static function extractFieldValues(TypedDataInterface $data) { } /** + * Retrieves a list of nested properties from a complex property. + * + * Takes care of including bundle-specific properties for entity reference + * properties. + * + * @param \Drupal\Core\TypedData\ComplexDataDefinitionInterface $property + * The base definition. + * + * @return \Drupal\Core\TypedData\DataDefinitionInterface[] + * The nested properties, keyed by property name. + */ + public static function getNestedProperties(ComplexDataDefinitionInterface $property) { + $nested_properties = $property->getPropertyDefinitions(); + if ($property instanceof EntityDataDefinitionInterface) { + $container = \Drupal::getContainer(); + $bundles = $container->get('entity_type.bundle.info') + ->getBundleInfo($property->getEntityTypeId()); + $field_manager = $container->get('entity_field.manager'); + foreach ($bundles as $bundle => $bundle_label) { + $bundle_properties = $field_manager->getFieldDefinitions($property->getEntityTypeId(), $bundle); + $nested_properties += $bundle_properties; + } + } + return $nested_properties; + } + + /** * Retrieves a nested property from a list of properties. * * @param \Drupal\Core\TypedData\DataDefinitionInterface[] $properties @@ -327,7 +354,7 @@ public static function retrieveNestedProperty(array $properties, $property_path) return NULL; } - return static::retrieveNestedProperty($property->getPropertyDefinitions(), $nested_path); + return static::retrieveNestedProperty(static::getNestedProperties($property), $nested_path); } /** @@ -757,28 +784,4 @@ public static function splitCombinedId($combined_id) { return array(NULL, $combined_id); } - /** - * Get nested properties. - * - * If we have bundles, we have to arrive up to the fields that are in the - * bundle. - * - * @param \Drupal\Core\TypedData\DataDefinitionInterface[] $property - * The base definition. - * - * @return \Drupal\Core\Field\FieldDefinitionInterface[] - * The nested properties and fields of the entity. - */ - public static function getNestedProperties($property) { - $nested_properties = $property->getPropertyDefinitions(); - if ($property instanceof EntityDataDefinitionInterface) { - $bundles = \Drupal::entityManager()->getBundleInfo($property->getEntityTypeId()); - foreach ($bundles as $bundle => $bundle_label) { - $bundle_properties = \Drupal::entityManager()->getFieldDefinitions($property->getEntityTypeId(), $bundle); - $nested_properties += $bundle_properties; - } - } - return $nested_properties; - } - }