Skip to content

Commit

Permalink
Merge pull request #709 from City-of-Helsinki/UHF-10631-recommendatio…
Browse files Browse the repository at this point in the history
…ns-save-logic

UHF-10631: recommendations save logic
  • Loading branch information
hyrsky authored Sep 24, 2024
2 parents b25c41c + 4010966 commit c703667
Show file tree
Hide file tree
Showing 27 changed files with 919 additions and 227 deletions.
24 changes: 8 additions & 16 deletions conf/cmi/core.entity_form_display.node.news_article.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ dependencies:
module:
- field_group
- hdbt_admin_tools
- helfi_annif
- linkit
- media_library
- paragraphs
- path
- publication_date
- radioactivity
- readonly_field_widget
- scheduler
- select2
third_party_settings:
Expand Down Expand Up @@ -78,9 +78,9 @@ third_party_settings:
required_fields: true
group_automatically_recommended:
children:
- annif_suggested_topics
- in_recommendations
- show_annif_block
- annif_keywords
label: 'Automatically recommended content'
region: content
parent_name: ''
Expand All @@ -99,19 +99,11 @@ targetEntityType: node
bundle: news_article
mode: default
content:
annif_keywords:
type: readonly_field_widget
weight: 30
annif_suggested_topics:
type: suggested_topics_reference
weight: 31
region: content
settings:
label: above
formatter_type: entity_reference_label
formatter_settings:
entity_reference_entity_view:
view_mode: default
entity_reference_label:
link: false
show_description: false
settings: { }
third_party_settings: { }
created:
type: datetime_timestamp
Expand Down Expand Up @@ -231,7 +223,7 @@ content:
third_party_settings: { }
in_recommendations:
type: boolean_checkbox
weight: 28
weight: 32
region: content
settings:
display_label: true
Expand Down Expand Up @@ -275,7 +267,7 @@ content:
third_party_settings: { }
show_annif_block:
type: boolean_checkbox
weight: 29
weight: 33
region: content
settings:
display_label: true
Expand Down
25 changes: 8 additions & 17 deletions conf/cmi/core.entity_form_display.node.news_item.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ dependencies:
module:
- field_group
- hdbt_admin_tools
- helfi_annif
- linkit
- media_library
- paragraphs
- path
- publication_date
- radioactivity
- readonly_field_widget
- scheduler
- select2
third_party_settings:
Expand Down Expand Up @@ -79,7 +79,6 @@ third_party_settings:
group_updating_news:
children:
- field_news_item_updating_news
- field_news_item_updating_news
label: 'Updating news'
region: content
parent_name: ''
Expand All @@ -94,9 +93,9 @@ third_party_settings:
required_fields: false
group_automatically_recommended:
children:
- annif_suggested_topics
- in_recommendations
- show_annif_block
- annif_keywords
label: 'Automatically recommended content'
region: content
parent_name: ''
Expand All @@ -115,19 +114,11 @@ targetEntityType: node
bundle: news_item
mode: default
content:
annif_keywords:
type: readonly_field_widget
weight: 2
annif_suggested_topics:
type: suggested_topics_reference
weight: 29
region: content
settings:
label: above
formatter_type: entity_reference_label
formatter_settings:
entity_reference_entity_view:
view_mode: default
entity_reference_label:
link: false
show_description: false
settings: { }
third_party_settings: { }
created:
type: datetime_timestamp
Expand Down Expand Up @@ -261,7 +252,7 @@ content:
third_party_settings: { }
in_recommendations:
type: boolean_checkbox
weight: 0
weight: 30
region: content
settings:
display_label: true
Expand Down Expand Up @@ -305,7 +296,7 @@ content:
third_party_settings: { }
show_annif_block:
type: boolean_checkbox
weight: 1
weight: 31
region: content
settings:
display_label: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
field.value.helfi_annif_suggestion:
type: mapping
label: Default value
mapping:
target_id:
type: string
label: 'Value'
target_uuid:
type: uuid
score:
type: label
label: Score

