Skip to content
Snippets Groups Projects
Commit 598517df authored by Robin De Herdt's avatar Robin De Herdt Committed by Floris Walraet
Browse files

Issue #3473055 by robindh: Views area plugin will not work for some entity types

parent 25c6c68b
No related branches found
No related tags found
1 merge request!4Add utility test that should fail initially
Pipeline #291240 passed with warnings
......@@ -18,6 +18,7 @@ class Utility {
*
* @param string $item_id
* The Azure index item ID.
* Structured as <datasource>_<entity_type>_<entity_id>_<langcode>.
* @param bool $include_langcode
* (optional) Whether to the language code in the entity URI. Make sure this
* is FALSE when using the entity URI in routing. Defaults to FALSE.
......@@ -26,9 +27,20 @@ class Utility {
* The entity URI.
*/
public static function itemIdToEntityUri(string $item_id, bool $include_langcode = FALSE): string {
[$datasource, $bundle, $eid, $langcode] = explode('_', $item_id);
$parts = explode('_', $item_id);
$parts_count = count($parts);
$parts = [$datasource, implode('/', [$bundle, $eid])];
$datasource = $parts[0];
$entity_id = $parts[$parts_count - 2];
$langcode = $parts[$parts_count - 1];
// By exclusion of other parts, the remainder of the item
// ID should be the entity type (e.g. node, taxonomy_term, ..).
unset($parts[0], $parts[$parts_count - 2], $parts[$parts_count - 1]);
$entity_type_id = implode('_', $parts);
// Reconstruct the drupal entity uri.
$parts = [$datasource, implode('/', [$entity_type_id, $entity_id])];
// Include the langcode if specifically requested.
if ($include_langcode) {
......
<?php
namespace Drupal\Tests\search_api_aais\Unit;
use Drupal\search_api_aais\Utility;
use Drupal\Tests\UnitTestCase;
/**
* Tests the Search API Azure AI Search utility class.
*
* @coversDefaultClass \Drupal\search_api_aais\Utility
*
* @group search_api_aais
*/
class UtilityTest extends UnitTestCase {
/**
* Test the itemIdToEntityUri() function.
*
* @dataProvider itemIdToEntityUriProvider
*/
public function testItemIdToEntityUri($expected, $value) {
$this->assertEquals($expected, Utility::itemIdToEntityUri($value));
}
/**
* Test the itemIdToEntityUri() function with langcode.
*
* @dataProvider itemIdToEntityUriWithLangcodeProvider
*/
public function testItemIdToEntityUriWithLangcode($expected, $value) {
$this->assertEquals($expected, Utility::itemIdToEntityUri($value, TRUE));
}
/**
* Provides values for testing the itemIdToEntityUri() methods.
*
* @return array[]
* A multidimensional array containing the values.
*/
public static function itemIdToEntityUriProvider(): array {
return [
['entity:node/1', 'entity_node_1_en'],
['entity:taxonomy_term/1', 'entity_taxonomy_term_1_en'],
['entity:some_entity_type/1', 'entity_some_entity_type_1_en'],
];
}
/**
* Provides values for testing the itemIdToEntityUri() methods.
*
* @return array[]
* A multidimensional array containing the values.
*/
public static function itemIdToEntityUriWithLangcodeProvider(): array {
return [
['entity:node/1:en', 'entity_node_1_en'],
['entity:taxonomy_term/1:en', 'entity_taxonomy_term_1_en'],
['entity:some_entity_type/1:en', 'entity_some_entity_type_1_en'],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment