From e721477d876eb8b35aa5ae418edf7d003fdb6343 Mon Sep 17 00:00:00 2001 From: Alexander Varwijk Date: Sat, 7 Apr 2018 20:05:09 +0200 Subject: [PATCH] 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. --- config/install/yoast_seo.settings.yml | 5 +++ config/schema/yoast_seo.schema.yml | 11 +++++ config/yoast_seo.yml | 13 ------ js/yoast_seo.js | 33 +++++++-------- .../Field/FieldWidget/YoastSeoWidget.php | 12 +++--- src/SeoManager.php | 42 ++++++++----------- yoast_seo.install | 15 +++++++ 7 files changed, 69 insertions(+), 62 deletions(-) create mode 100644 config/install/yoast_seo.settings.yml delete mode 100644 config/yoast_seo.yml diff --git a/config/install/yoast_seo.settings.yml b/config/install/yoast_seo.settings.yml new file mode 100644 index 0000000..93f53bb --- /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 94b5b97..edf4d1a 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 5361f48..0000000 --- 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 ca21e0a..ff1bcd8 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 ea41e8c..2996702 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 c477984..77211a4 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 77f2cc1..44f7e75 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(); +} -- GitLab