field.value.suggested_topics_reference:
type: mapping
label: Default value
mapping:
value:
type: label
label: Value
3 changes: 2 additions & 1 deletion public/modules/custom/helfi_annif/helfi_annif.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ dependencies:
- drupal:text
- drupal:language
- helfi_api_base:helfi_api_base
- helfi_etusivu:helfi_etusivu
- readonly_field_widget:readonly_field_widget
- drupal:field
# TODO: remove dependency to etusivu.
- helfi_etusivu:helfi_etusivu
- helfi_node_news_item:helfi_node_news_item
- helfi_node_news_article:helfi_node_news_article
'interface translation project': helfi_annif
Expand Down
46 changes: 46 additions & 0 deletions public/modules/custom/helfi_annif/helfi_annif.install
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,49 @@ function helfi_annif_update_9001(): void {
);
}
}

/**
* Creates the database table for the HelfiRecommendations entity.
*/
function helfi_annif_update_9002(): void {
$definition = \Drupal::entityTypeManager()->getDefinition('suggested_topics');

\Drupal::entityDefinitionUpdateManager()
->installEntityType($definition);
}

/**
* Updates field storage definition for etusivu news entities.
*/
function helfi_annif_update_9003(): void {
$updates = [
'annif_suggested_topics' => [
'node' => [
'news_article',
'news_item',
],
],
];

$entity_field_manager = \Drupal::service('entity_field.manager');
$update_manager = \Drupal::entityDefinitionUpdateManager();

foreach ($updates as $field => $entity_types) {
foreach ($entity_types as $entity_type_id => $bundles) {
foreach ($bundles as $bundle) {
$field_definitions = $entity_field_manager->getFieldDefinitions($entity_type_id, $bundle);

$update_manager->installFieldStorageDefinition(
$field,
$entity_type_id,
'helfi_annif',
$field_definitions[$field],
);
}
}
}

\Drupal::messenger()->addMessage('Run drush helfi:annif-fix-references');

// @todo remove obsolete field annif_keywords in a future update hook.
}
53 changes: 27 additions & 26 deletions public/modules/custom/helfi_annif/helfi_annif.module
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity\BundleFieldDefinition;
use Drupal\helfi_annif\KeywordManager;
use Drupal\helfi_annif\RecommendableInterface;
use Drupal\helfi_annif\TextConverter\Document;
use Drupal\helfi_annif\TopicsManager;

