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:
edit-description:
type: boolean
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 @@
};
/**
* 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
* @returns rating
* @param score integer
* @returns integer
* The human readable rating
*/
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) {
rating = 'na';
}
else if (score <= 4) {
rating = 'bad';
}
else if (score > 4 && score <= 7) {
rating = 'ok';
console.log(thresholds);
for (var i in thresholds) {
var minimum = thresholds[i];
console.log(score, minimum);
if (score >= minimum) {
return this.config.score_rules[minimum];
}
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
*
* @var \Drupal\yoast_seo\SeoManager
*/
protected $yoastSeoManager;
protected $seoManager;
/**
* Target elements for Javascript.
......@@ -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) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
$this->yoastSeoManager = $manager;
$this->seoManager = $manager;
}
/**
......@@ -106,7 +106,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
$element['overall_score'] = [
'#theme' => 'overall_score',
'#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'] = [
......@@ -237,7 +237,7 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
*/
protected function getJavaScriptConfiguration() {
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: Translate to something usable by YoastSEO.js.
......@@ -248,8 +248,8 @@ class YoastSeoWidget extends WidgetBase implements ContainerFactoryPluginInterfa
'language' => $language,
// Set the base for URL analysis.
'base_root' => $base_root,
// Set up score to indiciator word rules.
'score_status' => $score_to_status_rules,
// Set up score to indicator word rules.
'score_rules' => $score_rules,
// Possibly allow properties to be editable.
'enable_editing' => [],
];
......
......@@ -126,38 +126,30 @@ class SeoManager {
* Status corresponding to the score.
*/
public function getScoreStatus($score) {
$rules = $this->getConfiguration()['score_to_status_rules'];
$default = $rules['default'];
unset($rules['default']);
foreach ($rules as $status => $status_rules) {
$min_max_isset = isset($status_rules['min']) && isset($status_rules['max']);
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;
$rules = $this->getScoreRules();
foreach ($rules as $minimum => $label) {
// As soon as our score is bigger than a rules threshold, use that label.
if ($score >= $minimum) {
return $label;
}
}
return $default;
return $this->t('Unknown');
}
/**
* Get configuration from Yaml file.
*
* @return mixed
* Configuration details will be returned.
* Retrieves the score rules from configuration.
*
* @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() {
$conf = Yaml::parse(
file_get_contents(
drupal_get_path('module', 'yoast_seo') . '/config/yoast_seo.yml'
)
);
return $conf;
}
public function getScoreRules() {
$rules = \Drupal::config('yoast_seo.settings')->get('score_rules');
// 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() {
}
}
/**
* 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