From db97e674b48525d0b3d2c7481d98304b3fbfc7e2 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Tue, 31 Jan 2023 08:48:46 +0200 Subject: [PATCH 1/4] added missing dependencies --- modules/helfi_test_content/helfi_test_content.info.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/helfi_test_content/helfi_test_content.info.yml b/modules/helfi_test_content/helfi_test_content.info.yml index 02a1fc3fb..c5c655761 100644 --- a/modules/helfi_test_content/helfi_test_content.info.yml +++ b/modules/helfi_test_content/helfi_test_content.info.yml @@ -5,7 +5,10 @@ core_version_requirement: '^8 || ^9' package: HELfi dependencies: - default_content:default_content + - helfi_charts:helfi_charts - helfi_content:helfi_content + - helfi_media:helfi_media + - helfi_media_map_config:helfi_media_map_config - helfi_tpr:helfi_tpr default_content: menu_link_content: From c32ef3ede7b07933a57c1690afbd3b6b51a82188 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Tue, 31 Jan 2023 12:57:24 +0200 Subject: [PATCH 2/4] UHF-6836: Add processor for filtering schools --- .../Plugin/search_api/processor/IsSchool.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 helfi_features/helfi_react_search/src/Plugin/search_api/processor/IsSchool.php diff --git a/helfi_features/helfi_react_search/src/Plugin/search_api/processor/IsSchool.php b/helfi_features/helfi_react_search/src/Plugin/search_api/processor/IsSchool.php new file mode 100644 index 000000000..4d4ebd6b1 --- /dev/null +++ b/helfi_features/helfi_react_search/src/Plugin/search_api/processor/IsSchool.php @@ -0,0 +1,90 @@ +getDatasources() as $datasource) { + $entityTypeId = $datasource->getEntityTypeId(); + if (!$entityTypeId) { + continue; + } + + if ($entityTypeId === 'tpr_unit') { + return TRUE; + } + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function alterIndexedItems(array &$items): void { + foreach ($items as $id => $item) { + $shouldIndex = $this->shouldIndex($item); + + if (!$shouldIndex) { + unset($items[$id]); + } + } + } + + /** + * Determine if entity should be indexed. + * + * @param \Drupal\search_api\Item\ItemInterface $item + * Item to check. + * + * @return bool + * The result. + */ + protected function shouldIndex(ItemInterface $item): bool { + $object = $item->getOriginalObject()->getValue(); + $fieldValues = $object->get('services')->getValue(); + + $flatValues = array_map(function (array $fieldValue) { + return $fieldValue['target_id']; + }, $fieldValues); + + if (count(array_intersect($flatValues, self::SCHOOL_SERVICE_IDS))) { + return TRUE; + } + + return FALSE; + } + +} From 9189a39799a02d7ae884ea6a4c9bf90d667a06aa Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Tue, 31 Jan 2023 13:27:00 +0200 Subject: [PATCH 3/4] UHF-6836: Expose elastic proxy url via hook --- .../helfi_react_search.module | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/helfi_features/helfi_react_search/helfi_react_search.module b/helfi_features/helfi_react_search/helfi_react_search.module index f5855dfdc..e76834604 100644 --- a/helfi_features/helfi_react_search/helfi_react_search.module +++ b/helfi_features/helfi_react_search/helfi_react_search.module @@ -14,3 +14,20 @@ function helfi_react_search_event_list_allowed_values_function() : array { 5 => 5, ]; } + +function helfi_react_search_preprocess_paragraph(array &$variables) { + $reactParagraphs = [ + 'school_search' + ]; + + $type = $variables['paragraph']->getType(); + + $config = \Drupal::config('elastic_proxy.settings'); + if ( + isset($variables['paragraph']) && + in_array($variables['paragraph']->getType(), $reactParagraphs) && + $proxyUrl = $config->get('elastic_proxy_url') + ) { + $variables['#attached']['drupalSettings']['helfi_react_search']['elastic_proxy_url'] = $proxyUrl; + } +} From a2d296d9e3f8fd9c3c33573739624ed0cfa80646 Mon Sep 17 00:00:00 2001 From: Jere Ljungberg Date: Fri, 3 Feb 2023 15:23:53 +0200 Subject: [PATCH 4/4] UHF-6836: Add processor for indexing media as objects --- .../processor/MediaReferenceToObject.php | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 helfi_features/helfi_react_search/src/Plugin/search_api/processor/MediaReferenceToObject.php diff --git a/helfi_features/helfi_react_search/src/Plugin/search_api/processor/MediaReferenceToObject.php b/helfi_features/helfi_react_search/src/Plugin/search_api/processor/MediaReferenceToObject.php new file mode 100644 index 000000000..96e43d974 --- /dev/null +++ b/helfi_features/helfi_react_search/src/Plugin/search_api/processor/MediaReferenceToObject.php @@ -0,0 +1,145 @@ + $this->t('Media objects'), + 'description' => $this->t('Media entities as objects. Define desired media reference fields from processor settings.'), + 'type' => 'object', + 'processor_id' => $this->getPluginId(), + ]; + + $properties['media_as_objects'] = new ProcessorProperty($definition); + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $formState): array { + $form['#description'] = $this->t('Select fields to apply the filter to.'); + + $datasources = $this->index->getDatasources(); + $fieldDefs = []; + + foreach ($datasources as $datasource) { + $entityTypeId = $datasource->getEntityTypeId(); + $bundles = $datasource->getBundles(); + $entityFieldManager = $datasource->getEntityFieldManager(); + + $fieldDefs = array_map(function ($bundle) use ($entityFieldManager, $entityTypeId) { + return $entityFieldManager->getFieldDefinitions($entityTypeId, $bundle); + }, array_keys($bundles)); + } + + foreach (array_merge(...$fieldDefs) as $def) { + if (!$def instanceof BaseFieldDefinition || $def->getType() !== 'entity_reference') { + continue; + } + + $targetDef = $def->getPropertyDefinition('entity')->getTargetDefinition()->getEntityTypeId(); + + if ($targetDef !== 'media') { + continue; + } + + $enabled = !empty($this->configuration['fields'][$def->getName()]); + $form['fields'][$def->getName()] = [ + '#type' => 'checkbox', + '#title' => $def->getLabel()->render(), + '#default_value' => $enabled, + ]; + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function addFieldValues(ItemInterface $item): void { + $fields = $this->getEnabledFields(); + $object = $item->getOriginalObject()->getValue(); + + foreach ($fields as $key => $field) { + $media = $object->get($key)->entity; + + if ( + !$media || + !($image = $media->get('field_media_image')) || + !($file = $image->entity) + ) { + continue; + } + + $imageStyle = ImageStyle::load('3_2_l'); + $imagePath = $file->getFileUri(); + $imageUri = $imageStyle->buildUri($imagePath); + + if (!file_exists($imageUri)) { + $imageStyle->createDerivative($imagePath, $imageUri); + } + + $url = $imageStyle->buildUrl($imagePath); + + $values = [ + 'alt' => $image->alt, + 'photographer' => $media->get('field_photographer')->value, + 'title' => $image->title, + 'url' => $url, + ]; + + $itemFields = $item->getFields(); + $itemFields = $this->getFieldsHelper() + ->filterForPropertyPath($itemFields, NULL, 'media_as_objects'); + foreach ($itemFields as $itemField) { + $itemField->addValue([$key => $values]); + } + } + } + + /** + * Return enabled fields. + */ + protected function getEnabledFields() { + return array_filter($this->configuration['fields']); + } + +}