/**
* Implements hook_theme().
Expand All @@ -34,7 +34,7 @@ function helfi_annif_theme() : array {
/**
* Implements hook_themes_installed().
*/
function helfi_annif_themes_installed($theme_list) {
function helfi_annif_themes_installed($theme_list): void {
/** @var Drupal\helfi_platform_config\Helper\BlockInstaller $block_installer */
$block_installer = Drupal::service('helfi_platform_config.helper.block_installer');

Expand Down Expand Up @@ -74,7 +74,7 @@ function helfi_annif_themes_installed($theme_list) {
* @return array[]
* The block configurations.
*/
function helfi_annif_get_block_configurations(string $theme) {
function helfi_annif_get_block_configurations(string $theme): array {
return [
'hdbt_subtheme_hdbt_subtheme_aipoweredrecommendations' => [
'block' => [
Expand Down Expand Up @@ -132,9 +132,9 @@ function helfi_annif_get_block_configurations(string $theme) {
*/
function helfi_annif_entity_insert(EntityInterface $entity) : void {
if ($entity instanceof RecommendableInterface) {
/** @var \Drupal\helfi_annif\KeywordManager $keywordManager */
$keywordManager = \Drupal::service(KeywordManager::class);
$keywordManager->queueEntity($entity, TRUE);
/** @var \Drupal\helfi_annif\TopicsManager $topicsManager */
$topicsManager = \Drupal::service(TopicsManager::class);
$topicsManager->queueEntity($entity, TRUE);
}
}

Expand All @@ -143,9 +143,9 @@ function helfi_annif_entity_insert(EntityInterface $entity) : void {
*/
function helfi_annif_entity_update(EntityInterface $entity) : void {
if ($entity instanceof RecommendableInterface) {
/** @var \Drupal\helfi_annif\KeywordManager $keywordManager */
$keywordManager = \Drupal::service(KeywordManager::class);
$keywordManager->queueEntity($entity, TRUE);
/** @var \Drupal\helfi_annif\TopicsManager $topicsManager */
$topicsManager = \Drupal::service(TopicsManager::class);
$topicsManager->queueEntity($entity, TRUE);
}
}

Expand All @@ -159,6 +159,7 @@ function helfi_annif_entity_bundle_field_info_alter(&$fields, EntityTypeInterfac
}
}

// @todo remove dependency to etusivu config.
$recommendable_node_bundles = ['news_item', 'news_article'];
if ($entityType->id() == 'node' && in_array($bundle, $recommendable_node_bundles)) {
foreach (helfi_annif_bundle_fields($entityType->id(), $bundle) as $name => $field) {
Expand All @@ -172,7 +173,10 @@ function helfi_annif_entity_bundle_field_info_alter(&$fields, EntityTypeInterfac
*/
function helfi_annif_entity_field_storage_info(EntityTypeInterface $entity_type): array {
if ($entity_type->id() === 'node') {
return array_merge(helfi_annif_bundle_fields($entity_type->id(), 'news_item'), helfi_annif_bundle_fields($entity_type->id(), 'news_article'));
return array_merge(
helfi_annif_bundle_fields($entity_type->id(), 'news_item'),
helfi_annif_bundle_fields($entity_type->id(), 'news_article')
);
}
return [];
}
Expand Down Expand Up @@ -208,30 +212,27 @@ function helfi_annif_bundle_fields(string $entity_type_id, string $bundle): arra
->setDisplayConfigurable('view', TRUE);
}

$keywordfieldId = 'annif_keywords';
$keywordfieldName = new TranslatableMarkup('Automatically selected news categories', [], ['context' => 'annif']);
$fields['annif_suggested_topics'] = BundleFieldDefinition::create('suggested_topics_reference')
->setName('annif_suggested_topics')
->setLabel(new TranslatableMarkup('Automatically selected news categories', [], ['context' => 'annif']))
->setTargetEntityTypeId($entity_type_id)
->setTargetBundle($bundle)
->setReadonly(TRUE)
->setTranslatable(FALSE)
->setDisplayOptions('form', ['type' => 'suggested_topics_reference'])
->setDisplayConfigurable('form', TRUE);

// @todo remove this.
$keywordfieldId = 'annif_keywords';
$fields[$keywordfieldId] = BundleFieldDefinition::create('entity_reference')
->setName($keywordfieldId)
->setLabel($keywordfieldName)
->setSettings(['target_type' => 'taxonomy_term'])
->setTargetEntityTypeId($entity_type_id)
->setTargetBundle($bundle)
->setDisplayOptions('form',
[
'type' => 'readonly_field_widget',
'third_party_settings' => [],
'settings' =>
[
'formatter_type' => 'entity_reference_label',
],
]
)
->setReadonly(TRUE)
->setTranslatable(FALSE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);

return $fields;
}
Expand Down
6 changes: 4 additions & 2 deletions public/modules/custom/helfi_annif/helfi_annif.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ services:
parent: logger.channel_base
arguments: ['helfi_annif']

Drupal\helfi_annif\KeywordManager: ~
Drupal\helfi_annif\TopicsManager: ~

Drupal\helfi_annif\ReferenceUpdater: ~

Drupal\helfi_annif\RecommendationManager: ~

Drupal\helfi_annif\Client\KeywordClient: ~
Drupal\helfi_annif\Client\ApiClient : ~

Drupal\helfi_annif\TextConverter\TextConverterManager:
tags:
Expand Down
Loading

0 comments on commit c703667

Please sign in to comment.