Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions analyze_ai_sentiments.install
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,58 @@ function analyze_ai_sentiments_install() {
$database->schema()->createTable($table_name, $table_spec);
}
}

// Install default sentiment configurations.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('analyze_ai_sentiments.settings');

// Only install defaults if no sentiments are configured.
if (empty($config->get('sentiments'))) {
$default_sentiments = [
'overall' => [
'id' => 'overall',
'label' => 'Overall Sentiments',
'min_label' => 'Negative',
'mid_label' => 'Neutral',
'max_label' => 'Positive',
'weight' => 0,
],
'engagement' => [
'id' => 'engagement',
'label' => 'Engagement Level',
'min_label' => 'Passive',
'mid_label' => 'Balanced',
'max_label' => 'Interactive',
'weight' => 1,
],
'trust' => [
'id' => 'trust',
'label' => 'Trust/Credibility',
'min_label' => 'Promotional',
'mid_label' => 'Balanced',
'max_label' => 'Authoritative',
'weight' => 2,
],
'objectivity' => [
'id' => 'objectivity',
'label' => 'Objectivity',
'min_label' => 'Subjective',
'mid_label' => 'Mixed',
'max_label' => 'Objective',
'weight' => 3,
],
'complexity' => [
'id' => 'complexity',
'label' => 'Technical Complexity',
'min_label' => 'Basic',
'mid_label' => 'Moderate',
'max_label' => 'Complex',
'weight' => 4,
],
];

$config->set('sentiments', $default_sentiments)->save();
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions analyze_ai_sentiments.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
analyze_ai_sentiments.sentiment.add:
route_name: analyze_ai_sentiments.add_sentiments
title: 'Add sentiment'
appears_on:
- analyze_ai_sentiments.settings
51 changes: 31 additions & 20 deletions src/Form/AddSentimentsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\analyze_ai_sentiments\Service\SentimentsStorageService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -19,14 +20,27 @@ class AddSentimentsForm extends FormBase {
*/
protected $configFactory;

/**
* The sentiments storage service.
*
* @var \Drupal\analyze_ai_sentiments\Service\SentimentsStorageService
*/
protected $sentimentsStorage;

/**
* Constructs a new AddSentimentsForm.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\analyze_ai_sentiments\Service\SentimentsStorageService $sentiments_storage
* The sentiments storage service.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
public function __construct(
ConfigFactoryInterface $config_factory,
SentimentsStorageService $sentiments_storage,
) {
$this->configFactory = $config_factory;
$this->sentimentsStorage = $sentiments_storage;
}

/**
Expand All @@ -35,7 +49,8 @@ public function __construct(ConfigFactoryInterface $config_factory) {
public static function create(ContainerInterface $container): static {
/** @var static */
return new self(
$container->get('config.factory')
$container->get('config.factory'),
$container->get('analyze_ai_sentiments.storage')
);
}

Expand All @@ -56,9 +71,7 @@ public function getFormId() {
* TRUE if the sentiments exists, FALSE otherwise.
*/
public function sentimentsExists($id) {
$config = $this->configFactory->get('analyze_ai_sentiments.settings');
$sentiments = $config->get('sentiments') ?: [];
return isset($sentiments[$id]);
return $this->sentimentsStorage->sentimentExists($id);
}

/**
Expand Down Expand Up @@ -157,27 +170,25 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
/** @var array<string, mixed> $form */
$config = $this->configFactory->getEditable('analyze_ai_sentiments.settings');
$sentiments = $config->get('sentiments') ?: [];
$values = $form_state->getValues();

// Get the maximum weight and add 1.
$sentiments = $this->sentimentsStorage->getAllSentiments();
$max_weight = 0;
foreach ($sentiments as $sentiments) {
$max_weight = max($max_weight, $sentiments['weight'] ?? 0);
foreach ($sentiments as $sentiment) {
$max_weight = max($max_weight, $sentiment['weight'] ?? 0);
}

$values = $form_state->getValues();
$sentiments[$values['id']] = [
'id' => $values['id'],
'label' => $values['label'],
'min_label' => $values['min_label'],
'mid_label' => $values['mid_label'],
'max_label' => $values['max_label'],
'weight' => $max_weight + 1,
];
$this->sentimentsStorage->saveSentiment(
$values['id'],
$values['label'],
$values['min_label'],
$values['mid_label'],
$values['max_label'],
$max_weight + 1
);

$config->set('sentiments', $sentiments)->save();
$this->messenger()->addStatus($this->t('Added new sentiments %label.', ['%label' => $values['label']]));
$this->messenger()->addStatus($this->t('Added new sentiment %label.', ['%label' => $values['label']]));
$form_state->setRedirectUrl(Url::fromRoute('analyze_ai_sentiments.settings'));
}

Expand Down
Loading