Commit e721477d authored by Kingdutch's avatar Kingdutch

Move score rules to proper Drupal configuration

Instead of manually reading a YML file we now use the Drupal config
system to store the SEO scores. This makes it configurable and
easier to maintain.

This configuration is now also used by the javascript.
parent 43827f8b
score_rules:
0: 'Not available'
8: 'Good'
1: 'Bad'
5: 'Okay'
...@@ -8,3 +8,14 @@ field.widget.settings.yoast_seo_widget: ...@@ -8,3 +8,14 @@ field.widget.settings.yoast_seo_widget:
edit-description: edit-description:
type: boolean type: boolean
label: 'Description Editing' label: 'Description Editing'
yoast_seo.settings:
type: config_object
label: 'Real-Time SEO Settings'
mapping:
score_rules:
type: sequence
label: 'Labels for the scores, indexed by the minimum required score for that label'
sequence:
type: string
label: 'Score Label'
score_to_status_rules:
na:
equal: 0
bad:
min: 0
max: 4
ok:
min: 4
max: 7
good:
min: 7
max: 10
default: bad
...@@ -290,30 +290,27 @@ ...@@ -290,30 +290,27 @@
}; };
/** /**
* retuns a string that is used as a CSS class, based on the numeric score * Returns a string that indicates the score to the user.
* *
* TODO: This can probably be replaced by the seoAssessorPresentor which includes a presenter configuration that does this. * @param score integer
* * @returns integer
* @param score * The human readable rating
* @returns rating
*/ */
Orchestrator.prototype.scoreToRating = function (score) { Orchestrator.prototype.scoreToRating = function (score) {
var rating; // Get the label thresholds from high to low.
var thresholds = Object.keys(this.config.score_rules).sort().reverse();
if (score === 0) { console.log(thresholds);
rating = 'na';
} for (var i in thresholds) {
else if (score <= 4) { var minimum = thresholds[i];
rating = 'bad'; console.log(score, minimum);
} if (score >= minimum) {
else if (score > 4 && score <= 7) { return this.config.score_rules[minimum];
rating = 'ok'; }
}
else if (score > 7) {
rating = 'good';
} }
return Drupal.t('SEO: <strong>' + rating + '</strong>'); return Drupal.t('Unknown');
}; };
/** /**
......
...@@ -39,7 +39,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa ...@@ -39,7 +39,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
* *
* @var \Drupal\yoast_seo\SeoManager * @var \Drupal\yoast_seo\SeoManager
*/ */
protected $yoastSeoManager; protected $seoManager;
/** /**
* Target elements for Javascript. * Target elements for Javascript.
...@@ -74,7 +74,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa ...@@ -74,7 +74,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, SeoManager $manager) { public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, SeoManager $manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->yoastSeoManager = $manager; $this->seoManager = $manager;
} }
/** /**
...@@ -106,7 +106,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa ...@@ -106,7 +106,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
$element['overall_score'] = [ $element['overall_score'] = [
'#theme' => 'overall_score', '#theme' => 'overall_score',
'#overall_score_target_id' => self::$jsTargets['overall_score_target_id'], '#overall_score_target_id' => self::$jsTargets['overall_score_target_id'],
'#overall_score' => $this->yoastSeoManager->getScoreStatus(isset($items[$delta]->status) ? $items[$delta]->status : 0), '#overall_score' => $this->seoManager->getScoreStatus(isset($items[$delta]->status) ? $items[$delta]->status : 0),
]; ];
$element['status'] = [ $element['status'] = [
...@@ -237,7 +237,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa ...@@ -237,7 +237,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
*/ */
protected function getJavaScriptConfiguration() { protected function getJavaScriptConfiguration() {
global $base_root; global $base_root;
$score_to_status_rules = $this->yoastSeoManager->getConfiguration()['score_to_status_rules']; $score_rules = $this->seoManager->getScoreRules();
// TODO: Use dependency injection for language manager. // TODO: Use dependency injection for language manager.
// TODO: Translate to something usable by YoastSEO.js. // TODO: Translate to something usable by YoastSEO.js.
...@@ -248,8 +248,8 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa ...@@ -248,8 +248,8 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
'language' => $language, 'language' => $language,
// Set the base for URL analysis. // Set the base for URL analysis.
'base_root' => $base_root, 'base_root' => $base_root,
// Set up score to indiciator word rules. // Set up score to indicator word rules.
'score_status' => $score_to_status_rules, 'score_rules' => $score_rules,
// Possibly allow properties to be editable. // Possibly allow properties to be editable.
'enable_editing' => [], 'enable_editing' => [],
]; ];
......
...@@ -126,38 +126,30 @@ class SeoManager { ...@@ -126,38 +126,30 @@ class SeoManager {
* Status corresponding to the score. * Status corresponding to the score.
*/ */
public function getScoreStatus($score) { public function getScoreStatus($score) {
$rules = $this->getConfiguration()['score_to_status_rules']; $rules = $this->getScoreRules();
$default = $rules['default'];
unset($rules['default']); foreach ($rules as $minimum => $label) {
// As soon as our score is bigger than a rules threshold, use that label.
foreach ($rules as $status => $status_rules) { if ($score >= $minimum) {
$min_max_isset = isset($status_rules['min']) && isset($status_rules['max']); return $label;
if (isset($status_rules['equal']) && $status_rules['equal'] == $score) {
return $status;
}
elseif ($min_max_isset && $score > $status_rules['min'] && $score <= $status_rules['max']) {
return $status;
} }
} }
return $default; return $this->t('Unknown');
} }
/** /**
* Get configuration from Yaml file. * Retrieves the score rules from configuration.
*
* @return mixed
* Configuration details will be returned.
* *
* @TODO: Turn this into proper Drupal configuration! * @return string[] rules
* A list of labels indexed by the minimum score required. Ordered from high
* to low.
*/ */
public function getConfiguration() { public function getScoreRules() {
$conf = Yaml::parse( $rules = \Drupal::config('yoast_seo.settings')->get('score_rules');
file_get_contents(
drupal_get_path('module', 'yoast_seo') . '/config/yoast_seo.yml'
)
);
return $conf;
}
// Ensure rules are sorted from high to low score.
ksort($rules);
return array_reverse($rules, true);
}
} }
...@@ -69,3 +69,18 @@ function yoast_seo_update_8202() { ...@@ -69,3 +69,18 @@ function yoast_seo_update_8202() {
} }
} }
/**
* Move score label configuration into module settings.
*/
function yoast_seo_update_8203() {
\Drupal::configFactory()
->getEditable('yoast_seo.settings')
->set('score_rules', [
0 => 'Not available',
8 => 'Good',
1 => 'Bad',
5 => 'Okay',
])
->save();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment