diff --git a/config/install/yoast_seo.settings.yml b/config/install/yoast_seo.settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..93f53bbad0b4e8350fdb70d8c048628b8e67d84e --- /dev/null +++ b/config/install/yoast_seo.settings.yml @@ -0,0 +1,5 @@ +score_rules: + 0: 'Not available' + 8: 'Good' + 1: 'Bad' + 5: 'Okay' diff --git a/config/schema/yoast_seo.schema.yml b/config/schema/yoast_seo.schema.yml index 94b5b977c94722b04425216ba6f72cb9416073ff..edf4d1a8d231d3707af971e04669f22e9b4b6482 100644 --- a/config/schema/yoast_seo.schema.yml +++ b/config/schema/yoast_seo.schema.yml @@ -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' diff --git a/config/yoast_seo.yml b/config/yoast_seo.yml deleted file mode 100644 index 5361f488f3c109c78de7e7dcdab8b4f4ad4435f4..0000000000000000000000000000000000000000 --- a/config/yoast_seo.yml +++ /dev/null @@ -1,13 +0,0 @@ -score_to_status_rules: - na: - equal: 0 - bad: - min: 0 - max: 4 - ok: - min: 4 - max: 7 - good: - min: 7 - max: 10 - default: bad diff --git a/js/yoast_seo.js b/js/yoast_seo.js index ca21e0adabede0ec5167a32201c2a55a7e5b36c0..ff1bcd802e73600ec69a3a2317670d9c8a8e9349 100644 --- a/js/yoast_seo.js +++ b/js/yoast_seo.js @@ -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'; - } - else if (score > 7) { - rating = 'good'; + 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]; + } } - return Drupal.t('SEO: ' + rating + ''); + return Drupal.t('Unknown'); }; /** diff --git a/src/Plugin/Field/FieldWidget/YoastSeoWidget.php b/src/Plugin/Field/FieldWidget/YoastSeoWidget.php index ea41e8c75419e27dde4ac89a5b2819405fbecfca..2996702073affa95b835de14607ccf23ef5759b5 100644 --- a/src/Plugin/Field/FieldWidget/YoastSeoWidget.php +++ b/src/Plugin/Field/FieldWidget/YoastSeoWidget.php @@ -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' => [], ]; diff --git a/src/SeoManager.php b/src/SeoManager.php index c477984e448142e061d45a2bc8832750412a61d4..77211a404a425f03af77c9a4169d9b993ebd606b 100644 --- a/src/SeoManager.php +++ b/src/SeoManager.php @@ -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); + } } diff --git a/yoast_seo.install b/yoast_seo.install index 77f2cc169cda397dcbfc52c09d8ab03f5c98075e..44f7e755223ed680101d2b83995b32844cc31018 100644 --- a/yoast_seo.install +++ b/yoast_seo.install @@ -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(); +}